//main.cpp 1 #include "j_socket.h" 2 #include <stdio.h> 3 #include <pthread.h> 4 static int port=21; 5 j_server* ser; 6 void* main_listen( void* args) 7 { 8 ser=new j_server(port); 9 ser->j_listen(); 10 } 11 int main() 12 { 13 static pthread_t listen_p; 14 int ret; 15 ret= pthread_create( &listen_p, NULL, main_listen, NULL ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数 16 if( ret != 0 ) //创建线程成功返回0 17 { 18 printf("pthread_create error:error_code=%d\n",ret ); 19 } 20 static j_client* cli; 21 cli =new j_client("127.0.0.1",port); 22 cli->j_send(); 23 return 0; 24 }
//j_socket.h 1 #ifndef J_SOCKET_H_INCLUDED 2 #define J_SOCKET_H_INCLUDED 3 #include <stdio.h> 4 #include <errno.h> 5 #include <winsock2.h> 6 #include <pthread.h> 7 8 #define INIT_ERROR 1 9 #define BIND_ERROR 2 10 #define LISTEN_ERROR 3 11 struct rec_args{ 12 int * l; 13 SOCKET* s; 14 }; 15 void* receive( void* args) 16 { 17 char recvBuf[512]; 18 rec_args *a=(rec_args*)args; 19 while(true) 20 { 21 recv(*(a->s),recvBuf,512,0); 22 printf("receive message: %s\n",recvBuf); 23 printf("111111\n"); 24 if(strcmp(recvBuf,"end")==0){break;} 25 26 } 27 closesocket(*(a->s)); 28 (*(a->l))--; 29 printf("22222\n"); 30 return NULL; 31 } 32 class j_server 33 { 34 private: 35 SOCKET Server_Sock; 36 SOCKADDR_IN server_addr; 37 SOCKADDR_IN client_addr; 38 39 int max_num; 40 int link_num=0; 41 int error_num; 42 43 public: 44 j_server(int port) 45 { 46 WSADATA wsa; 47 /*初始化socket资源*/ 48 if (WSAStartup(MAKEWORD(1,1),&wsa) != 0) 49 { 50 error_num=INIT_ERROR; 51 return; 52 } 53 54 if((Server_Sock = socket(AF_INET, SOCK_STREAM, 0))==-1) 55 { 56 error_num=INIT_ERROR; 57 return; 58 } 59 ZeroMemory((char *)&server_addr,sizeof(server_addr)); 60 server_addr.sin_family = AF_INET; 61 server_addr.sin_port = htons(port); /*本地监听端口*/ 62 server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*有IP*/ 63 64 if(bind(Server_Sock,(struct sockaddr *)&server_addr,sizeof(server_addr))==-1) 65 { 66 error_num=BIND_ERROR; 67 return; 68 } 69 if(listen(Server_Sock,5)==-1) //其中第二个参数代表能够接收的最多的连接数 70 { 71 error_num=LISTEN_ERROR; 72 return; 73 } 74 max_num=20; 75 76 } 77 int j_listen() 78 { 79 int len=sizeof(SOCKADDR); 80 while(true) 81 { 82 if(link_num>max_num) 83 { 84 Sleep(100); 85 continue; 86 } 87 SOCKET Command_Sock = accept(Server_Sock, (SOCKADDR*)&client_addr,&len); 88 if(Command_Sock == INVALID_SOCKET) 89 { 90 printf("accept error !"); 91 closesocket(Command_Sock); 92 } 93 link_num++; 94 pthread_t listen_p; 95 int ret; 96 rec_args a; 97 a.l=&link_num; 98 a.s=&Command_Sock; 99 ret= pthread_create( &listen_p, NULL, receive,(void *)&a ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数 100 } 101 return 0; 102 } 103 104 }; 105 class j_client 106 { 107 private: 108 SOCKET Client_Sock; 109 SOCKADDR_IN server_addr; 110 char sendBuf[512]; 111 int error_num; 112 public: 113 114 j_client(char * server_addr_str,int port) 115 { 116 WSADATA wsa; 117 /*初始化socket资源*/ 118 if (WSAStartup(MAKEWORD(1,1),&wsa) != 0) 119 { 120 error_num=INIT_ERROR; 121 return; //代表失败 122 } 123 if((Client_Sock = socket(AF_INET, SOCK_STREAM, 0))==-1) 124 { 125 error_num=INIT_ERROR; 126 return; //代表失败 127 } 128 server_addr.sin_addr.S_un.S_addr=inet_addr(server_addr_str); 129 server_addr.sin_family=AF_INET; 130 server_addr.sin_port=htons(port); 131 if(connect(Client_Sock,(SOCKADDR*)&server_addr,sizeof(SOCKADDR)) ==-1) 132 { 133 printf("connect error !"); 134 //printf( "%d ", WSAGetLastError()); 135 } 136 } 137 int j_send() 138 { 139 while(true) 140 { 141 printf("input message: "); 142 scanf("%s",sendBuf); 143 if(send(Client_Sock,sendBuf,strlen(sendBuf)+1,0)!=(strlen(sendBuf)+1)) 144 { 145 printf("send error !"); 146 //printf( "%d ", WSAGetLastError()); 147 } 148 } 149 return 0; 150 } 151 int j_close() 152 { 153 closesocket(Client_Sock); 154 WSACleanup(); 155 } 156 }; 157 158 159 160 #endif // J_SOCKET_H_INCLUDED
时间: 2024-10-27 07:12:31