메아리 서버 쪽/클라이언트 (스레드 사용하지 않음)

16186 단어
서버:
 1 #include
 2 #include
 3 #include
 4 void error_handling(char* message);
 5 #define BUF_SIZE 1024
 6 int main()
 7 {
 8     WSADATA wsaData;
 9     SOCKET hServSock, hClntSock;
10     SOCKADDR_IN servAddr, clntAddr;
11     char message[BUF_SIZE];
12     int strLen,i;
13     int clntAdrSize;
14     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
15     {
16         error_handling("WSAStartup() error");
17     }
18     hServSock = socket(PF_INET, SOCK_STREAM, 0);
19     if (hServSock == INVALID_SOCKET)
20     {
21         error_handling("socket() error");
22     }
23 
24     memset(&servAddr, 0, sizeof(servAddr));
25     servAddr.sin_family = AF_INET;
26     servAddr.sin_addr.s_addr =  inet_addr("127.0.0.1");
27     servAddr.sin_port = htons(9190);
28     if (bind(hServSock, (SOCKADDR*)& servAddr, sizeof(servAddr)) == SOCKET_ERROR)
29     {
30         error_handling("bind() error");
31     }
32     if (listen(hServSock, 5) == SOCKET_ERROR)
33     {
34         error_handling("listen() error");
35     }
36     clntAdrSize = sizeof(clntAddr);
37 
38     for(i = 0;i < 5;i++)
39     {
40         hClntSock = accept(hServSock,(SOCKADDR*)&clntAddr,&clntAdrSize);
41         if(hClntSock == -1)
42         {
43             error_handling("accept() error");
44         }
45         else
46         {
47             printf("Connected client %d 
",i+1); 48 } 49 while((strLen = recv(hClntSock,message,BUF_SIZE,0))!=0) 50 { 51 send(hClntSock,message,strLen,0); 52 } 53 closesocket(hClntSock); 54 } 55 closesocket(hServSock); 56 WSACleanup(); 57 return 0; 58 } 59 60 void error_handling(char* message) 61 { 62 fputs(message, stderr); 63 fputc('
', stderr); 64 exit(1); 65 }

클라이언트:
 1 #include
 2 #include
 3 #include
 4 void error_handling(char* message);
 5 #define Buf_Size 1024
 6 int main()
 7 {
 8     WSADATA wsaData;
 9     SOCKET hSocket;
10     SOCKADDR_IN servAdr;
11     char message[1024];
12     int strLen;
13 
14     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
15     {
16         error_handling("WSAStartup() error");
17     }
18     hSocket = socket(PF_INET, SOCK_STREAM, 0);
19     if (hSocket == INVALID_SOCKET)
20     {
21         error_handling("socket() error");
22     }
23 
24     memset(&servAdr, 0, sizeof(servAdr));
25     servAdr.sin_family = AF_INET;
26     servAdr.sin_addr.s_addr = inet_addr("127.0.0.1");
27     servAdr.sin_port = htons(9190);
28     
29     if(connect(hSocket,(SOCKADDR*)&servAdr,sizeof(servAdr)) == SOCKET_ERROR)
30         error_handling("connect() error");
31     else
32         puts("Connected............");
33     while(1)
34     {
35         fputs("Input message(Q to quit): ",stdout);
36         fgets(message,Buf_Size,stdin);
37         if(!strcmp(message,"q
") || !strcmp(message,"Q
")) 38 { 39 break; 40 } 41 send(hSocket,message,strlen(message),0); 42 strLen = recv(hSocket,message,Buf_Size-1,0); 43 message[strLen] = 0; 44 printf("Message from sever: %s",message); 45 } 46 closesocket(hSocket); 47 WSACleanup(); 48 return 0; 49 } 50 51 void error_handling(char* message) 52 { 53 fputs(message, stderr); 54 fputc('
', stderr); 55 exit(1); 56 }

 
다음으로 전송:https://www.cnblogs.com/Jawen/p/11025622.html

좋은 웹페이지 즐겨찾기