zoukankan      html  css  js  c++  java
  • Socket (二) 简单的代码实现

    SocktServer.cpp

    #include "stdafx.h"
    #pragma comment(lib,"ws2_32.lib")
    #include <Winsock2.h> 
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	WORD wVersionRequested; 
     WSADATA wsaData; 
     int err; 
       
     wVersionRequested = MAKEWORD( 1, 1 ); 
       
     err = WSAStartup( wVersionRequested, &wsaData ); 
     if ( err != 0 ) { 
      return 0; 
     } 
       
     if ( LOBYTE( wsaData.wVersion ) != 1 || 
            HIBYTE( wsaData.wVersion ) != 1 ) { 
      WSACleanup( ); 
      return 0; 
     } 
     SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); 
      
     SOCKADDR_IN addrSrv; 
     addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY); 
     addrSrv.sin_family=AF_INET; 
     addrSrv.sin_port=htons(8080); 
       
     bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); 
      
     listen(sockSrv,5); 
      
     SOCKADDR_IN addrClient; 
     int len=sizeof(SOCKADDR); 
     while(1) 
     { 
      printf("in while
    ");
      SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len); //服务端会在此一直等到客户端连接,只有客户端连接成功后,才会执行后续语句
      printf("have a client connecting
    ");
      char sendBuf[50]; 
      sprintf(sendBuf,"Welcome %s ",inet_ntoa(addrClient.sin_addr)); 
      send(sockConn,sendBuf,strlen(sendBuf)+1,0); 
      char recvBuf[50]; 
    
      for (int j = 0; j < 50; j++)
      {
    	  recvBuf[j] = NULL;
      }
      recv(sockConn,recvBuf,50,0); 
    
      printf("%s
    ",recvBuf); 
      closesocket(sockConn); 
     } 
      
    	return 0;
    }
    

     SocktClient.cpp

    #include "stdafx.h"
    #pragma comment(lib,"ws2_32.lib")
    #include <Winsock2.h> 
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	WORD wVersionRequested;
     WSADATA wsaData;
     int err;
     
     wVersionRequested = MAKEWORD( 1, 1 );
     
     err = WSAStartup( wVersionRequested, &wsaData );
     if ( err != 0 ) {
      return 0;
     }
     
     if ( LOBYTE( wsaData.wVersion ) != 1 ||
            HIBYTE( wsaData.wVersion ) != 1 ) {
      WSACleanup( );
      return 0;
     }
     SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
     
     SOCKADDR_IN addrSrv;
     addrSrv.sin_addr.S_un.S_addr=inet_addr("192.168.0.127");
     addrSrv.sin_family=AF_INET;
     addrSrv.sin_port=htons(8080);
     connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
     send(sockClient,"00000",strlen("11111 ")+1,0);
     char recvBuf[50];
     recv(sockClient,recvBuf,50,0);
     printf("%s
    ",recvBuf);
     
     closesocket(sockClient);
     WSACleanup();
    	return 0;
    }
    
  • 相关阅读:
    Python--json处理
    Python--加密模块
    Python--函数即变量
    bzoj 2276: [Poi2011]Temperature
    1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
    2017 9 15 noip模拟赛
    bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛
    2017 9 11 noip模拟赛T2
    URAL 1613. For Fans of Statistics (2017 9 6 noip模拟赛T3)
    codeforces 105 B. Dark Assembly(birbe贿赂 noip模拟赛)
  • 原文地址:https://www.cnblogs.com/blogwww/p/9508904.html
Copyright © 2011-2022 走看看