分析一个基于windows的客户端和服务器端程序

服务器端源码:

#include 
#include 
#pragma comment (lib,"ws2_32.lib")

int main()
{
      
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
 
    wVersionRequested = MAKEWORD( 2, 2 );
 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        return 0;
    }
 
 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 ) {
        WSACleanup( );
        return 0; 
    }

    SOCKET  socketServer=socket(AF_INET,SOCK_STREAM,0);

    SOCKADDR_IN  addrServer;

    addrServer.sin_family=AF_INET;

    addrServer.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

    addrServer.sin_port=htons(6000);

    bind(socketServer, (struct sockaddr *)&addrServer, sizeof(struct sockaddr));

   listen(socketServer, 5);

   SOCKADDR_IN  addrClient;

   int addrLen=sizeof(SOCKADDR_IN);

   char sendBuf[100];

   char recvBuf[100];

   int i=1;
   while(1)
  {
      printf("服务器端等待第%d个客户端连接请求...\n", i++);

      SOCKET newsocketServer=accept(socketServer,(struct sockaddr *)&addrClient, &addrLen);

      if(newsocketServer!=INVALID_SOCKET)
      {
       printf("服务器端与客户端连接成功...\n");
      }

      memset(sendBuf,0,100);

      sprintf(sendBuf,"Welcome you to come here");

           send(newsocketServer, sendBuf, strlen(sendBuf)+1,0);

      memset(recvBuf,0,100);

      recv(newsocketServer,recvBuf,100,0);

      printf("服务器端收到信息:%s\n",recvBuf);

      closesocket(newsocketServer);
 }

 WSACleanup();

 return 0;
}

 此文章来自于个人博客: 阿浪博客     http://blog.163.com/wenxianliang08@126/

客户端源码:

#include 
#include 
#pragma comment (lib,"ws2_32.lib")

int main()
{
      
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
 
    wVersionRequested = MAKEWORD( 2, 2 );
 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        return 0;
    }
 
 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 ) {
        WSACleanup( );
        return 0; 
    }
 
    SOCKET  socketClient=socket(AF_INET,SOCK_STREAM,0);    //UDP使用SOCK_DGRAM
 
    SOCKADDR_IN  addrServer;
 
    addrServer.sin_family=AF_INET;
 
    addrServer.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
 
    addrServer.sin_port=htons(6000);
 
    char sendBuf[100];
 
    char recvBuf[100]; 

    printf("客户端向服务器端连接请求...\n"); 

    int Isconnect=connect(socketClient, (struct sockaddr *)&addrServer, sizeof(struct sockaddr));

    if(Isconnect!=0)
    {
        printf("客户端无法连接服务器端...\n");

        return 0;
    }

    printf("客户端已成功连接服务器端...\n");   

    memset(recvBuf,0,100);
 
    recv(socketClient,recvBuf,100,0);

    printf("客户端收到信息:%s\n",recvBuf);

    memset(sendBuf,0,100);  //清空sendBuf
 
    sprintf(sendBuf,"Hello , I am Mr Wen !");
 
    send(socketClient, sendBuf, strlen(sendBuf)+1,0); 
 
    closesocket(socketClient);
 
    WSACleanup();
 
    return 0;
}
UDP模型见下:

https://blog.csdn.net/tianhaobing/article/details/69679693?locationNum=12&fps=1


C++操作MySQL数据库:

https://www.cnblogs.com/huang6mi/p/7233476.html

https://www.cnblogs.com/xiao-cheng/archive/2011/10/03/2198382.html

http://blog.chinaunix.net/uid-24862988-id-3833978.html


https://zhidao.baidu.com/question/81951651.html

你可能感兴趣的:(学习)