C++ socket编程

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

TCP类的:

C++ socket编程_第1张图片

 

TcpServer.cpp

//WSAStartup socket bind listen accept send/recv closesocket WSACleanup
#include 
#include 
#include 
#include 
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 \r\n",strlen("hello client \r\n" + 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 :"<endl;
  
     }

     closesocket(clientSock);
     closesocket(s);

     WSACleanup();
     return 0;
}

 

TcpClient.cpp

#include 
#include 
#include 
#include 
#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:"<endl;
   
     //不断地给服务端发送信息
     while(TRUE)
     {
        cin>>Cmd;
        cout<<"you input command is:"<endl;
        send(s,Cmd,MAXBYTE,0);
         
     }
     getchar();
     WSACleanup();
     return 0;
}

运行结果:

C++ socket编程_第2张图片

 

UDP:

C++ socket编程_第3张图片

UDPServer.cpp

#include 
#include 
#include 
#include 
using namespace std;
#pragma comment (lib,"ws2_32")
#pragma comment (lib,"winmm")

int main(){

    printf("This is UDP Server\n");

    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 
#include 
#include 
#include 
using namespace std;
#pragma comment (lib,"ws2_32")
#pragma comment (lib,"winmm")
int main(){

    printf("This is UDP Client\n");
    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\n",inet_ntoa(addr.sin_addr));
    sendto(socketClient,buf,sizeof(buf)+1,0,(SOCKADDR*)&addr,sizeof(SOCKADDR));

    closesocket(socketClient);
    WSACleanup();

    return 0;
}

 

运行结果:

C++ socket编程_第4张图片

你可能感兴趣的:(C++ socket编程)