客户端
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <string.h> #include<arpa/inet.h> #include <error.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <pthread.h> void* recvsocket(void* arg)//接受来着客户端数据的线程 { int st = *(int*)arg; char s[1024]; while (1) { memset(s, 0, sizeof(s)); int rc = recv(st, s, sizeof(s), 0); if (rc <= 0)//代表socket被关闭(0)或者出错(-1) { break; } printf("client receive:%s\n", s); } return NULL; } void* sendsocket(void* arg)//向服务端socket发送数据的线程 { int st = *(int*)arg; char s[1024]; while (1) { memset(s, 0, sizeof(s)); scanf("%s", s); int sc = send(st, s, strlen(s), 0); } } //执行 ./ClientLinux.out 127.0.0.1 8080 int main(int arg, char* args[]) { if (arg < 3) { printf("arg<3\n"); return - 1; } int port = atoi(args[2]); //第一步:初始化一个socket实例 int st = socket(AF_INET, SOCK_STREAM, 0); //第二步:定义一个IP地址结构并设置值 struct sockaddr_in addr; //内存初始化,将addr变量指向的内存签n个字节用0进行初始化填充 memset(&addr, 0, sizeof(addr)); //设置采用的协议为TCP/IP协议 addr.sin_family = AF_INET; //设置端口号 addr.sin_port = htons(port); //设置IP地址 addr.sin_addr.s_addr = inet_addr(args[1]); //第三步:开始连接服务端 if (connect(st, (struct sockaddr*) & addr, sizeof(addr)) == -1) { printf("connect fail %s\n", strerror(errno)); return EXIT_FAILURE; } //第四步:初始化要发送的信息并且通过send函数发送数据 pthread_t thrd1, thrd2;//定义一个线程 pthread_create(&thrd1, NULL, recvsocket, &st); pthread_create(&thrd2, NULL, sendsocket, &st); pthread_join(thrd1,NULL); pthread_join(thrd2,NULL); close(st); getchar(); return EXIT_SUCCESS; }
服务端
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <string.h> #include<arpa/inet.h> #include <error.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <pthread.h> void* recvsocket(void* arg)//接受来着客户端数据的线程 { int st = *(int*)arg; char s[1024]; while (1) { memset(s, 0, sizeof(s)); int rc = recv(st, s, sizeof(s), 0); if (rc <= 0)//代表socket被关闭(0)或者出错(-1) { printf("sercver recv fail:%d\n", rc); break; } printf("server receive:%s\n", s); } return NULL; } void* sendsocket(void* arg)//向客户端socket发送数据的线程 { int st = *(int*)arg; char s[1024]; while (1) { memset(s, 0, sizeof(s)); scanf("%s", s); int sc = send(st, s, strlen(s), 0); } } //执行命令 ./ServerLinux.out 8080 int main(int arg, char* args[]) { if (arg < 2) { return -1; } int port = atoi(args[1]); int st = socket(AF_INET, SOCK_STREAM, 0); //setsockopt 设置socket的一个属性,让地址可以重用。 int on = 0; if (setsockopt(st, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { printf("setsockopt failed:%s\n", strerror(errno)); return EXIT_FAILURE; } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); //INADDR_ANY表示这个服务器上的所有Ip地址。一台服务器可以有多个ip地址。将socket绑定到这个机器的所有ip地址上 addr.sin_addr.s_addr = htonl(INADDR_ANY); //将ip地址与server程序绑定 if (bind(st, (struct sockaddr*) & addr, sizeof(addr)) == -1) { printf("bind fail %s\n", strerror(errno)); return EXIT_FAILURE; } //server开始监听。 20代表同时有多少个连接过来(20并发) if (listen(st, 20) == -1) { printf("listen fail %s\n", strerror(errno)); return EXIT_FAILURE; } //char s[1024]; int client_st = 0;//客户端socket struct sockaddr_in client_addr;//客户端IP pthread_t thrd1, thrd2;//定义一个线程 while (1) { memset(&client_addr, 0, sizeof(client_addr)); socklen_t len = sizeof(client_addr); //accept会阻塞,直到有客户端连接过来。accept返回客户端的描述符 client_st = accept(st, (struct sockaddr*) & client_addr, &len); if (client_st == -1) { printf("accept fail %s\n", strerror(errno)); return EXIT_FAILURE; } //打印客户端的ip地址 printf("accept ip : %s\n", inet_ntoa(client_addr.sin_addr)); pthread_create(&thrd1, NULL, recvsocket, &client_st); pthread_create(&thrd2, NULL, sendsocket, &client_st); } close(st); getchar(); }
原文地址:https://www.cnblogs.com/caoruipeng/p/11784106.html
时间: 2024-09-30 12:05:07