socket编程

服务器端:


#include<cstring>
#include<iostream>
#include<winsock2.h>

void main()
{
    WORD wVersionRequested;//要求的版本号
    WSADATA wsaData;//that is to receive details of the Windows Sockets implementation.
    int err;
    /*
    MAKEWORD(x,y)x是低字节这里表主版本号,y是高字节表次版本号
    */
    wVersionRequested = MAKEWORD( 2, 2 );
    /*
    WSAStartup()加载相应的动态链接库并协商使用那个版本的socket
    */
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return;
    }
   
    /* Confirm that the WinSock DLL supports 2.2.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.2 in addition to 2.2, it will still return */
    /* 2.2 in wVersion since that is the version we      */
    /* requested.                                        */
   
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        WSACleanup( );
        return;
    }

    SOCKET listenSocket = socket(AF_INET,SOCK_STREAM,0);
    /*
    struct sockaddr_in{
    short            sin_family;
    unsigned short      sin_port;
    struct   in_addr      sin_addr;
    char               sin_zero[8];};

  */
    SOCKADDR_IN addrSrv;
    addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    addrSrv.sin_family = AF_INET;
    addrSrv.sin_port = htons(8080);

    bind(listenSocket,(SOCKADDR *) &addrSrv,sizeof (SOCKADDR));

    listen(listenSocket,5);
    SOCKADDR addClient ;
    int len = sizeof (SOCKADDR);
    SOCKET sockConn = accept(listenSocket,&addClient,&len);
    while(1)
    {
        char sendBuff[100];
        std::cin>>sendBuff;
        send(sockConn,sendBuff,strlen(sendBuff)+1,0);
        char recBuff[100];
        recv(sockConn,recBuff,sizeof(recBuff),0);
        std::cout<<recBuff<<std::endl;

    }
   
}

客服端:


#include<cstring>
#include<iostream>
#include<winsock2.h>//包含socket用到的函数
#pragma comment(lib,"ws2_32.lib")//加入动态链接库

void main()
{
    WORD wVersionRequested;//要求的版本号
    WSADATA wsaData;//that is to receive details of the Windows Sockets implementation.
    int err;
    /*
    MAKEWORD(x,y)x是低字节这里表主版本号,y是高字节表次版本号
    */
    wVersionRequested = MAKEWORD( 2, 2 );
    /*
    WSAStartup()加载相应的动态链接库并协商使用那个版本的socket
    */
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return;
    }
   
    /* Confirm that the WinSock DLL supports 2.2.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.2 in addition to 2.2, it will still return */
    /* 2.2 in wVersion since that is the version we      */
    /* requested.                                        */
   
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        WSACleanup( );
        return;
    }

    //创建一个客服端socket
    SOCKET clientSocket = socket(AF_INET,SOCK_STREAM,0);
   
    //addrSev记录主机的ip和端口信息,只有其sin_family元素网络字节顺序不需要转换
    SOCKADDR_IN addrSev;
   
    addrSev.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    addrSev.sin_family = AF_INET;
    //转换
    addrSev.sin_port = htons(8080);
   
    connect(clientSocket,(SOCKADDR *) &addrSev,sizeof (SOCKADDR));
    while(1)
    {
        
        char  recvBuf[100];
        
        recv(clientSocket,recvBuf,100,0);
        
        std::cout<<recvBuf<<std::endl;
        
        char sendBuf[100];
        
        std::cin>>sendBuf;
        
        send(clientSocket,sendBuf,strlen(sendBuf)+1,0);
    }
   
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值