之前学习了基于TCP协议下实现服务器与一个客户端的通信,服务器与多个客户端之间的通信,
以及客户端之间的互相通信。
下面就是我写的利用TCP和多线程技术实现客户端之间互相通信的代码:
服务器端:Tc
#include <; #include <sy; #include <sy; #include <sy; #include <netine; #include <arpa; #include <; #include <; #include <; #define PORT 8888 struct info { int Tofd; char buf[100]; }; void *MyReceive(void *arg) { struct info RecvBuf = {0}; int ret; pthread_detach(pthread_self()); while (1) { ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0); //接收消息,从fd里面接收 if (-1 == ret) { perror("recv"); exit(1); } if (!strcm;bye")) { //close(*(int *)arg); break; } // printf("Recv From Client : %s\n", buf); ret = send, &RecvBuf, sizeof(RecvBuf),0); if (-1 == ret) { perror("send"); exit(1); } memset(&RecvBuf, 0, sizeof(RecvBuf)); } } int main() { int sockfd, ret,length, fd[100] = {0},i = 0; char buf[100] = {0}; struct sockaddr_in server_addr; //用于存放服务器本身的信息,包括自己的port和ip struct sockaddr_in client_addr; //接受客户端连接的时候, 用于存放客户端信息 pthread_t tid[100] = {0}; printf("Start Server!\n"); sockfd = socket(PF_INET, SOCK_STREAM, 0); //创建socket,处理客户端的连接,不用于发送信息 if (-1 == sockfd) { perror("socket"); exit(1); } int opt = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); memset(&server_addr, 0, sizeof(server_addr)); = PF_INET; //地址族 = PORT; //指定socket的端口号 = inet_addr("192.168.0.128"); //本机ip ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); //上述信息绑定到socket if (-1 == ret) { perror("bind"); exit(1); } printf("Listening...\n"); ret = listen(sockfd, 5); //监听,是否有客户端发起连接 if(-1 == ret) { perror("listen"); exit(1); } while(1) { length = sizeof(client_addr); fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length);//接受客户端的连接,返回值用于发送信息 if (-1 == fd[i]) { perror("accept"); exit(1); } printf("Accept %d , Port %d\n", fd[i], clien); ret = pthread_create(&tid[i], NULL, MyReceive, (void *)&fd[i]); if (0 != ret) { perror("pthread_create"); exit(1); } i++; } close(sockfd); return 0;
客户端:Tc
#include <; #include <sy; #include <sy; #include <; #include <; #include <netine; #include <arpa; #include <; #define PORT 8888 struct info { int Tofd; char buf[100]; }; pthread_t tid[2] = {0}; void *Send(void *arg) { struct info SendBuf = {0}; int ret; while (1) { scanf("%s %d", SendBuf.buf, &SendBuf.Tofd); ret = send(*(int *)arg, &SendBuf, sizeof(SendBuf), 0); //发送消息 if (-1 == ret) { perror("send"); exit(1); } if (!strcm, "bye")) { close(*(int *)arg); pthread_cancel(tid[1]); break; } memset(&SendBuf, 0, sizeof(SendBuf)); } } void *Recv(void *arg) { struct info RecvBuf = {0}; int ret,oldtype; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); while(1) { ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0); if (-1 == ret) { perror("recv"); exit(1); } printf("\t\t\t%s\n", RecvBuf.buf); memset(&RecvBuf, 0, sizeof(RecvBuf)); } } int main() { int sockfd, ret; struct sockaddr_in server_addr; //向server_addr发起连接 sockfd = socket(PF_INET, SOCK_STREAM, 0); //创建socket,既用于连接,又用于发送消息 if (-1 == sockfd) { perror("socket"); exit(1); } memset(&server_addr, 0, sizeof(server_addr)); //都是服务器的消息 = PF_INET; = PORT; = inet_addr("192.168.0.128"); printf("Start Connecting...\n"); ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); //发起连接 if (-1 == ret) { perror("connect"); exit(1); } ret = pthread_create(&tid[0], NULL, Send, (void *)&sockfd); if (0 != ret) { perror("pthread_create1"); exit(1); } ret = pthread_create(&tid[1], NULL, Recv, (void *)&sockfd); if (0 != ret) { perror("pthread_create2"); exit(1); } pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); close(sockfd); return 0; }