Networking 使用UDP进行联网

Networking 使用UDP进行联网,networking,udp,firewall,Networking,Udp,Firewall,使用sendto()将UDP数据包发送到主机时会发生什么情况。 我发送的所有位都被发送(从返回值中知道)。我立即使用recvfrom(),它不输出任何内容,但程序不退出(即没有返回值) 我认为如果没有收到回复,程序必须退出 从端口对UDP数据包的回复将是什么 这个包被防火墙阻止了吗??如果是,那么为什么sendto的返回值是非负的 recvfrom()将阻塞,直到收到消息,除非您将套接字设置为非阻塞 您要查找的接口是 ioctl()与FIONBIO或O_NONBLOCK(取决于您的平台) se

使用sendto()将UDP数据包发送到主机时会发生什么情况。 我发送的所有位都被发送(从返回值中知道)。我立即使用recvfrom(),它不输出任何内容,但程序不退出(即没有返回值)

我认为如果没有收到回复,程序必须退出

从端口对UDP数据包的回复将是什么

这个包被防火墙阻止了吗??如果是,那么为什么sendto的返回值是非负的

recvfrom()
将阻塞,直到收到消息,除非您将套接字设置为非阻塞

您要查找的接口是

  • ioctl()
    FIONBIO
    O_NONBLOCK
    (取决于您的平台)
  • select()
    等待数据到达,或等待一段时间后超时

还要记住,
sendto()
的地址和端口号通常是网络字节顺序,因此查看
ntohl
ntohs
您的客户端或服务器中一定有错误。首先尝试本地主机,这样可以避免防火墙问题

这是我用于测试的非阻塞udp客户端/服务器的一个示例,它使用ioctl()检查套接字上是否有要读取的数据,但是如果您想使用epoll执行一些重要的应用程序,那么使用epoll将更为有效,您还可以指定以微秒为单位的等待超时:

[null@localhost tests]$ cat udpserv.c 
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>

#define BUFLEN 512
#define NPACK 15
#define PORT 9930

void diep(char *s)
{
  printf("erno=%d errstr=%s\n",errno,strerror(errno));
  perror(s);
  exit(1);
}

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

  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    diep("socket");

  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);
  if (bind(s,  (struct sockaddr*) &si_me, sizeof(si_me))==-1)
      diep("bind");

  fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
  sleep(10);  
  for (i=0; i<NPACK; i++) {
    ret=ioctl(s,FIONREAD,&nbytes);
    if (ret==-1) {
        printf("error on FIONREAD\n");
    } else {
        printf("nbytes=%d\n",nbytes);
    }
    if (recvfrom(s, buf, BUFLEN, 0,  (struct sockaddr*) &si_other, &slen)==-1)
      diep("recvfrom()");
    printf("Received first half of packet from %s:%d\nData: %s\n\n", 
           inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
  }

  close(s);
  return 0;

}



[null@localhost tests]$ cat udpclient.c 
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define SRV_IP "127.0.0.1"
#define BUFLEN 200
#define NPACK 10
#define PORT 9930

/* diep(), #includes and #defines like in the server */
void diep(char *s)
{
  perror(s);
  exit(1);
}

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

  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    diep("socket");

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

 for (i=0; i<NPACK; i++) {
    printf("Sending packet %d\n", i);
    sprintf(buf, "This is packet %d\n", i);
    if (sendto(s, buf, BUFLEN, 0, (struct sockaddr*) &si_other, slen)==-1)
      diep("sendto()");
 }

 close(s);
 return 0;
}
[null@localhost测试]$cat udpserv.c
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义BUFLEN 512
#定义NPACK 15
#定义端口9930
无效diep(字符*s)
{
printf(“erno=%d errstr=%s\n”,errno,strerror(errno));
佩罗尔(s);
出口(1);
}
内部主(空)
{
结构sockaddr_在si_me中,si_other;
int s,ret,nbytes,i,slen=sizeof(其他);
char-buf[BUFLEN];
if((s=套接字(AF_INET,SOCK_DGRAM,IPPROTO_UDP))=-1)
diep(“插座”);
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(在任何情况下);
if(bind(s,(struct sockaddr*)&si_me,sizeof(si_me))=-1)
diep(“绑定”);
fcntl(s,F_SETFL,fcntl(s,F_GETFL,0)| O_NONBLOCK);
睡眠(10);

对于(i=0;我可以分享更多关于您尝试做什么的信息吗?您是否验证了UDP是您想要使用的协议?