一个简单的socket服务端和客户端程序:客户端发送数据给服务端,服务端接收到数据后再给客户端发送数据
View Code
1 /***********server***********/ 2 3 #include <netinet/in.h> // for sockaddr_in 4 #include <sys/types.h> // for socket 5 #include <sys/socket.h> // for socket 6 #include <stdio.h> // for printf 7 #include <stdlib.h> // for exit 8 #include <string.h> // for bzero 9 #include <unistd.h> 10 11 #define PORT 6666 12 #define BUFFER_SIZE 1024 13 14 int main(int argc, char *argv[]) 15 { 16 int recv_bytes = 0, send_bytes = 0; 17 char recv_buf[BUFFER_SIZE] = {0}; 18 char send_buf[BUFFER_SIZE] = {"hello"}; 19 int server_socket = 0; 20 //定义socket地址结构client_addr 21 struct sockaddr_in client_addr; 22 socklen_t length = sizeof(client_addr); 23 //设置socket地址结构client_addr 24 struct sockaddr_in server_addr; 25 bzero(&server_addr, sizeof(server_addr)); 26 server_addr.sin_family = AF_INET; 27 server_addr.sin_addr.s_addr = htons(INADDR_ANY); 28 server_addr.sin_port = htons(PORT); 29 //新建一个socket 30 if((server_socket = socket(PF_INET, SOCK_STREAM, 0)) < 0) 31 { 32 printf("server create socket failed\n"); 33 return -1; 34 } 35 36 int opt =1; 37 setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); 38 //讲server_addr地址和server_socket接口绑定 39 if(bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr))) 40 { 41 printf("server bind socket failed\n"); 42 return -1; 43 } 44 //设置最大连接数 45 if(listen(server_socket, 10)) 46 { 47 printf("server listen failed\n"); 48 return -1; 49 } 50 51 while(1) 52 { 53 int new_socket = 0; 54 printf("waiting for client connect\n"); 55 //等待客户端连接 56 if((new_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length)) < 0) 57 { 58 printf("server accept socket failed\n"); 59 return -1; 60 } 61 //接收客户端数据,保存到recv_buf 62 if((recv_bytes = recv(new_socket, recv_buf, sizeof(recv_buf), 0)) < 0) 63 { 64 printf("serve recv data failed\n"); 65 return -1; 66 } 67 68 printf("serve recv data from: %s\n", recv_buf); 69 sleep(2);//休眠2s(为了显示直观,测试用) 70 printf("server send data to client......"); 71 sleep(5);//休眠5s(为了显示直观,测试用) 72 //发送数据到客户端 73 if((recv_bytes = send(new_socket, send_buf, sizeof(send_buf), 0)) < 0) 74 { 75 printf("server send data failde\n"); 76 return -1; 77 } 78 79 printf("server send data to client: %s\n", send_buf); 80 close(new_socket);//关闭new_socket 81 } 82 83 close(server_socket);//关闭server_socket 84 85 return 0; 86 }
View Code
1 /******client********/ 2 3 #include <netinet/in.h> // for sockaddr_in 4 #include <sys/types.h> // for socket 5 #include <sys/socket.h> // for socket 6 #include <stdio.h> // for printf 7 #include <stdlib.h> // for exit 8 #include <string.h> // for bzero 9 #include <unistd.h> 10 #include <arpa/inet.h> 11 12 #define PORT 6666 13 #define BUFFER_SIZE 1024 14 15 16 int main(int argc, char *argv[]) 17 { 18 if(argc != 3) 19 { 20 printf("please input the ip and the text\n"); 21 return -1; 22 } 23 int send_bytes = 0, recv_bytes = 0; 24 char buf[BUFFER_SIZE] = {0}; 25 //设置socket的地址结构client_addr 26 int client_socket; 27 struct sockaddr_in client_addr, server_addr; 28 bzero( &client_addr, sizeof(client_addr) ); 29 client_addr.sin_family = AF_INET; 30 client_addr.sin_addr.s_addr = htons(INADDR_ANY); 31 client_addr.sin_port = htons(0); 32 //设置socket的地址结构server_addr 33 bzero( &server_addr, sizeof(server_addr) ); 34 server_addr.sin_family = AF_INET; 35 if(inet_aton(argv[1],&server_addr.sin_addr) == 0)//IP地址格式转换 36 { 37 printf("client server IP error\n"); 38 return -1; 39 } 40 server_addr.sin_port = htons(PORT); 41 //新建一个socket 42 if( (client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) 43 { 44 printf("client create socket failed\n"); 45 return -1; 46 } 47 //将client_addr的地址和client_socket绑定 48 if(bind(client_socket, (struct sockaddr*)&client_addr, sizeof(client_addr))) 49 { 50 printf("client bind socket failed\n"); 51 return -1; 52 } 53 printf("connect to server.........\n"); 54 //通过socket接口client_socket链接地址server_addr 55 if(connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) 56 { 57 printf("client can not connect to server: %s", argv[1]); 58 return -1; 59 } 60 sleep(5);//休眠5s(主要为了是结果更直观) 61 //通过client_socket接口,发送数据到服务器 62 if((send_bytes = send(client_socket, argv[2], sizeof(argv[2]), 0)) < 0) 63 { 64 printf("client send data error\n"); 65 return -1; 66 } 67 printf("client send data to server: %s\n", argv[2]); 68 //通过client_socket接口接收数据到buf 69 if((recv_bytes = recv(client_socket, buf, sizeof(buf), 0)) < 0) 70 { 71 printf("client recv data from server error\n"); 72 } 73 74 printf("client recv data from server: %s\n", buf); 75 76 close(client_socket);//关闭client_socket 77 78 return 0; 79 80 }