Getting Started with Winsock
Complete Server Code
The following is the complete source code for the TCP/IP Server application.
Server Source Code
1 #include <stdio.h> 2 #include "winsock2.h" 3 4 void main() { 5 6 // Initialize Winsock. 7 WSADATA wsaData; 8 int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); 9 if ( iResult != NO_ERROR ) 10 printf("Error at WSAStartup()\n"); 11 12 // Create a socket. 13 SOCKET m_socket; 14 m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); 15 16 if ( m_socket == INVALID_SOCKET ) { 17 printf( "Error at socket(): %ld\n", WSAGetLastError() ); 18 WSACleanup(); 19 return; 20 } 21 22 // Bind the socket. 23 sockaddr_in service; 24 25 service.sin_family = AF_INET; 26 service.sin_addr.s_addr = inet_addr( "127.0.0.1" ); 27 service.sin_port = htons( 27015 ); 28 29 if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) { 30 printf( "bind() failed.\n" ); 31 closesocket(m_socket); 32 return; 33 } 34 35 // Listen on the socket. 36 if ( listen( m_socket, 1 ) == SOCKET_ERROR ) 37 printf( "Error listening on socket.\n"); 38 39 // Accept connections. 40 SOCKET AcceptSocket; 41 42 printf( "Waiting for a client to connect...\n" ); 43 while (1) { 44 AcceptSocket = SOCKET_ERROR; 45 while ( AcceptSocket == SOCKET_ERROR ) { 46 AcceptSocket = accept( m_socket, NULL, NULL ); 47 } 48 printf( "Client Connected.\n"); 49 m_socket = AcceptSocket; 50 break; 51 } 52 53 // Send and receive data. 54 int bytesSent; 55 int bytesRecv = SOCKET_ERROR; 56 char sendbuf[32] = "Server: Sending Data."; 57 char recvbuf[32] = ""; 58 59 bytesRecv = recv( m_socket, recvbuf, 32, 0 ); 60 printf( "Bytes Recv: %ld\n", bytesRecv ); 61 62 bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); 63 printf( "Bytes Sent: %ld\n", bytesSent ); 64 65 return; 66 }
Complete Client Code
The following is the complete source code for the TCP/IP Client Application.
Client Source Code
1 #include <stdio.h> 2 #include "winsock2.h" 3 4 void main() { 5 6 // Initialize Winsock. 7 WSADATA wsaData; 8 int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); 9 if ( iResult != NO_ERROR ) 10 printf("Error at WSAStartup()\n"); 11 12 // Create a socket. 13 SOCKET m_socket; 14 m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); 15 16 if ( m_socket == INVALID_SOCKET ) { 17 printf( "Error at socket(): %ld\n", WSAGetLastError() ); 18 WSACleanup(); 19 return; 20 } 21 22 // Connect to a server. 23 sockaddr_in clientService; 24 25 clientService.sin_family = AF_INET; 26 clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" ); 27 clientService.sin_port = htons( 27015 ); 28 29 if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) { 30 printf( "Failed to connect.\n" ); 31 WSACleanup(); 32 return; 33 } 34 35 // Send and receive data. 36 int bytesSent; 37 int bytesRecv = SOCKET_ERROR; 38 char sendbuf[32] = "Client: Sending data."; 39 char recvbuf[32] = ""; 40 41 bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); 42 printf( "Bytes Sent: %ld\n", bytesSent ); 43 44 while( bytesRecv == SOCKET_ERROR ) { 45 bytesRecv = recv( m_socket, recvbuf, 32, 0 ); 46 if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { 47 printf( "Connection Closed.\n"); 48 break; 49 } 50 if (bytesRecv < 0) 51 return; 52 printf( "Bytes Recv: %ld\n", bytesRecv ); 53 } 54 55 return; 56 }
About Servers and Clients
There are two distinct types of socket network applications: Server and Client. Servers and Clients have different behaviors; therefore, the process of creating them is different. What follows is the general model for creating a streaming TCP/IP Server and Client.
Server
- Initialize WSA.
- Create a socket.
- Bind the socket.
- Listen on the socket.
- Accept a connection.
- Send and receive data.
- Disconnect.
Client
- Initialize WSA.
- Create a socket.
- Connect to the server.
- Send and receive data.
- Disconnect.
浙公网安备 33010602011771号