一个最简单的聊天程序2

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <winsock2.h>
  5. #pragma comment(lib,"Ws2_32.lib")
  6. SOCKET server;
  7. SOCKET client;
  8. WSADATA wsaData;
  9. int wsaReturn;
  10. sockaddr_in local;
  11. sockaddr_in from;
  12. int fromlen = sizeof(from);
  13. int sendlen = 0;
  14. static char chat[256] = {0};
  15. char recevie[256] = {0};
  16. static char infor[50] = {0};
  17. char welcome[] = "欢迎登陆到Etionas的主机";
  18. bool cansend = false;
  19. int MyServer(void)
  20. {
  21.     wsaReturn = WSAStartup(0x101,&wsaData);
  22.     if(wsaReturn != 0)
  23.     {
  24.         printf("未知错误!/n");
  25.         WSACleanup();
  26.         return 0;
  27.     }
  28.     local.sin_family = AF_INET;
  29.     local.sin_addr.s_addr = ADDR_ANY;
  30.     local.sin_port = htons((u_short)4567);
  31.     server = socket(AF_INET,SOCK_STREAM,0);
  32.     if(client == INVALID_SOCKET)
  33.     {
  34.         printf("创建socket失败!/n");
  35.         return 0;
  36.     }
  37.     if(bind(server,(sockaddr *)&local,sizeof(local)) != 0)
  38.     {
  39.         printf("绑定失败!/n");
  40.         return 0;
  41.     }
  42.     if(listen(server,5) != 0)
  43.     {
  44.         printf("监听失败!/n");
  45.         return 0;
  46.     }
  47.     while(1)
  48.     {
  49.         printf("等待客户的连接…………/n");
  50.         client = accept(server,(sockaddr *)&from,&fromlen);
  51.         if(client == INVALID_SOCKET)
  52.         {
  53.             printf("accept失败!/n");
  54.             return 0;
  55.         }
  56.         else
  57.         {
  58.             strcpy(infor,inet_ntoa(from.sin_addr));
  59.             printf("接受来自%s的连接/n",infor);
  60.             printf("发送欢迎消息……/n");
  61.             if((sendlen = send(client,welcome,sizeof(welcome),0)) == -1)
  62.             {
  63.                 printf("发送欢迎消息失败!/n");
  64.                 return 0;
  65.             }
  66.             else
  67.             {
  68.                 printf("要发送的数据长度为%d/n",sizeof(welcome));
  69.                 printf("实际发送的数据长度为%d/n",sendlen);
  70.             }
  71.             while(1)
  72.             {
  73.                 if(!cansend)
  74.                 {
  75.                     printf("………………………………接收消息…………………………/n");
  76.                     recv(client,recevie,sizeof(recevie),0);
  77.                     printf("%s/n",recevie);
  78.                     cansend = true;
  79.                     printf("………………………………接收完成…………………………/n");
  80.                 }
  81.                 else
  82.                 {
  83.                     printf("请输入你要说的话:");
  84.                     scanf("%s/0",chat);
  85.                     send(client,chat,strlen(chat)+1,0);
  86.                     cansend = false;
  87.                 }
  88.             }
  89.         }
  90.         closesocket(client);
  91.     }
  92.     closesocket(server);
  93.     WSACleanup();
  94.     return 0;
  95. }
  96. int main(void)
  97. {
  98.     int ok;
  99.     ok = MyServer();
  100.     if(!ok)
  101.     {
  102.         printf("启动服务器失败!/n");
  103.     }
  104.     return 0;
  105. }

//服务器端代码:

 

你可能感兴趣的:(一个最简单的聊天程序2)