Linux下C语言实现TCP通信
来源:互联网 发布:徐水网络互动平台 编辑:程序博客网 时间:2024/05/03 11:42
TCP编程:
写一个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 512int 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 1void 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;}
0 0
- Linux下C语言实现TCP通信
- linux下c语言tcp通信
- C语言实现TCP通信
- C语言实现DOS下的TCP/IP通信
- c 语言下 TCP/UDP 通信
- C语言socket编程----实现TCP通信
- c语言实现TCP的socket通信
- Linux下C语言实现的Socket通信
- C语言实现windows和linux下socket通信
- Linux下C语言实现简单的udp通信
- linux下C++实现TCP通信
- linux下C语言 socket tcp编程
- Linux下C语言TCP编程02
- Linux下C语言TCP编程01
- linux下C语言UDP通信
- C语言实现服务端和客户端进行TCP通信实例
- C语言实现服务端和客户端进行TCP通信实例
- Linux C语言实现的Socket通信
- 25-ZigZag Conversion
- Java中传值还是传引用
- DB2查询行号
- windows Eclipse运行mapreduce配置说明
- android的Environment类
- Linux下C语言实现TCP通信
- Docker--使用Dockerfile创建镜像--RHEL7.2
- MySQL存储过程实现动态执行SQL
- vncserver设置开机自动启动并取消开机密码
- iOS UIWebView的基本用法
- jqueryeasyui tabs组件里面的内容不显示滚动条 解决方案
- Unity3D学习笔记(四)天空、光晕和迷雾
- 【JAVA】对象的序列化和反序列化
- NoSQL之Redis(五)--- Redis测试以及其与Spring的集成