鱼C论坛

 找回密码
 立即注册
查看: 1612|回复: 1

[已解决]Winsock编程怎么在服务器上配置?

[复制链接]
发表于 2018-8-9 13:48:15 | 显示全部楼层 |阅读模式
20鱼币
在学习Winsock编程,突然想到一个问题,买的服务器通常是做网站用的,怎么在服务器上配置Winsock服务端呢,不会配置服务端在本机上两个端通信也没用呀?
最佳答案
2018-8-9 13:48:16
校对了一些视频,有一些不一样的地方,但大致结构相同
参考一下。这是给 Winsock Client Server 的
  1. #undef UNICODE

  2. #define WIN32_LEAN_AND_MEAN

  3. // #include <windows.h>   // 这个在 WIN32_LEAN_AND_MEAN 里有被定义了
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>      // ?
  6. #include <stdlib.h>
  7. #include <stdio.h>

  8. // Need to link with Ws2_32.lib
  9. #pragma comment (lib, "Ws2_32.lib")
  10. // #pragma comment (lib, "Mswsock.lib")

  11. #define DEFAULT_BUFLEN 512
  12. #define DEFAULT_PORT "27015"

  13. int __cdecl main(void)
  14. {
  15.     WSADATA wsaData;
  16.     int iResult;

  17.     SOCKET ListenSocket = INVALID_SOCKET;
  18.     SOCKET ClientSocket = INVALID_SOCKET;

  19.     struct addrinfo *result = NULL;
  20.     struct addrinfo hints;

  21.     int iSendResult;
  22.     char recvbuf[DEFAULT_BUFLEN];
  23.     int recvbuflen = DEFAULT_BUFLEN;
  24.    
  25.     // Initialize Winsock
  26.     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  27.     if (iResult != 0) {
  28.         printf("WSAStartup failed with error: %d\n", iResult);
  29.         return 1;
  30.     }

  31.     ZeroMemory(&hints, sizeof(hints));
  32.     hints.ai_family = AF_INET;
  33.     hints.ai_socktype = SOCK_STREAM;
  34.     hints.ai_protocol = IPPROTO_TCP;
  35.     hints.ai_flags = AI_PASSIVE;

  36.     // Resolve the server address and port
  37.     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  38.     if ( iResult != 0 ) {
  39.         printf("getaddrinfo failed with error: %d\n", iResult);
  40.         WSACleanup();
  41.         return 1;
  42.     }

  43.     // Create a SOCKET for connecting to server
  44.     ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  45.     if (ListenSocket == INVALID_SOCKET) {
  46.         printf("socket failed with error: %ld\n", WSAGetLastError());
  47.         freeaddrinfo(result);
  48.         WSACleanup();
  49.         return 1;
  50.     }

  51.     // Setup the TCP listening socket
  52.     iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  53.     if (iResult == SOCKET_ERROR) {
  54.         printf("bind failed with error: %d\n", WSAGetLastError());
  55.         freeaddrinfo(result);
  56.         closesocket(ListenSocket);
  57.         WSACleanup();
  58.         return 1;
  59.     }

  60.     freeaddrinfo(result);

  61.     iResult = listen(ListenSocket, SOMAXCONN);
  62.     if (iResult == SOCKET_ERROR) {
  63.         printf("listen failed with error: %d\n", WSAGetLastError());
  64.         closesocket(ListenSocket);
  65.         WSACleanup();
  66.         return 1;
  67.     }

  68.     // Accept a client socket
  69.     ClientSocket = accept(ListenSocket, NULL, NULL);
  70.     if (ClientSocket == INVALID_SOCKET) {
  71.         printf("accept failed with error: %d\n", WSAGetLastError());
  72.         closesocket(ListenSocket);
  73.         WSACleanup();
  74.         return 1;
  75.     }

  76.     // No longer need server socket
  77.     closesocket(ListenSocket);

  78.     // Receive until the peer shuts down the connection
  79.     do {

  80.         iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  81.         if (iResult > 0) {
  82.             printf("Bytes received: %d\n", iResult);

  83.         // Echo the buffer back to the sender
  84.             iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
  85.             if (iSendResult == SOCKET_ERROR) {
  86.                 printf("send failed with error: %d\n", WSAGetLastError());
  87.                 closesocket(ClientSocket);
  88.                 WSACleanup();
  89.                 return 1;
  90.             }
  91.             printf("Bytes sent: %d\n", iSendResult);
  92.         }
  93.         else if (iResult == 0)
  94.             printf("Connection closing...\n");
  95.         else  {
  96.             printf("recv failed with error: %d\n", WSAGetLastError());
  97.             closesocket(ClientSocket);
  98.             WSACleanup();
  99.             return 1;
  100.         }

  101.     } while (iResult > 0);

  102.     // shutdown the connection since we're done
  103.     iResult = shutdown(ClientSocket, SD_SEND);
  104.     if (iResult == SOCKET_ERROR) {
  105.         printf("shutdown failed with error: %d\n", WSAGetLastError());
  106.         closesocket(ClientSocket);
  107.         WSACleanup();
  108.         return 1;
  109.     }

  110.     // cleanup
  111.     closesocket(ClientSocket);
  112.     WSACleanup();

  113.     return 0;
  114. }
