C套接字程序无法打印所需的输出(无错误)

时间:2018-06-08 13:53:45

标签: c sockets networking server client

我似乎无法打印我在客户端输入的内容。我编译了两个没有任何错误。无法弄清楚为什么它不打印所需的输出。

Q值。我的代码出了什么问题?如何通过服务器中的客户端打印带有空格的字符串。

以下是服务器的代码。

/* Server Side program */


    int sid,nid;
    struct sockaddr_in q;
    char x[100];

    int len=sizeof(struct sockaddr_in);

    sid=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    q.sin_family=PF_INET;
    q.sin_port=1600;
    q.sin_addr.s_addr=0;

    bind(sid,&q,len);

    listen(sid,20);

    while(1)
    {
            memset(x,0,sizeof(x));
            nid=accept(sid,&q,&len);
            read(nid,x,100);
            printf("%s\n",x);

    }

以下是客户端的代码。

/*client side program*/


    int sid,status;
    struct sockaddr_in q;
    int len=sizeof(struct sockaddr_in);
    char x[100];

    sid=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
            q.sin_family=PF_INET;                   
            q.sin_port=1600;                        
            q.sin_addr.s_addr=0;                  
    status=connect(sid,&q,len);
    if(status==-1)
    {
            printf("Connection failure");
            exit(0);
    }

    while(1)
    {
            printf("Enter string to send : ");
            scanf("%[^\n]s",x);
            write(sid,x,strlen(x));
            if(strcmp(x,"bye")==0)
                     break;

    }

1 个答案:

答案 0 :(得分:0)

您的代码存在各种问题。缺乏错误处理。 printf()scanf()的使用不正确。资源泄漏。

尝试更像这样的东西:

服务器:

int main()
{
    int sid, nid;
    struct sockaddr_in q;
    char x[100];
    socklen_t qlen;
    ssize_t xlen;

    sid = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sid < 0)
    {
        perror("socket failure");
        return -1;
    }

    q.sin_family = AF_INET;
    q.sin_port = htons(1600);
    q.sin_addr.s_addr = INADDR_ANY;

    if (bind(sid, &q, sizeof(q)) < 0)
    {
        perror("bind failure");
        return -1;
    }

    if (listen(sid, 20) < 0)
    {
        perror("listen failure");
        return -1;
    }

    while(1)
    {
        qlen = sizeof(q);
        nid = accept(sid, &q, &qlen);
        if (nid < 0)
        {
            perror("accept failure");
            return -1;
        }

        while ((xlen = read(nid, x, sizeof(x))) > 0)
        {
            printf("Received: '%.*s'\n", xlen, x);
        }

        if (xlen < 0)
            perror("read failure");
        else
            printf("client disconnected\n");

        close(nid);
    }

    return 0;
}

客户端:

int main()
{
    int sid, xlen;
    struct sockaddr_in q;
    char x[100];

    sid = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sid < 0)
    {
        perror("socket failure");
        return -1;
    }

    q.sin_family = AF_INET;                   
    q.sin_port = htons(1600);
    q.sin_addr.s_addr = inet_addr("server ip here"); // or INADDR_LOOPBACK (127.0.0.1)

    if (connect(sid, &q, sizeof(q)) < 0)
    {
        printf("connect failure");
        return -1;
    }

    while(1)
    {
        printf("Enter string to send : ");
        if (!fgets(x, sizeof(x), stdin))
            break;

        xlen = strlen(x);
        if ((xlen > 0) && (x[xlen-1] == '\n'))
        {
            x[xlen-1] = '\0';
            --xlen;
        }

        if (write(sid, x, xlen) < 0)
        {
            perror("write failure");
            break;
        }

        if (strcmp(x, "bye") == 0)
            break;
    }

    close(sid);

    return 0;
}