UDP C Server 未收到数据包

发布于 2024-10-22 23:01:47 字数 2729 浏览 7 评论 0原文

我知道已经回答了相关问题,但我没能解决我的问题。

我有一个简单的 UDP 客户端-服务器应用程序。客户和服务器看起来没问题,但是服务器没有从客户端接收数据包,它只是无限地接收网络 ID 192(或者客户端可能没有正确发送数据包)。

我似乎无法弄清楚问题、地址和地址。端口都可以,我没有任何防火墙,我什至为端口添加了一个例外,只是为了确定。如果我启动服务器,我可以看到它在正确的端口上正常监听(netstat -a -s -p udp)。你能给我一个提示,告诉我出了什么问题吗?

这是我的服务器代码:

/*.. includes */
#define PORT 8888
#define NPACK 10
#define MAXLEN 100

void signalError(char* s){
    perror(s);    
    exit(1);
}

int main()
{
struct sockaddr_in struct_srv, struct_client;
int s,i,cod, numbytes;
size_t clientSize;
int32_t nr;
int32_t sir1[MAXLEN], sir2[MAXLEN], sirComune[MAXLEN], nrEl1, nrEl2, nrComune;

//Creating the socket:
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s==-1) signalError("Error while creating socket!");

memset(&struct_srv, 0, sizeof(struct_srv));
struct_srv.sin_family = AF_INET;
struct_srv.sin_addr.s_addr = htonl(INADDR_ANY);
struct_srv.sin_port = htons(PORT);  

s = bind(s, (struct sockaddr*) &struct_srv, sizeof(struct_srv));
if(s==-1) signalError("Bind error. Port is already in use!");

//receive packets:    
nrEl1 = -1;

char buf[MAXLEN];
printf("Accepting packets:\n");
//for(i=0;i<NPACK;i++) {
for(;;) {
     //Receive packets from client:
    clientSize = sizeof(struct_client);        
    numbytes = recvfrom(s, buf, MAXLEN - 1, 0,
                (struct sockaddr*) &struct_client, &clientSize);
    buf[numbytes] = '\0';

    printf("Packet is %d long.\n", numbytes);
    printf("Packet contains %s:\n", buf);       
    sleep(3);
}

close(s);
return 0;
}

和我的客户端代码:

/* includes */
#define SRV_IP "127.0.0.1"
#define NPACK 100
#define MAXLEN 100
#define PORT 8888

void signalError(char* s){
    perror(s);
    exit(1);
}

int main(void)
{
struct sockaddr_in struct_client;
int s, i, result, size_client = sizeof(struct_client);
int32_t nr, sir1[MAXLEN], sir2[MAXLEN];

s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(s==-1) signalError("Erorr creating socket!");

memset((char*) &struct_client, 0, sizeof(struct_client));
struct_client.sin_family = AF_INET;
struct_client.sin_addr.s_addr = htonl(INADDR_ANY);
struct_client.sin_port = htons(PORT);
if(inet_aton(SRV_IP, &struct_client.sin_addr)==0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
}

char buf[MAXLEN];
int len;
for(i=0;i<NPACK;i++) {    
    printf("Give packet %d:\n", i+1);       
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf)-1] = '\0';
    printf("I've read %s\n", buf);
    printf("Sending packet %d\n", i+1);
    result = sendto(s, buf, sizeof(buf)+1, 0,
            (struct sockaddr*) &struct_client, size_client);
    if(result==-1) signalError("Error sending packets!");        
}

close(s);
return 0;
}

I know that there are related questions already answered, but I didn't manage to solve my problem.

I have a simple UDP client-server app. Both the client & server look ok, however the server doesn't receive packets from the client, it just infinitely receives the net id, 192 (or maybe the client doesn't send the packages correctly).

I can't seem to figure out the problem, the address & port are both ok, I don't have any firewall, and I even added an exception for the port, just to be sure. If I start the server, I can see that it's listening ok, on the right port (netstat -a -s -p udp). Can you please give me a hint on what's wrong?

Here is my server code:

/*.. includes */
#define PORT 8888
#define NPACK 10
#define MAXLEN 100

void signalError(char* s){
    perror(s);    
    exit(1);
}

int main()
{
struct sockaddr_in struct_srv, struct_client;
int s,i,cod, numbytes;
size_t clientSize;
int32_t nr;
int32_t sir1[MAXLEN], sir2[MAXLEN], sirComune[MAXLEN], nrEl1, nrEl2, nrComune;

//Creating the socket:
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s==-1) signalError("Error while creating socket!");

memset(&struct_srv, 0, sizeof(struct_srv));
struct_srv.sin_family = AF_INET;
struct_srv.sin_addr.s_addr = htonl(INADDR_ANY);
struct_srv.sin_port = htons(PORT);  

