数据未正确发送/接收

发布于 2024-12-07 18:20:43 字数 1831 浏览 0 评论 0原文

我正在尝试使用 c 中的套接字实现 TCP 服务器/客户端。 我以这样的方式编写程序,无论我们在客户端发送什么,都将逐行显示在服务器中,直到键入 exit 为止。 程序可以运行,但数据最后全部显示在服务器上。有人可以检查一下代码吗?

TCP 服务器

#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

main()
{
    char buf[100];
    socklen_t len;
    int k,sock_desc,temp_sock_desc;
    struct sockaddr_in client,server;

    memset(&client,0,sizeof(client));
    memset(&server,0,sizeof(server));
    sock_desc = socket(AF_INET,SOCK_STREAM,0);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_port = 7777;

    k = bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
    k = listen(sock_desc,20);
    len = sizeof(client);
    temp_sock_desc = accept(sock_desc,(struct sockaddr*)&client,&len);

    while(1)
        {
        k = recv(temp_sock_desc,buf,100,0);
        if(strcmp(buf,"exit")==0)
            break;

        if(k>0)
            printf("%s",buf);
    }

    close(sock_desc);
    close(temp_sock_desc);
    return 0;
}

TCP 客户端

#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

main()
{
    char buf[100];
    struct sockaddr_in client;
    int sock_desc,k;

    sock_desc = socket(AF_INET,SOCK_STREAM,0);
    memset(&client,0,sizeof(client));
    client.sin_family = AF_INET;
    client.sin_addr.s_addr = inet_addr("127.0.0.1");
    client.sin_port = 7777;

    k = connect(sock_desc,(struct sockaddr*)&client,sizeof(client));

    while(1)
        {
        gets(buf);
        k = send(sock_desc,buf,100,0);

        if(strcmp(buf,"exit")==0)
            break;
    }

    close(sock_desc);
    return 0;
}

I am trying to implement a TCP server/client using sockets in c.
I have written the program in such a way that whatever we send in the client is displayed in the server line by line till exit is typed.
The program works but the data is shown in the server all together at the last. Can anybody please check the code?

TCP SERVER

#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

main()
{
    char buf[100];
    socklen_t len;
    int k,sock_desc,temp_sock_desc;
    struct sockaddr_in client,server;

    memset(&client,0,sizeof(client));
    memset(&server,0,sizeof(server));
    sock_desc = socket(AF_INET,SOCK_STREAM,0);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_port = 7777;

    k = bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
    k = listen(sock_desc,20);
    len = sizeof(client);
    temp_sock_desc = accept(sock_desc,(struct sockaddr*)&client,&len);

    while(1)
        {
        k = recv(temp_sock_desc,buf,100,0);
        if(strcmp(buf,"exit")==0)
            break;

        if(k>0)
            printf("%s",buf);
    }

    close(sock_desc);
    close(temp_sock_desc);
    return 0;
}

TCP CLIENT

#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

main()
{
    char buf[100];
    struct sockaddr_in client;
    int sock_desc,k;

    sock_desc = socket(AF_INET,SOCK_STREAM,0);
    memset(&client,0,sizeof(client));
    client.sin_family = AF_INET;
    client.sin_addr.s_addr = inet_addr("127.0.0.1");
    client.sin_port = 7777;

    k = connect(sock_desc,(struct sockaddr*)&client,sizeof(client));

    while(1)
        {
        gets(buf);
        k = send(sock_desc,buf,100,0);

        if(strcmp(buf,"exit")==0)
            break;
    }

    close(sock_desc);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-12-14 18:20:43
while(1){
    k = recv(temp_sock_desc,buf,100,0);
    if(memcmp(buf,"exit", 4)==0)
        break;
    if(k>0)
        printf("%*.*s", k, k, buf);
}

recv() 的结果不是字符串,因此不会以 null 结尾。
上述“修复”并不完全正确,只是为了给您指明正确的方向。

while(1){
    k = recv(temp_sock_desc,buf,100,0);
    if(memcmp(buf,"exit", 4)==0)
        break;
    if(k>0)
        printf("%*.*s", k, k, buf);
}

The result of recv() is not a string, so it will not be nul-terminated.
The above "fix" is not entirely correct, but is only intended to point you in the right direction.

甜扑 2024-12-14 18:20:43

gets() 用 '\0' 替换终止换行符。结果字符串被发送到服务器,服务器将其写入标准输出。标准输出被缓冲,因此在程序终止或打印“\n”之前,屏幕上不会显示任何内容。

将 '\n' 添加到 printf 中:

if(k>0)
    printf("%s\n",buf);

gets() replaces terminating newline with '\0'. Resulting string is sent to server, which writes it to stdout. Stdout is buffered, so nothing will appear on screen until program terminates or '\n' is printed.

Add '\n' to your printf:

if(k>0)
    printf("%s\n",buf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文