复制代码

最佳答案

查看完整内容

校对了一些视频,有一些不一样的地方,但大致结构相同 参考一下。这是给 Winsock Client Server 的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-9 13:48:16 | 显示全部楼层    本楼为最佳答案   
校对了一些视频,有一些不一样的地方,但大致结构相同
参考一下。这是给 Winsock Client Server 的
  1. #undef UNICODE

  2. #define WIN32_LEAN_AND_MEAN

  3. // #include <windows.h>   // 这个在 WIN32_LEAN_AND_MEAN 里有被定义了
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>      // ?
  6. #include <stdlib.h>
  7. #include <stdio.h>

  8. // Need to link with Ws2_32.lib
  9. #pragma comment (lib, "Ws2_32.lib")
  10. // #pragma comment (lib, "Mswsock.lib")

  11. #define DEFAULT_BUFLEN 512
  12. #define DEFAULT_PORT "27015"

  13. int __cdecl main(void)
  14. {
  15.     WSADATA wsaData;
  16.     int iResult;

  17.     SOCKET ListenSocket = INVALID_SOCKET;
  18.     SOCKET ClientSocket = INVALID_SOCKET;

  19.     struct addrinfo *result = NULL;
  20.     struct addrinfo hints;

  21.     int iSendResult;
  22.     char recvbuf[DEFAULT_BUFLEN];
  23.     int recvbuflen = DEFAULT_BUFLEN;
  24.    
  25.     // Initialize Winsock
  26.     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  27.     if (iResult != 0) {
  28.         printf("WSAStartup failed with error: %d\n", iResult);
  29.         return 1;
  30.     }

  31.     ZeroMemory(&hints, sizeof(hints));
  32.     hints.ai_family = AF_INET;
  33.     hints.ai_socktype = SOCK_STREAM;
  34.     hints.ai_protocol = IPPROTO_TCP;
  35.     hints.ai_flags = AI_PASSIVE;

  36.     // Resolve the server address and port
  37.     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  38.     if ( iResult != 0 ) {
  39.         printf("getaddrinfo failed with error: %d\n", iResult);
  40.         WSACleanup();
  41.         return 1;
  42.     }

  43.     // Create a SOCKET for connecting to server
  44.     ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  45.     if (ListenSocket == INVALID_SOCKET) {
  46.         printf("socket failed with error: %ld\n", WSAGetLastError());
  47.         freeaddrinfo(result);
  48.         WSACleanup();
  49.         return 1;
  50.     }

  51.     // Setup the TCP listening socket
  52.     iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  53.     if (iResult == SOCKET_ERROR) {
  54.         printf("bind failed with error: %d\n", WSAGetLastError());
  55.         freeaddrinfo(result);
  56.         closesocket(ListenSocket);
  57.         WSACleanup();
  58.         return 1;
  59.     }

  60.     freeaddrinfo(result);

  61.     iResult = listen(ListenSocket, SOMAXCONN);
  62.     if (iResult == SOCKET_ERROR) {
  63.         printf("listen failed with error: %d\n", WSAGetLastError());
  64.         closesocket(ListenSocket);
  65.         WSACleanup();
  66.         return 1;
  67.     }

  68.     // Accept a client socket
  69.     ClientSocket = accept(ListenSocket, NULL, NULL);
  70.     if (ClientSocket == INVALID_SOCKET) {
  71.         printf("accept failed with error: %d\n", WSAGetLastError());
  72.         closesocket(ListenSocket);
  73.         WSACleanup();
  74.         return 1;
  75.     }

  76.     // No longer need server socket
  77.     closesocket(ListenSocket);

  78.     // Receive until the peer shuts down the connection
  79.     do {

  80.         iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  81.         if (iResult > 0) {
  82.             printf("Bytes received: %d\n", iResult);

  83.         // Echo the buffer back to the sender
  84.             iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
  85.             if (iSendResult == SOCKET_ERROR) {
  86.                 printf("send failed with error: %d\n", WSAGetLastError());
  87.                 closesocket(ClientSocket);
  88.                 WSACleanup();
  89.                 return 1;
  90.             }
  91.             printf("Bytes sent: %d\n", iSendResult);
  92.         }
  93.         else if (iResult == 0)
  94.             printf("Connection closing...\n");
  95.         else  {
  96.             printf("recv failed with error: %d\n", WSAGetLastError());
  97.             closesocket(ClientSocket);
  98.             WSACleanup();
  99.             return 1;
  100.         }

  101.     } while (iResult > 0);

  102.     // shutdown the connection since we're done
  103.     iResult = shutdown(ClientSocket, SD_SEND);
  104.     if (iResult == SOCKET_ERROR) {
  105.         printf("shutdown failed with error: %d\n", WSAGetLastError());
  106.         closesocket(ClientSocket);
  107.         WSACleanup();
  108.         return 1;
  109.     }

  110.     // cleanup
  111.     closesocket(ClientSocket);
  112.     WSACleanup();

  113.     return 0;
  114. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 19:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表