从服务器向客户端发送UDP消息

3
我正在尝试使用UDP套接字从服务器向客户端发送消息...然而,我的客户端没有收到任何消息。非常感谢您的反馈!
编辑:也许我应该澄清一下,消息似乎已经成功发送,但是当我运行客户端时,它卡在等待数据上...如果有任何指针可以解释为什么会发生这种情况,将不胜感激! UDP服务器
#include<stdio.h> 
#include<string.h>
#include<stdlib.h> 
#include<arpa/inet.h>
#include<sys/socket.h>

#define SERVER "127.0.0.1" 
#define BUFLEN 512  //Max length of buffer
#define PORT 8888   //The port on which to listen for incoming data

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

int main(void)
{
    struct sockaddr_in si_other;

    int s, i, slen = sizeof(si_other);
    char buf[BUFLEN];
    char message[BUFLEN]; 
    //create a UDP socket
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);

    if (inet_aton(SERVER , &si_other.sin_addr) == 0) 
    {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

    //bind socket to port
    if( bind(s , (struct sockaddr*)&si_other, sizeof(si_other) ) == -1)
    {
        die("bind");
    }
    else
    {
        printf ("Success!\n");
    }   


    while(1)
    {
        printf("Enter message : ");
        gets(message);

        //send the message
        if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
        {
            die("sendto()");
        }
        else
        {
            printf ("Success!\n");
        }

    } 

    close(s);
    return 0;
}

UDP客户端


#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>


#define BUFLEN 512  //Max length of buffer
#define PORT 8888   //The port on which to send data

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

int main(void)
{
    struct sockaddr_in si_me, si_other;
    int s, i, slen=sizeof(si_other), recv_len;
    char buf[BUFLEN];
    char message[BUFLEN];

    if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

    while(1)
    { 
        printf("Waiting for data...");
        fflush(stdout);

        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            die("recvfrom()");
        }

        //print details of the client/peer and the data received
        printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Data: %s\n" , buf);        
    } 

    close(s);
    return 0;
}

int s, i, slen=sizeof(si_other), recv_len;: what is i? Try: socklen_t slen; and slen = sizeof si_other - jhscheer
我已经成功让它工作了... 我需要从客户端而不是服务器调用bind()函数... - user1816546
将其作为答案编写,然后接受它(您可以回答自己的问题)。 - JeremyP
无关的话题,永远永远不要使用gets(),否则你会被热针拿来戳眼睛。 - JeremyP
谢谢你的提示! :) - user1816546
2个回答

1

在客户端调用bind()而不是服务器端。


由于您正在使用SOCK_DGRAM,因此根本不需要调用bind()。http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#sendtorecv - jhscheer
https://dev59.com/ZGoy5IYBdhLWcg3wUcVL - jhscheer
针对我的上述代码,这是我找到的唯一解决方案。如果您能找到另一种解决方法,我会很乐意尝试! - user1816546

1
你需要绑定你的客户端,而不是你的服务器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接