gpt4 book ai didi

c - 原始套接字 : sendto() and recvfrom() not working

转载 作者:行者123 更新时间:2023-11-30 19:40:35 25 4
gpt4 key购买 nike

我正在尝试使用 RAW 套接字编写客户端/服务器应用程序。

存在多个问题:

  1. 当客户端使用sendto()方法向服务器发送消息时,sendto()返回错误invalid argument em> 方法。 为什么会出现此错误消息?。相应的代码标记在错误1部分下。 sendto() 的代码在这篇文章中进行了注释。

  2. 由于我已经注释了发送消息部分,因此客户端应该等待消息; recvfrom() 是一个阻塞系统调用。相反,recvfrom() 总是返回一条消息E此消息从哪里到达?。相应的代码标记为ERROR 2

  3. 如果我将socket()中的协议(protocol)(第三个)参数更改为0IPPROTO_RAW我收到协议(protocol)不支持的错误。 为什么会出现这些错误?

操作系统是Ubuntu

    #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/socket.h> // For the socket () etc. functions.
#include <netinet/in.h> // For IPv4 data struct..
#include <string.h> // For memset.
#include <arpa/inet.h> // For inet_pton ().

#define BUF_SIZE 30

void main ()

{
int rst; // Return status of functions.
/**************** Create a socket. *******************************/
int sfd; // Socket file descriptor.
sfd = socket (AF_INET, SOCK_RAW, IPPROTO_UDP); /*
* AF_INET --> IPv4, SOCK_RAW for Raw socket,
* 0 --> for any protocol. */
if (sfd == -1)
{
perror ("Client: socket error");
exit (1);
}


/*********** Server's address ***********************************/
struct sockaddr_in srv_addr;
socklen_t addrlen = sizeof (struct sockaddr_in);

// Initializing the server's address to zero.
memset (&srv_addr, 0, addrlen);

srv_addr.sin_family = AF_INET; // Address is in IPv4 format.
// srv_addr.sin_port = htons (0); // Port number of the server.


rst = inet_pton (AF_INET, "127.0.0.1", &srv_addr.sin_addr); /* Note
* that third field should point to an in_addr (in6_addr). */
if (rst <= 0)
{
perror ("Client Presentation to network address conversion.\n");
exit (1);
}



/****************** ERROR 1 ************************************
******************* Sending message to the server. *************/
const int flags = 0;
const char *msg = "Hello";
/* rst = sendto (sfd, msg, strlen(msg)+1, flags,
(struct sockaddr *) &srv_addr,
sizeof (struct sockaddr_in));
if (rst < 0)
{
perror ("Client: Sendto function call failed");
exit (1);
}
else
printf ("Client: Sent data size = %d\n", rst);

*/


/******************* ERROR 2 ***********************************
******************* Receiving message from server. ************/
// Initializing the server's address to zero.
memset (&srv_addr, 0, addrlen);

char buf[BUF_SIZE] = {'\0'};

rst = recvfrom (sfd, buf, BUF_SIZE, flags,
(struct sockaddr *) &srv_addr,
&addrlen);
if (rst < 0)
{
perror ("Client: couldn't receive");
exit (1);
}
printf ("Message from server = |%s|\n", buf);

/* Address of the server. */
const char *buf2 = inet_ntop (AF_INET,
(struct sockaddr *) &srv_addr, buf, BUF_SIZE);
if (buf2 == NULL)
{
perror ("Client: Conversion of sender's address to presentation failed");
exit (1);
}

printf ("Servers address, = %s\n", buf2);
close (sfd);

}

最佳答案

SOCK_RAW 不适用于 UDP。 SOCK_DGRAM 是正确的。有关教程,请参阅:

a tutorial from Rutgers

关于c - 原始套接字 : sendto() and recvfrom() not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com