s = bind(s, (struct sockaddr*) &struct_srv, sizeof(struct_srv));
if(s==-1) signalError("Bind error. Port is already in use!");

//receive packets:    
nrEl1 = -1;

char buf[MAXLEN];
printf("Accepting packets:\n");
//for(i=0;i<NPACK;i++) {
for(;;) {
     //Receive packets from client:
    clientSize = sizeof(struct_client);        
    numbytes = recvfrom(s, buf, MAXLEN - 1, 0,
                (struct sockaddr*) &struct_client, &clientSize);
    buf[numbytes] = '\0';

    printf("Packet is %d long.\n", numbytes);
    printf("Packet contains %s:\n", buf);       
    sleep(3);
}

close(s);
return 0;
}

And my client code:

/* includes */
#define SRV_IP "127.0.0.1"
#define NPACK 100
#define MAXLEN 100
#define PORT 8888

void signalError(char* s){
    perror(s);
    exit(1);
}

int main(void)
{
struct sockaddr_in struct_client;
int s, i, result, size_client = sizeof(struct_client);
int32_t nr, sir1[MAXLEN], sir2[MAXLEN];

s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(s==-1) signalError("Erorr creating socket!");

memset((char*) &struct_client, 0, sizeof(struct_client));
struct_client.sin_family = AF_INET;
struct_client.sin_addr.s_addr = htonl(INADDR_ANY);
struct_client.sin_port = htons(PORT);
if(inet_aton(SRV_IP, &struct_client.sin_addr)==0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
}

char buf[MAXLEN];
int len;
for(i=0;i<NPACK;i++) {    
    printf("Give packet %d:\n", i+1);       
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf)-1] = '\0';
    printf("I've read %s\n", buf);
    printf("Sending packet %d\n", i+1);
    result = sendto(s, buf, sizeof(buf)+1, 0,
            (struct sockaddr*) &struct_client, size_client);
    if(result==-1) signalError("Error sending packets!");        
}

close(s);
return 0;
}

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

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

发布评论

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

评论(3

万劫不复 2024-10-29 23:01:47

以下是一些要点:

  1. 在服务器中,在每次调用 recvfrom 之前设置 size_client = sizeof(struct_client)。它既是输入参数又是输出参数,因此您需要确保一次调用的输出不会干扰下一次调用的输入。
  2. 确保将 buf 的最后一个字节设置为 '\0' 以防止 printf 读取超出边界的内存。
  3. 什么是鳕鱼?它从哪里来?我认为您的意思是测试numbytes
  4. 设置 buf[strlen(buf) - 1] = '\0' 会截掉缓冲区的最后一个字符;这就是你想做的吗?

它不起作用的主要原因是:

您从未初始化过 nr,因此无法得知 sendto 正在发送什么。它肯定不会发送 buf,我认为这就是您想要的......您可能想要这个:

sendto(s, buf, sizeof(buf), ...)

Here are a few minor points:

  1. In your server, set size_client = sizeof(struct_client) before every call to recvfrom. It is both an input and output parameter, so you want to make sure the output from one call does not interfere with the input to the next.
  2. Make sure to set the last byte of buf to '\0' to prevent your printf from reading memory beyond the boundary.
  3. What is cod? Where did it come from? I think you meant to test numbytes.
  4. Setting buf[strlen(buf) - 1] = '\0' chops off the last character of the buffer; is that what you were trying to do?

And the major reason it doesn't work:

You never initialized nr, so there is no telling what sendto is sending. It certainly isn't sending buf, which I think was what you wanted... You probably want this:

sendto(s, buf, sizeof(buf), ...)
陪你搞怪i 2024-10-29 23:01:47

除非我错过了一些重大的事情,否则你只是在这里发送垃圾。

nr 永远不会在发送者上初始化,除了在评论中。
buf 正在初始化,但未在任何地方使用。

您可以尝试将 sendto 部分更改为以下内容:

result = sendto(s, buf, strlen(buf)+1, 0,
        (struct sockaddr*) &struct_client, size_client);

Unless I'm missing something drastic, you're just sending garbage here.

nr is never initialized on the sender, except in a comment.
buf is being initialized, but not used anywhere.

Can you try changing the sendto part to something like:

result = sendto(s, buf, strlen(buf)+1, 0,
        (struct sockaddr*) &struct_client, size_client);
初相遇 2024-10-29 23:01:47

在您的客户端代码中,通过网络发送的 nr 变量未初始化,可以指向它想要的任何内容。您可能想发送您的 buf 数组。

In your client code, the nr variable that is sent over the net is not initialized and can point to whatever it wants. You probably want to send your buf array instead.

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