qianhangkang
qianhangkang
采纳率100%
2016-02-27 03:11

新手关于socket的小问题 各位大大来看看谢谢啦.。

5
已采纳

socket的通信中 client端接受的消息总是慢一拍怎么回事图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • shaosheng2008 wolf188 5年前

    原因是多了'\n':
    scanf("%s\n", cmd);
    改成
    scanf("%s", cmd);
    也是OK的

    点赞 1 评论 复制链接分享
  • Royal_lr Royal_lr 5年前

    客户端接受的消息本来就是通过服务端转发的信息,,

    点赞 评论 复制链接分享
  • shaosheng2008 wolf188 5年前

    scanf("%s\n", cmd);
    把这个换成以下的语句试试(头文件增加 #include ):
    std::cin>>cmd;

    点赞 评论 复制链接分享
  • qianhangkang qianhangkang 5年前

    谢谢各位大大,帮小生一看

    点赞 评论 复制链接分享
  • qianhangkang qianhangkang 5年前

    这是客户端的:
    #include "stdafx.h"
    #include
    #include
    #include
    #pragma comment (lib,"ws2_32.lib")

    int _tmain(int argc, _TCHAR* argv[])
    {
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);
    if (2 != LOBYTE(wsadata.wVersion) || 2 != HIBYTE(wsadata.wVersion))
    exit(0);

        SOCKET sockclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (INVALID_SOCKET == sockclient)
            exit(0);
    
        sockaddr_in addsever = { 0 };
        addsever.sin_family = AF_INET;
        addsever.sin_port = htons(8383);
        addsever.sin_addr.S_un.S_addr = inet_addr("192.168.23.7");
    
        if (SOCKET_ERROR == connect(sockclient, (SOCKADDR*)&addsever, sizeof(addsever)))
            exit(0);
        printf("call succeed\n");
    
        int recvlen = { 0 };
        char message[MAXBYTE] = { 0 };
        while (true)
        {
        recvlen=recv(sockclient, message,MAXBYTE,0);
        if (recvlen>0)
        {
            printf(message);
            printf("\n");
        }
        }
    
    WSACleanup();
    return 0;
    

    }

    点赞 评论 复制链接分享
  • qianhangkang qianhangkang 5年前

    服务端代码:
    #include "stdafx.h"
    #include
    #include
    #pragma comment(lib,"ws2_32.lib")

    int _tmain(int argc, _TCHAR* argv[])
    {
    BOOL bret = false;
    do
    {
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);
    if (2 != LOBYTE(wsadata.wVersion) || 2 != HIBYTE(wsadata.wVersion))
    break;

        SOCKET socksever;
        socksever = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (INVALID_SOCKET == socksever)
            break;
    
        sockaddr_in addrsever = { 0 };
        addrsever.sin_family = AF_INET;
        addrsever.sin_port = htons(8383);
    
        if (SOCKET_ERROR == bind(socksever, (SOCKADDR*)&addrsever, sizeof(addrsever)))
            break;
    
    
        if (SOCKET_ERROR == listen(socksever, 5))
            break;
        SOCKET sockclient;
        sockclient= accept(socksever, nullptr, nullptr);
        if (INVALID_SOCKET == socksever)
            break;
    
        char cmd[MAXBYTE] = { 0 };      
        while (true)
        {
            printf("please input your cmd:\n");
            scanf("%s\n", cmd);
            send(sockclient, cmd, strlen(cmd)+1, 0);
    
        }
        system("pause");
        bret = true;
    } while (false);
    WSACleanup();
    return 0;
    

    }

    点赞 评论 复制链接分享

相关推荐