Linux下C语言实现TCP通信
2016-04-28 10:22
489 查看
TCP编程:
写一个tcp的服务端,一个tcp客户端。
客户端连接到服务端,然后每隔2s向服务端发送消息:
下面是服务器代码:
写一个tcp的服务端,一个tcp客户端。
客户端连接到服务端,然后每隔2s向服务端发送消息:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define SERVER_IP "127.10.10.11" #define PORT 12345 #define MAXDATASIZE 512 int main() { int fd; int num; char recebuf[MAXDATASIZE]; char sendbuff[200]; int number = 1; struct hostent *he; //the struct that will get information about remote host struct sockaddr_in server; if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("create socket error!\n"); exit(1); } bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); //server.sin_addr = *((struct in_addr *)he->h_addr); server.sin_addr.s_addr = inet_addr(SERVER_IP); //start to conncet if(connect(fd, (struct sockaddr *)&server, sizeof(server)) == -1) { printf("connect error\n"); exit(1); } while(1) { //start to send message snprintf(sendbuff, sizeof(sendbuff), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n <request id=\"%d\" type=\"connect\">\n <say>I am client!</say>\n </request>" ,number); //printf("%s\n", sendbuff); num = send(fd, sendbuff, sizeof(sendbuff), 0); if(num == -1) { printf("send message error\n"); } //start to receive message num = recv(fd, recebuf, MAXDATASIZE, 0); if(num == -1) { printf("receive message error\n"); exit(-1); } recebuf[num] = '\0'; printf("\n\n接收到的回复是:\n"); printf("%s\n\n", recebuf); sleep(2); number++; } close(fd); return 0; }
下面是服务器代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #define PORT 12345 #define MAXDATASIZE 512 #define BACKLOG 1 void setresponse(char rece[], char send[]) { char str1[] = "request"; char str2[] == "client"; char dest1[] = "response"; char dest2[] = "server"; int i1 = 0, i2 = 0; int j1 = 0, j2 = 0; int flag_send = 0; int flag_dest1 = 0, flag_dest2 = 0; //match the str request while(rece[i1] != '\0') { if(rece[i1] == str1[j1]) { i1++; j1++; if(str1[j1] == '\0') { break; } } else { i1++; j1 = 0; } } //match the str client while(rece[i2] != '\0') { if(rece[i2] == str2[j1]) { i2++; j2++; if(str2[j2] == '\0') { break; } } else { i2++; j2 = 0; } } //start to conncet the str before response while(flag_send < (i1-j1)) { send[flag_send] = rece[flag_send]; flag_send++; } // start to connect the str response while(dest1[flag_dest1] != '\0') { send[flag_send] = dest1[flag_dest1]; flag_dest1++; flag_send++; } //start to connect the str between response and server while(i1 < (i2-j2)) { send[flag_send] = rece[i1+1]; i1++; flag_send++; } // start to connect the str server while(dest2[flag_dest2] != '\0') { send[flag_send] = dest2[flag_dest2]; flag_dest2++; flag_send++; } //start to connect the str after server while(rece[i2] != '\0') { send[flag_send] = rece[i2]; flag_send++; i2++; } send[flag_send] = '\0'; } int main() { char recebuf[MAXDATASIZE]; char sendbuff[200]; int listenfd, connectfd; struct sockaddr_in server; //server's address information struct sockaddr_in client; // client's address information socklen_t addrlen; /*create tcp socket*/ if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("create socket failed\n"); exit(1); } //set socket options int opt = SO_REUSEADDR; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr.s_addr = htonl(INADDR_ANY); //bind socket if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1) { printf("bind error\n"); exit(1); } //listen scoket if(listen(listenfd, BACKLOG) == -1) { printf("listen error\n"); exit(1); } addrlen = sizeof(client); while(1) { //conncet the socket if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen)) == -1) { printf("accept error\n"); exit(1); } struct timeval tv; gettimeofday(&tv, NULL); printf("you got a connnetion from client's ip %s, port %d at time %ld.%ld\n", inet_ntoa(client.sin_addr), htons(client.sin_port), tv.tv_sec, tv.tv_usec); int iret = -1; while(1) { iret = recv(connectfd, recebuf, MAXDATASIZE, 0); if(iret > 0) { printf("%s\n", recebuf); } else { close(connectfd); break; } setresponse(recebuf, sendbuff); send(connectfd, recebuf, iret, 0); } } close(listenfd); return 0; }
相关文章推荐
- 基于空间金字塔池化的卷积神经网络物体检测
- 教你快速认识HTTP状态码
- CloseableHttpClient类与HttpClients类解析
- 【通用技术】实时网络响应要求的移动端App的网络超时设定
- 聊聊Socket、TCP/IP、HTTP、FTP及网络编程
- 理解HTTP幂等性
- 认识神经网络
- 学习整理——以太帧、ip帧、udp/tcp帧、http报文结构
- svc 报“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。“的HTTP 错误 404.3 – Not Found
- 网络营销给网站SEO优化带来的帮助有哪些?你知道吗
- Android:ViewPager详解(异步网络加载图片,带图片缓存,并带导航小圆点)
- TCP协议与UDP协议的区别
- 关于网络请求的学习理解
- 使用ASI网络请求后,程序闪退的问题
- TCP/IP详解 卷一 ---------ICMP
- 网络直播监管趋严,主播们最终的出路在哪?
- 2015年中国网络版权保护年度报告:网络音乐侵权案最多
- Linux下四款Web服务器压力测试工具(http_load、webbench、ab、siege)介绍
- 一些常用的网络命令
- swfit中的reduxhttps://github.com/ReSwift/ReSwift