Guest User

Untitled

a guest
Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. // A simple example of opening a AF_INET family TCP socket and receiving data over it
  3. // This code will compile in a Win32 Console application in VS if you include:
  4. // Include: wsock32.lib
  5. // (project -> properties -> linker -> input -> additional dependencies
  6. ////////////////////////////////////////////////////////////////////////////////////////
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10.  
  11. int main( int argc, char* argv[] ) {
  12. ## Data Declarations
  13. WSADATA wsa; // WSA Socket Data
  14. SOCKET sock; // The Socket
  15. SOCKADDR_IN sin; // The Socket Address
  16. char szHost[80]; // A buffer for the hostname
  17. char szBuf[250]; // A buffer for the received data
  18.  
  19. ## Init
  20. // Initialize Windows Sockets in the app
  21. WSAStartup( MAKEWORD(2, 0), &wsa ); // Returns error code, 0 = no error
  22.  
  23. // Acquire the local computers host name
  24. gethostname( szHost, sizeof( szHost ) ); // Also returns 0 if successful
  25.  
  26. // Acquire the local computer IP address
  27. struct hostent *sHost = gethostbyname( szHost );
  28. for ( int i = 0; sHost->h_addr_list[i] != 0; i++ ) {
  29. struct in_addr addr;
  30. memcpy( &addr, sHost->h_addr_list[i], sizeof( struct in_addr ) );
  31. printf( "Found local address: %s\n", inet_ntoa( addr ) );
  32. sin.sin_addr.s_addr = inet_addr( inet_ntoa( addr ) );
  33. }
  34.  
  35. // Set address family... AF_INET can be connection-oriented (TCP) or connectionless (UDP)
  36. sin.sin_family = AF_INET;
  37. sin.sin_port = htons( 5000 ); // The port number you want to use for the connection
  38.  
  39. // Tell windows to create a TCP socket
  40. sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  41.  
  42. // Make sure it worked
  43. if ( sock == INVALID_SOCKET ) {
  44. printf( "Socket failed to create.\n" );
  45. }
  46.  
  47. ## Bind
  48. // Bind the socket to the address
  49. if ( bind( sock, (struct sockaddr *)&sin, sizeof(SOCKADDR_IN) ) < 0 ) {
  50. printf( "Bind failed...\n" );
  51. }
  52.  
  53. ## Listen
  54. // Now listen for incoming connections on the socket
  55. // The second parameter is how many connections this socket will listen for and accept
  56. // We set it to 1 here to only allow one incoming connection request
  57. if ( listen( sock, 1 ) < 0 ) {
  58. printf( "Listen failed...\n" );
  59. }
  60.  
  61. printf( "Socket init... OK\nWaiting for connection... " );
  62.  
  63. ## Accept -- Blocks
  64. // This accepts the incoming connection, and will block indefinitely waiting for it
  65. sock = accept( sock, NULL, NULL );
  66.  
  67. ## Receive data
  68. printf( "Connection established!\n" );
  69.  
  70. memset( szBuf, 0, sizeof(szBuf) );
  71. // Receive data from the connection
  72. if ( recv( sock, szBuf, sizeof(szBuf), 0 ) >= 0 ) {
  73. // Print the data to the screen
  74. printf( "The server received:\n\n%s\n\n", szBuf );
  75. } else {
  76. printf( "Recv failed with Socket Error: %i\n", WSAGetLastError() );
  77. }
  78.  
  79. // Make the user press any key to exit
  80. system("PAUSE");
  81. return 0;
  82. }
Add Comment
Please, Sign In to add comment