Windows TCP协议网络编程

时间:2023-02-13 10:25:18
服务器代码 
包括创建Socket  绑定  监听  Accept  最后是清理工作
//
#include "stdafx.h"#include <winsock2.h>#include <iostream>#pragma comment( lib, "ws2_32.lib" )
const static int BUF_SIZE = 256;using namespace std;
#define MAIN_RETURN_ERROR   -1;#define MAIN_RETURN_NORMAL  0;
int _tmain( int argc , _TCHAR* argv[]){      // 初始化Winsock 2.2      WSADATA wsaData ;      if( WSAStartup ( MAKEWORD( 2, 2 ), & wsaData ) != 0 )     {           cout<<"WSAStartup 无法初始化 !" <<endl ;           return MAIN_RETURN_ERROR ;     }
      // 通过WinSock 实现网络通信      SOCKET sServer ;    // 创建用于监听的Socket      sServer = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );      if( INVALID_SOCKET == sServer )     {           cout<<"Socket Failed !" <<endl;           WSACleanup();           return MAIN_RETURN_ERROR ;     }
      // 设置服务器Socket 地址      SOCKADDR_IN addrServ ;      addrServ.sin_family   = AF_INET;      addrServ.sin_port     = htons( 9990 );   // 服务器端口号      addrServ.sin_addr .S_un. S_addr = htonl ( INADDR_ANY );
      // 绑定      int    retVal ;      retVal = bind ( sServer, ( const struct sockaddr *)&addrServ , sizeof( SOCKADDR_IN ) );      if( SOCKET_ERROR == retVal )     {           cout<<"bind failed !" <<endl;           closesocket( sServer );           WSACleanup();           return -1;     }
      // 监听      retVal = listen ( sServer, 1 );      if( SOCKET_ERROR == retVal )     {           cout<<"listen failed !" <<endl;           closesocket( sServer );           WSACleanup();           return -1;     }
      // 设置socket 其为非阻塞模式 , argp 设为非零值      int iModel = 1;       retVal = ioctlsocket ( sServer, FIONBIO, (u_long FAR* )&iModel );      if( SOCKET_ERROR == retVal )     {           cout<<"ioctlsocket failed " <<endl;           WSACleanup();           return MAIN_RETURN_ERROR ;     }
      cout<<"TCP Server Start ... ..." <<endl;      SOCKET sClient ;      sockaddr_in addrClient ;      int addrClientLen = sizeof( addrClient );      // 循环等待      while( true )     {           sClient = accept ( sServer, ( sockaddr FAR*)&addrClient , &addrClientLen );           if( INVALID_SOCKET == sClient )          {               int err = WSAGetLastError();               if( WSAEWOULDBLOCK == err  // 没接到连接请求              {                    Sleep( 100 );                    continue;              }               else              {                    cout<<"accept failed" <<endl;                    closesocket( sServer );                    WSACleanup();                    return MAIN_RETURN_ERROR ;              }          }
           break;     }
      // 循环接受客户端发送来的请求,直到客户端发送 quit 命令      char buf [BUF_SIZE];      while( true )     {           ZeroMemory( buf , BUF_SIZE );           retVal = recv ( sClient, buf, BUF_SIZE , 0 );
           if( SOCKET_ERROR == retVal )          {               int err = WSAGetLastError();               if( WSAEWOULDBLOCK == err )              {                    Sleep( 100 );                    continue;              }               else if ( WSAETIMEDOUT == err || WSAENETDOWN == err )   // 网络系统故障              {                    cout<<"recv failed !" <<endl;                    closesocket( sServer );                    closesocket( sClient );                    WSACleanup();                    return MAIN_RETURN_ERROR ;              }          }
           SYSTEMTIME st ;           GetLocalTime( &st );           char sDateTime [30];           sprintf( sDateTime , "%4d-%2d-%2d %2d:%2d:%2d", st.wYear , st. wMonth, st .wDay, st.wHour , st. wMinute, st .wSecond );           cout<<sDateTime <<"  , Recv From Client [ "<< inet_ntoa( addrClient .sin_addr )<<":" <<addrClient. sin_port<<"]: " <<buf<< endl;
           if( 0 == strcmp ( buf, "quit" ) )          {               retVal = send ( sClient, "quit", strlen ("quit"), 0 );               break;          }           else          {               char msg [BUF_SIZE];               sprintf( msg , "Message receive - %s ", buf );               while( true )              {                    retVal = send ( sClient, msg, strlen ( msg ), 0 );                    if( SOCKET_ERROR == retVal )                   {                         int err = WSAGetLastError();                         if( WSAEWOULDBLOCK == err )                        {                              Sleep( 100 );                              continue;                        }                         else                        {                              cout<<"send failed !" <<endl;                              closesocket( sServer );                              closesocket( sClient );                              WSACleanup();                              return MAIN_RETURN_ERROR ;                        }                   }                    else                         break;              }          }     }
      // 最后做一些清理工作      closesocket( sServer );      closesocket( sClient );      WSACleanup();      system("pause" );      return MAIN_RETURN_NORMAL ;}


