首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UDP的NETworking

使用UDP的NETworking
EN

Stack Overflow用户
提问于 2012-02-11 03:49:56
回答 2查看 206关注 0票数 0

使用sendto()将UDP数据包发送到主机时会发生什么情况。我发送的所有比特都是发送的(从返回值知道),我使用了一个.immediately(),它不输出任何东西,但程序不退出(即没有返回值)。

我认为如果没有收到响应,程序必须退出。

对来自端口的UDP数据包的应答将是什么。

此数据包是否被防火墙拦截??如果是,那么为什么sendto的返回值是非负的。

EN

回答 2

Stack Overflow用户

发布于 2012-02-11 05:58:45

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

您要查找的接口包括

使用FIONBIOO_NONBLOCK (取决于您的平台)进行超时,使用

  • select()等待数据到达,或在一段时间后超时

还要记住,sendto()的地址和端口号通常是网络字节顺序,因此请查看ntohlntohs

票数 0
EN

Stack Overflow用户

发布于 2012-02-12 07:45:48

您的客户端或服务器中一定有一些错误。首先尝试使用localhost,这样可以避免防火墙问题

这是我在测试中使用的非阻塞udp客户端/服务器的一个例子,它使用ioctl()来检查套接字上是否有要读取的数据,但是如果你想使用epoll来做一些严肃的应用程序,效率会更高,你也可以指定以微秒为单位的超时等待:

代码语言:javascript
复制
[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;
}

sendto()是非负的,因为它返回发送的字节数。查看sendto的手册页

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9233908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档