李毅吧 关注:34,004,175贴子:993,610,513
  • 3回复贴,共1

有哪位大神做过基于WinSock的网络通信啊??用VC的。

只看楼主收藏回复

搞不下去了啊!!!


1楼2014-08-19 12:15回复
    RT,顶!


    2楼2014-08-19 12:16
    回复
      两个控制台出来之后,在客户端输入数据后,按ENTER键后服务器没有反应,接收不到。
      同时,在服务器里也不能输入数据。


      3楼2014-08-19 12:16
      回复
        下面是源代码,哪位大神帮我看一下啊。新人,刚学C++,求解释。`
        //TcpClient
        #include
        #include
        #include
        #pragma comment(lib,"ws2_32.lib")
        using namespace std;
        void main(int argc,char *argv[])
        {
        //初始化套接字,分配网络资源
        WSADATA wsaData;
        WORD wVersion;
        wVersion = MAKEWORD(1,1); //生存版本号 1.1
        int err = WSAStartup(wVersion,&wsaData);
        if (err!=0)
        {
        return;
        }
        if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1) //版本不一致,通知用户,调用WSACleanup()函数退出
        {
        WSACleanup();
        return;
        }
        //创建套接字 int sockConn = socket(AF_INET,SOCK_STREAM,0); if (INVALID_SOCKET==sockConn) { WSACleanup(); return; } //向服务器端发送连接请求 struct sockaddr_in addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addrSrv.sin_port = htons(6000); int res1 = connect(sockConn,(struct sockaddr*)&addrSrv, sizeof(addrSrv)); if (res1 < 0) { if(res1 == EINPROGRESS) { perror("connecting stream socket time out:"); } else { perror("connecting stream socket error:"); } closesocket(sockConn); return; } //发送数据 char sendBuf[100]; char c; while(1) { int i = 0; while ((c=getchar())!=*\n*); { sendBuf[i++] = c; } sendBuf[i]=*\0*; } int res2 = send(sockConn,sendBuf,strlen(sendBuf),0); if (res2 == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(sockConn); WSACleanup(); return ; } cout<<res2<<endl; //接收服务器端数据 int len; char recvBuf[100]; while(1) { len=recv(sockConn,recvBuf,sizeof(recvBuf),0); if(len>0) { recvBuf[len]=*\0*; cout<<recvBuf<<endl; } } //关闭连接,本次客户请求完成 closesocket(sockConn); //释放套接字资源库 WSACleanup();}
        //TcpSrv
        #include
        #include
        #include
        #include
        #pragma comment(lib,"ws2_32.lib")
        using namespace std;
        #include
        void main()
        {
        //初始化套接字库,分配网络资源 WSADATA wsaData; WORD wVersion; wVersion = MAKEWORD(1,1); //生存版本号 1.1 int err = WSAStartup(wVersion,&wsaData); if (err) { return; } if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1) //版本不一致,通知用户,调用WSACleanup()函数退出 { WSACleanup(); return; } int sockConn,len1; char recvBuf[512]; //创建套接字 SOCKET ListenSocket = INVALID_SOCKET; ListenSocket = socket(AF_INET,SOCK_STREAM,0); //创建一个流式套接字 if (ListenSocket<0) { WSACleanup(); return; } //绑定套接字 struct sockaddr_in addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //为套接字分配地址 addrSrv.sin_port = htons(6000); err = bind(ListenSocket,(struct sockaddr*)&addrSrv,sizeof(addrSrv)); //开始监听客户端 listen(ListenSocket,10); //循环处理监听到的每个客户端请求 while(1) { printf("6000开始监听......\n"); //接收客户端请求,得到客户端信息于addrClient中 struct sockaddr_in addrClient; sockConn = accept(ListenSocket,(struct sockaddr*)&addrClient,NULL); if(sockConn) { continue; } while(1) { len1=recv(sockConn,recvBuf,sizeof(recvBuf),0); if(len1>0) { recvBuf[len1]=*\0*; cout<<recvBuf<<endl; } } //向该客户端发送数据 char sendBuf[100]; char c; while(1) { int i = 0; while ((c=getchar())!=*\n*); { sendBuf[i++] = c; } sendBuf[i]=*\0*; } send(sockConn,sendBuf,strlen(sendBuf),0); //cout<<WSAGetLastError()<<endl; //cout<<recvBuf<<endl; //关闭当前客户端连接 closesocket(sockConn); } //关闭服务器连接 closesocket(ListenSocket); //释放套接字库资源 WSACleanup();}


        4楼2014-08-19 12:17
        回复