客户端代码
包括创建Socket 连接
#include "stdafx.h"#include <WinSock2.h>#include <string>#include <iostream>
#pragma comment( lib, "WS2_32.lib" )const static int BUF_SIZE = 256;
using namespace std;
int _tmain( int argc , _TCHAR* argv[]){      char buf [BUF_SIZE];      WSADATA wsaData ;      if( WSAStartup ( MAKEWORD( 2, 2 ), & wsaData ) != 0 )     {           cout<<"WSAStartup 无法初始化 !" <<endl ;           return -1;     }
      SOCKET sHost ;      sHost = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );      if( INVALID_SOCKET == sHost )     {           cout<<"socket failed !" <<endl;           WSACleanup();           return -1;     }
      int iModel = 1;       int retVal = ioctlsocket( sHost, FIONBIO , (u_long FAR* )&iModel );      if( SOCKET_ERROR == retVal )     {           cout<<"ioctlsocket failed " <<endl;           WSACleanup();           return -1;     }
      SOCKADDR_IN servAddr ;      servAddr.sin_family = AF_INET;      servAddr.sin_addr .S_un. S_addr = inet_addr ( "192.168.1.44" );      servAddr.sin_port = htons(9990);      int sServerAddrLen = sizeof( servAddr );
      while( true )     {           retVal = connect ( sHost, ( LPSOCKADDR)& servAddr , sizeof (servAddr) );           if( SOCKET_ERROR == retVal )          {               int err = WSAGetLastError();               if( WSAEWOULDBLOCK == err || WSAEINVAL == err  // 没接到连接请求              {                    Sleep( 100 );                    continue;              }               else if ( err == WSAEISCONN )                    break;               else              {                    cout<<"accept failed" <<endl;                    closesocket( sHost );                    WSACleanup();                    return -1;              }          }     }
      while( true )     {           cout<<"Please input a string to send :" <<endl;           std::string str;           std::getline ( std:: cin, str );           ZeroMemory( buf , BUF_SIZE );           strcpy( buf , str. c_str() );
           while( true )          {               retVal = send ( sHost, buf, strlen (buf), 0 );               if( SOCKET_ERROR == retVal )              {                    int err = WSAGetLastError();                    if( WSAEWOULDBLOCK == err || WSAEINVAL == err  // 没接到连接请求                   {                         Sleep( 100 );                         continue;                   }                    else if ( err == WSAEISCONN )                         break;                    else                   {                         cout<<"accept failed" <<endl;                         closesocket( sHost );                         WSACleanup();                         return -1;                   }              }               else                    break;          }
           while( true )          {               ZeroMemory( buf , BUF_SIZE );               retVal = recv ( sHost, buf, sizeof (buf) + 1, 0 );               if( SOCKET_ERROR == retVal )              {                    int err = WSAGetLastError();                    if( WSAEWOULDBLOCK == err || WSAEINVAL == err  // 没接到连接请求                   {                         Sleep( 100 );                         continue;                   }                    else if ( err == WSAEISCONN )                         break;                    else                   {                         cout<<"accept failed" <<endl;                         closesocket( sHost );                         WSACleanup();                         return -1;                   }              }               else                    break;
               cout<<"Recv From Sserver : " <<buf<< endl;               if( strcmp ( buf, "quit") == 0 )              {                    cout<<"quit" <<endl;                    break;              }          }     }
      // 最后做一些清理工作      closesocket( sHost );      WSACleanup();      system("pause" );      return 0;}