sendto

Socket programming: sendto always fails with errno 22 (EINVAL)

柔情痞子 提交于 2019-12-18 06:41:44
问题 I am always getting no bytes sent, with an errno of 22 (EINVAL, Invalid Argument) with this code. The destination_host is set elsewhere and known to be valid, so I really don't see what is going on. MAXMSGSIZE is 1000. No errors, or warnings. I am compiling with -Wall -Werror -pedantic char *data_rec; u_int data_len; int sockfd; uint16_t *ns; struct sockaddr_in address; struct sockaddr *addr; char *ip; int i; int errno; int bytes_sent; data_len = MAXMSGSIZE; data_rec = malloc(sizeof(char)

Socket programming: sendto always fails with errno 22 (EINVAL)

泪湿孤枕 提交于 2019-12-18 06:40:47
问题 I am always getting no bytes sent, with an errno of 22 (EINVAL, Invalid Argument) with this code. The destination_host is set elsewhere and known to be valid, so I really don't see what is going on. MAXMSGSIZE is 1000. No errors, or warnings. I am compiling with -Wall -Werror -pedantic char *data_rec; u_int data_len; int sockfd; uint16_t *ns; struct sockaddr_in address; struct sockaddr *addr; char *ip; int i; int errno; int bytes_sent; data_len = MAXMSGSIZE; data_rec = malloc(sizeof(char)

UDP 套接字 recvfrom & sendto

时光总嘲笑我的痴心妄想 提交于 2019-12-17 03:06:46
UDP编程中,服务器不接受来自客户端的连接,只管调用recvfrom函数,阻塞等待客户端的连接。 UDP都是以数据报的形式进行发送和接收的,而TCP是以数据流的形式进行发送和接收的。 客户服务流程结构如下: recvfrom和sendto函数 函数定义: int sendto (int s, const void *buf, int len, unsigned int flags, const struct sockaddr *to, int tolen); int recvfrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); 函数说明: sendto(),是把UDP数据报发给指定地址; recvfrom()是从指定地址接收UDP数据报。 参数: s: socket描述符。 buf: UDP数据报 发送/接收 缓存地址。 len: UDP数据报长度。 flags: 该参数一般为0。 to: sendto()函数参数,struct sockaddr_in类型,指明UDP数据发往哪里报,手动填充对端数据。 tolen: 对方地址长度,一般为:sizeof(struct sockaddr_in)。 from: recvfrom()函数参数,struct

Sockets sendto() returning EINVAL

自作多情 提交于 2019-12-14 03:42:27
问题 I'm trying to send a UDP packet in C. I have the following sendto() : char* msg = "Hello"; //ret is the return value of getaddrinfo, the address is AF_INET (IPv4) //and the sock_type is SOCK_DGRAM (UDP) struct sockaddr_in *ip = (struct sockaddr_in *)ret->ai_addr; if ((sendto(sock, msg, strlen(msg), 0, (struct sockaddr *)ip, sizeof(struct sockaddr *))) != -1) { printf("msg sent successfully"); } else { printf("Error sending msg: %s\n", strerror(errno)); } However, it's returning an error

How to force client in UDP to open port when sending with sendto

僤鯓⒐⒋嵵緔 提交于 2019-12-13 00:17:45
问题 I have simple server and client in UDP (WinSocks/C++). I send datagram client -> server via sendto, and reply from server to client using the ip and port obtained from recvfrom function. I found out that: Every sendto from client is being sent from different port When trying to reply from server Windows returns WSAECONNRESET (which mean that port is closed - http://support.microsoft.com/kb/263823) How can I properly answer client from server (ie force port binding on client when sending using

How to Send a structure using sendto()

旧巷老猫 提交于 2019-12-12 19:03:28
问题 I have created structure : struct buffer { string ProjectName ; string ProjectID ; } buffer buf; buf.ProjectID = "212"; buf.ProjectName = "MyProj"; Now to send this structure using sendto method , I am typecasting the strucure and sending it back as below: char *sendbuf = (char*)&buf; sentbytes = sendto(sock,sendbuf,strlen(sendbuf),0,(sockaddr*)&their_addr,sizeof(their_addr)); But while I am casting my Struct ti char* the actual data is loosing it's values and while debugging I see sendbuf is

TCP messages getting coalesced

霸气de小男生 提交于 2019-12-11 10:54:12
问题 I have a Java application that is writing to the network. It is writing messages in the region of 764b, +/- 5b. A pcap shows that the stream is getting IP fragmented and we can't explain this. Linux 2.6.18-238.1.1.el5 A strace shows: ( strace -vvvv -f -tt -o strace.out -e trace=network -p $PID ) 1: 2045 12:48:23.984173 sendto(45, "\0\0\0\0\0\0\2\374\0\0\0\0\0\3\n\0\0\0\0\3upd\365myData"..., 764, 0, NULL, 0) = 764 2: 15206 12:48:23.984706 sendto(131, "\0\0\0\0\0\0\2\374\0\0\0\0\0\3\n\0\0\0\0

How to send modified IPv6 packet through RAW socket?

别说谁变了你拦得住时间么 提交于 2019-12-10 18:09:51
问题 I'm trying to send a custom IPv6 header through a RAW socket in C Linux. I already succeded in IPv4 using the IP_HDRINCL socket option, however, there's not an equivalent for IPv6. I found a workaround here suggesting to use socket(AF_INET6, SOCK_RAW, IPPROTO_RAW) to have the same effect as enabling the IP_HDRINCL socket option. The socket is created successfully and I'm not getting any error until I use the sendto function with my modified header. I setup the socket like this: static int

Periodic latency spikes from UDP socket caused by periodic sendto()/recvfrom() delay, C++ for Linux RT-PREEMPT system

那年仲夏 提交于 2019-12-10 11:04:38
问题 I have setup two Raspberry Pis to use UDP sockets, one as the client and one as the server. The kernel has been patched with RT-PREEMPT (4.9.43-rt30+). The client acts as an echo to the server to allow for the calculation of Round-Trip Latency (RTL). At the moment a send frequency of 10Hz is being used on the server side with 2 threads: one for sending the messages to the client and one for receiving the messages from the client. The threads are setup to have a schedule priority of 95 using

ECONNREFUSED errors on UDP sendto

风格不统一 提交于 2019-12-08 06:45:40
问题 I am experiencing some unexplained behavior with an app that is writing UDP data with sendto() to multiple ports (all opened with socket(PF_INET, SOCK_DGRAM, 0) ) for the benefit of a set of client reading processes. These sendto()s occasionally and unpredictably trigger ECONNREFUSED errors. This is happening on a macOS Sierra (10.12) system whose sendto(2) manual page does not even list ECONNREFUSED as a possible error. Interestingly, I have a CentOS7 system (where these errors never occur)