• C++ socket编程


     源代码项目下载:http://download.csdn.net/detail/mmyzlinyingjie/7049499

    TCP类的:

    TcpServer.cpp

    //WSAStartup socket bind listen accept send/recv closesocket WSACleanup
    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    #include <windows.h>
    using namespace std;
    #pragma comment (lib,"ws2_32")
    #pragma comment (lib,"winmm")
    
    #define NAME_LEN 20
    
    int main(int argc , char* grgv[])
    {
    
        cout<<"This is Server cmd"<<endl;
         WSADATA wsaData;
         WSAStartup(MAKEWORD(2,2),&wsaData);
    
         //这个是服务器的socket,用来绑定网卡来监听
         SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    
         sockaddr_in addr;
         addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
         addr.sin_port = htons(827);
         addr.sin_family = PF_INET;
    
         bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
    
         listen(s,1);
    
         SOCKADDR clientAddr;
         int nSize = sizeof(SOCKADDR);
    
         //这个socket用来接收客户端发过来的信息
         SOCKET clientSock;
    
         //阻塞式,有客户端来连接时才会往下执行
         //执行到这一步,说明客户端和服务器已经建立了连接
         clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
    
         cout<<"sending..."<<endl;
         send(clientSock,"hello client 
    ",strlen("hello client 
    " + sizeof(char)),NULL);
         cout<<"sended"<<endl;
        
        //不断的接收客户端发送过来的信息
        while(TRUE)
         {
              
              char buff[MAXBYTE] = {0};
              //接收客户端发来的命令
             int i=1;
             i = recv(clientSock,buff,MAXBYTE,0);
            if(i != -1)
              cout<<"from client :"<<buff<<endl;
      
         }
    
         closesocket(clientSock);
         closesocket(s);
    
         WSACleanup();
         return 0;
    }

    TcpClient.cpp

    #include <stdio.h>
    #include <winsock2.h>
    #include <iostream>
    #include <conio.h>
    #include <string>
    using namespace std;
    #pragma comment (lib,"ws2_32")
    
    #define NAME_LEN 20
    
    int main(int argc,char* argv[])
    {
        cout<<"This is Client cmd"<<endl;
         WSADATA wsaData;
         WSAStartup(MAKEWORD(2,2),&wsaData);
    
         //创建一个socket来连接服务端
         SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    
         sockaddr_in addr;
         addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
         addr.sin_family = PF_INET;
         addr.sin_port = htons(827);
    
         //这条执行后,说明和服务端建立了连接
         connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
    
         char Buff[MAXBYTE] = {0};
         char Cmd[MAXBYTE] = {0};
         recv(s,Buff , MAXBYTE,0);
         cout<<"from server:"<<Buff<<endl;
       
         //不断地给服务端发送信息
         while(TRUE)
         {
            cin>>Cmd;
            cout<<"you input command is:"<<Cmd<<endl;
            send(s,Cmd,MAXBYTE,0);
             
         }
         getchar();
         WSACleanup();
         return 0;
    }

    运行结果:

    UDP:

    UDPServer.cpp

    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    #include <windows.h>
    using namespace std;
    #pragma comment (lib,"ws2_32")
    #pragma comment (lib,"winmm")
    
    int main(){
    
        printf("This is UDP Server
    ");
    
        WORD wVersionRequested;
        WSADATA wsaData;
        
        wVersionRequested = MAKEWORD(1 , 1 );
        WSAStartup( wVersionRequested, &wsaData );
        
        SOCKET socketServer = socket(AF_INET,SOCK_DGRAM,0);
    
        SOCKADDR_IN addr;
        addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(6000);
        
        bind(socketServer,(SOCKADDR*)&addr,sizeof(SOCKADDR));
    
            
        int len = sizeof(SOCKADDR);
        SOCKADDR_IN addrClient;
        char buf[100];
        recvfrom(socketServer,buf,sizeof(buf),0,(SOCKADDR*)&addrClient,&len);
        printf("%s",buf);
    
        
        closesocket(socketServer);
        WSACleanup();
        return 0;
    }

    UDPClient.cpp

    #include <winsock2.h>
    #include <stdio.h>
    #include <iostream>
    #include <windows.h>
    using namespace std;
    #pragma comment (lib,"ws2_32")
    #pragma comment (lib,"winmm")
    int main(){
    
        printf("This is UDP Client
    ");
        WORD wVersionRequested;
        WSADATA wsaData;
        
        wVersionRequested = MAKEWORD(1 , 1 );
        WSAStartup( wVersionRequested, &wsaData );
    
        SOCKET socketClient = socket(AF_INET,SOCK_DGRAM,0);
    
        SOCKADDR_IN addr;
        addr.sin_addr.S_un.S_addr = inet_addr("10.21.23.117");//"10.21.23.117"是你要发送的目的地
        addr.sin_family = AF_INET;
        addr.sin_port = htons(6000);
    
        char buf[100];
        sprintf(buf,"i MYNONA from cclient
    ",inet_ntoa(addr.sin_addr));
        sendto(socketClient,buf,sizeof(buf)+1,0,(SOCKADDR*)&addr,sizeof(SOCKADDR));
    
        closesocket(socketClient);
        WSACleanup();
    
        return 0;
    }

    运行结果:

  • 相关阅读:
    iOS:TabBarController 显示/隐藏第一级页面的TabBar
    iOS : Blur Effect
    ASIHTTPRequest / ASIFormDataRequest
    Xcode :Missing file warnings
    Axure设计软件下载安装及注册
    windows服务更改配置文件
    sql server 清理数据库日志
    sql server 随机生成布尔值
    sql server 授权相关命令
    用VS2015创建ASP.NET Web Forms 应用程序
  • 原文地址:https://www.cnblogs.com/mynona/p/3603433.html
Copyright © 2020-2023  润新知