2 nishi520woshi nishi520woshi 于 2014.08.19 12:14 提问

基于winsock的网络通信设计。

服务器和客户端程序都已经编好,但是运行后在客户端控制台输入数据后,按enter后服务器没有反应,接收不到数据;同时服务器控制台中也输入不了数据。
下面是源代码,哪位大神帮我看一下啊。新人,刚学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();
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!