今天使用树莓派搭建一个TCP服务器,使用HTTP通信,话不多说,直接开始;
首先检查树莓派是都安装curl库;
输入命令:curl www.baidu.com
然后就是建立TCP服务,通过socket通信完成;具体过程:建立socket->绑定端口和IP->Listen->等待客户端连接->断开连接;如果是客户端则是:建立socket->绑定端口和IP->连接服务器->断开连接;
代码如下:
int main(int argc,char **argv)
{
const int port = 9689;//listen port
int listenfd=Socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in serverAddr;
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=INADDR_ANY;
serverAddr.sin_port=htons(port);
Bind(listenfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
Listen(listenfd,5);
while(true)
{
handleAccept(listenfd);
}
}
int Socket(int family , int type,int protocol)
{
int n;
if ( (n = socket(family, type, protocol)) < 0)
{
printf("socket error\r\n");
return -1;
}
return(n);
}
void
Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
if (bind(fd, sa, salen) < 0)
{
printf("bind error\r\n");
exit(-1);
}
}
void
Listen(int fd, int backlog)
{
char *ptr;
/*4can override 2nd argument with environment variable */
if ( (ptr = getenv("LISTENQ")) != NULL)
backlog = atoi(ptr);
if (listen(fd, backlog) < 0)
{
printf("listen error\r\n");
return ;
}
}
int
Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
int n;
again:
if ( (n = accept(fd, sa, salenptr)) < 0) {
#ifdef EPROTO
if (errno == EPROTO || errno == ECONNABORTED)
#else
if (errno == ECONNABORTED)
#endif
goto again;
else
{
printf("accept error\r\n");
return -1;
}
}
return(n);
}
void handleAccept(int listenfd)
{
sockaddr_in clientAddr;
socklen_t clientLen=sizeof(clientAddr);
int connfd=Accept(listenfd,(sockaddr *)&clientAddr,&clientLen);
handleHttp(connfd);
close(connfd);
}
我这里做了一个简单的http例程,就是访问连接服务器后,回复一个网页,代码如下:
void handleHttp(int connfd)
{
if(getRequest(connfd)<0)
{
perror("http request get error");
exit(-1);
}
}
int getRequest(int socket)
{
int msgLen=0;
char send_data[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
memset (buffer,'\0', BUFFER_SIZE);
if ((msgLen = recv(socket, buffer, BUFFER_SIZE, 0)) == -1)
{
printf("Error handling incoming request");
return -1;
}
else
{
printf("recv:\n%s\n",buffer);
}
memset (send_data,'\0', BUFFER_SIZE);
//create_objects(send_data);
stringstream ss;
ss<<buffer;
string method;
ss>>method;
string uri;
ss>>uri;
string version;
ss>>version;
//
string contentType("text/html");
string content("<html><head><title>simple httpserver</title></head><h1>hello eeworld</h1></body></html>");
string contentSize(std::to_string(content.size()));
string head("\r\nHTTP/1.1 ");
string statusCode("200 OK");
string ContentType("\r\nContent-Type: ");
string ServerHead("\r\nServer: localhost");
string ContentLength("\r\nContent-Length: ");
string Date("\r\nDate: ");
string Newline("\r\n");
time_t rawtime;
time(&rawtime);
string message;
message+=head;
message+=statusCode;
message+=ContentType;
message+=contentType;
message+=ServerHead;
message+=ContentLength;
message+=contentSize;
message+=Date;
message+=(string)ctime(&rawtime);
message+=Newline;
int messageLength=message.size();
int n;
n=send(socket,message.c_str(),messageLength,0);
n=send(socket,content.c_str(),content.size(),0);
return n;
}
然后编译一下:
g++ http_test.cpp -o http
在运行./http
服务运行后,我们用浏览器打开试下
打印了我们设置的内容;
我们在使用curl测试一下:
curl 192.168.31.235:9689