logokfu的个人空间 https://passport2.21ic.com/?807999 [收藏] [复制] [RSS]

日志

2013-09-01

已有 1292 次阅读2013-9-1 11:54 |系统分类:通信网络

完整的win32 socket 服务器、客户端控制台收发程序。



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. void main() {

  4.     // Initialize Winsock.
  5.     WSADATA wsaData;
  6.     int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
  7.     if ( iResult != NO_ERROR )
  8.         printf("Error at WSAStartup()\n");

  9.     // Create a socket.
  10.     SOCKET m_socket;
  11.     m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

  12.     if ( m_socket == INVALID_SOCKET ) {
  13.         printf( "Error at socket(): %ld\n", WSAGetLastError() );
  14.         WSACleanup();
  15.         return;
  16.     }

  17.     // Bind the socket.
  18.     sockaddr_in service;

  19.     service.sin_family = AF_INET;
  20.     service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
  21.     service.sin_port = htons( 27015 );

  22.     if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
  23.         printf( "bind() failed.\n" );
  24.         closesocket(m_socket);
  25.         return;
  26.     }
  27.     
  28.     // Listen on the socket.
  29.     if ( listen( m_socket, 1 ) == SOCKET_ERROR )
  30.         printf( "Error listening on socket.\n");

  31.     // Accept connections.
  32.     SOCKET AcceptSocket;

  33.     printf( "Waiting for a client to connect...\n" );
  34.     while (1) {
  35.         AcceptSocket = SOCKET_ERROR;
  36.         while ( AcceptSocket == SOCKET_ERROR ) {
  37.             AcceptSocket = accept( m_socket, NULL, NULL );
  38.         }
  39.         printf( "Client Connected.\n");
  40.         m_socket = AcceptSocket; 
  41.         break;
  42.     }
  43.     
  44.     // Send and receive data.
  45.     int bytesSent;
  46.     int bytesRecv = SOCKET_ERROR;
  47.     char sendbuf[32] = "Server: Sending Data.";
  48.     char recvbuf[32] = "";
  49.     
  50.     bytesRecv = recv( m_socket, recvbuf, 32, 0 );
  51.     printf( "Bytes Recv: %ld\n", bytesRecv );
  52.     
  53.     bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
  54.     printf( "Bytes Sent: %ld\n", bytesSent );

  55.     return;
  56. }
复制代码
Complete Client Code

The following is the complete source code for the TCP/IP Client Application.

Client Source Code




       
  1. #include 
  2. #include "winsock2.h"

  3. void main() {

  4.     // Initialize Winsock.
  5.     WSADATA wsaData;
  6.     int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
  7.     if ( iResult != NO_ERROR )
  8.         printf("Error at WSAStartup()\n");

  9.     // Create a socket.
  10.     SOCKET m_socket;
  11.     m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

  12.     if ( m_socket == INVALID_SOCKET ) {
  13.         printf( "Error at socket(): %ld\n", WSAGetLastError() );
  14.         WSACleanup();
  15.         return;
  16.     }

  17.     // Connect to a server.
  18.     sockaddr_in clientService;

  19.     clientService.sin_family = AF_INET;
  20.     clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
  21.     clientService.sin_port = htons( 27015 );

  22.     if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
  23.         printf( "Failed to connect.\n" );
  24.         WSACleanup();
  25.         return;
  26.     }

  27.     // Send and receive data.
  28.     int bytesSent;
  29.     int bytesRecv = SOCKET_ERROR;
  30.     char sendbuf[32] = "Client: Sending data.";
  31.     char recvbuf[32] = "";

  32.     bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
  33.     printf( "Bytes Sent: %ld\n", bytesSent );

  34.     while( bytesRecv == SOCKET_ERROR ) {
  35.         bytesRecv = recv( m_socket, recvbuf, 32, 0 );
  36.         if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
  37.             printf( "Connection Closed.\n");
  38.             break;
  39.         }
  40.         if (bytesRecv<0)
  41.             return;
  42.         printf( "Bytes Recv: %ld\n", bytesRecv );
  43.     }

  44.     return;
  45. }
复制代码
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



  1. Initialize WSA.


  2. Create a socket.


  3. Bind the socket.


  4. Listen on the socket.


  5. Accept a connection.


  6. Send and receive data.


  7. Disconnect.


Client



  1. Initialize WSA.


  2. Create a socket.


  3. Connect to the server.


  4. Send and receive data.


  5. Disconnect.

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)