Advertisement
Guest User

bind try #1

a guest
May 6th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1.  
  2. #ifndef UNICODE
  3. #define UNICODE
  4. #endif
  5.  
  6. #define WIN32_LEAN_AND_MEAN
  7.  
  8. #include <winsock2.h>
  9. #include <Ws2tcpip.h>
  10. #include <stdio.h>
  11.  
  12. // Link with ws2_32.lib
  13. #pragma comment(lib, "Ws2_32.lib")
  14.  
  15. int main()
  16. {
  17.  
  18.     // Declare some variables
  19.     WSADATA wsaData;
  20.  
  21.     int iResult = 0;            // used to return function results
  22.  
  23.     // the listening socket to be created
  24.     SOCKET ListenSocket = INVALID_SOCKET;
  25.  
  26.     // The socket address to be passed to bind
  27.     sockaddr_in service;
  28.  
  29.     //----------------------
  30.     // Initialize Winsock
  31.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  32.     if (iResult != NO_ERROR) {
  33.         wprintf(L"Error at WSAStartup()\n");
  34.         return 1;
  35.     }
  36.     //----------------------
  37.     // Create a SOCKET for listening for
  38.     // incoming connection requests
  39.     ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  40.     if (ListenSocket == INVALID_SOCKET) {
  41.         wprintf(L"socket function failed with error: %u\n", WSAGetLastError());
  42.         WSACleanup();
  43.         return 1;
  44.     }
  45.     //----------------------
  46.     // The sockaddr_in structure specifies the address family,
  47.     // IP address, and port for the socket that is being bound.
  48.     service.sin_family = AF_INET;
  49.     service.sin_addr.s_addr = inet_addr("127.0.0.1");
  50.     service.sin_port = htons(27015);
  51.  
  52.     //----------------------
  53.     // Bind the socket.
  54.     iResult = bind(ListenSocket, (SOCKADDR *) &service, sizeof (service));
  55.     if (iResult == SOCKET_ERROR) {
  56.         wprintf(L"bind failed with error %u\n", WSAGetLastError());
  57.         closesocket(ListenSocket);
  58.         WSACleanup();
  59.         return 1;
  60.     }
  61.     else
  62.         wprintf(L"bind returned success\n");
  63.  
  64.     WSACleanup();
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement