独步喜欢 2019-11-22 23:42 采纳率: 0%
浏览 467
已结题

linux c写了一个UDP服务器,然后通过网络助手做作为客户端,UDP无法接收到数据

#include

#include

#include

#include

#include

#include

#include

#include

#include

//#include

char* PORT="5588"; //server's port

char* SOCKET_SERVER_IP="192.168.1.105"; //

int main()

{

printf("Welcome! This is a UDP server, I can only received message from client and reply with same message\n");



struct sockaddr_in addr;

addr.sin_family = AF_INET;

addr.sin_port = htons(atoi(PORT));

addr.sin_addr.s_addr = htonl(INADDR_ANY);



int sock;

if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)

{

    perror("socket");

    exit(1);

}

//port bind to server

if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)

{

    perror("bind");

    exit(1);

}

char buff[512];

struct sockaddr_in clientAddr;

memset(&clientAddr,0,sizeof(clientAddr));

size_t n;

socklen_t len = sizeof(clientAddr);

while (1)

{

    n = recvfrom(sock, buff, 511, 0, (struct sockaddr*)&clientAddr, &len);

    if (n>0)

    {

        buff[n] = 0;

        printf("%s %u says: %s\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), buff);

        n = sendto(sock, buff, n, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));

        if (n < 0)

        {

            perror("sendto");

            break;

        }

    }

    else

    {

        perror("recv");

        break;

    }

}

return 0;

}

然后网络助手设置如下,防火墙那些都关闭了。图片说明图片说明图片说明

很奇怪,为何服务器会收不到网络助手发的数据,求解啊。卡了2天了

  • 写回答

1条回答 默认 最新

  • J4cks0n 2019-11-23 09:47
    关注

    你的服务器是192.168.1.105啊 你远程主机填的是192.168.1.123啊

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