My system is C++ on ARM running Ubuntu.
I am having some problems with Berkley Sockets when I am trying to do a blocking read I get an error and the code is EAGAIN. I put a timer in the code and I find that the error is occurring before the socket timeout. I look at the device that I am connecting too and the socket did not close.
I don't know how this relates but it appears that that this only happens when I am running the program under gdb...at least that is the only time that I have noticed it.
Here is a simplified version of the code. I have taken out the error checking to make it smaller.
int optval;
struct timeval tv;
// Set up the sockaddrIn structures for the port
struct sockaddr_in controlTcpAddr;
memset(&controlTcpAddr, 0, sizeof(controlTcpAddr)); // Clear struct
controlTcpAddr.sin_family = AF_INET; // Internet/IP
controlTcpAddr.sin_addr.s_addr = inet_addr(hostIp); // IP address
controlTcpAddr.sin_port = htons(hostPort); // server port
// Create the TCP socket
myControlTcpSockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
// Establish connection
connect(myControlTcpSockDesc, (struct sockaddr *) &controlTcpAddr, sizeof(controlTcpAddr);
int flags = fcntl(myControlTcpSockDesc, F_GETFL);
int result = fcntl(myControlTcpSockDesc, F_SETFL, flags & ~O_NONBLOCK);
// Set the SO_REUSEADDR option for the socket
optval = 1;
setsockopt(myControlTcpSockDesc, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)
tv.tv_sec = 10; // 10 Secs Timeout
tv.tv_usec = 0;
setsockopt(myControlTcpSockDesc, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
// try a read
cnt = read(myControlTcpSockDesc, myIncomingMsgBuf, MESSAGE_BUFFER_SIZE);
// at this point I find that 10 seconds have not expired
// (I have separate timer running that is not shown)
// cnt = -1
// errno = EAGAIN
Related
I am developing a linux application which communicates with OpenSSL. I am currently running some robustness tests and one of the is giving me a hard time.
I plug out the Ethernet cable when my program is downloading a big file and I wish that it stops after 30seconds for example. But it never stop.
I use SSL_read and this is where it blocks :
count = SSL_read(ssl, buffer, BUFSIZE);
Is it possible to set a timeout to SSL_read ?
I have tried SSL_CTX_set_timeout() but it is not working. I have also seen that it was maybe possible to use select() but I don't understand how to use it with SSL_read()
You can do that in the same way as you do it with "normal" sockets. Basically, you set the timeval on a socket passed to ssl and the SSL_read will return -1 when the time set in timeval passes if nothing is received. Example below (uninteresting parts are written in pseudo):
struct timeval tv;
char buffer[1024];
// socket, bind, listen, ...
// accept
int new_fd = accept(...)
tv.tv_sec = 5; // 5 seconds
tv.tv_usec = 0;
setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
// assign fd to ssl ...
// blocking method will return -1 if nothing is received after 5 seconds
int cnt = SSL_read(ssl, buffer, sizeof buffer);
if (cnt == -1) return; // connection error or timeout
new to multicast networking, I need to receive UDP packets from a multicast channel through one of the NICs on my windows box, followed Microsoft docs and some blog entry, but still having issues.
I create a socket via
ls = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
call.
Then setsockopt to SO_REUSEADDR
unsigned int reuse = 1;
if( setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0 )
{
LOG4CXX_ERROR(logger, "Reusing ADDR failed. Err: " << WSAGetLastError());
}
If socket is good
int result = bind(ls, reinterpret_cast<SOCKADDR*>(&server), sizeof(server));
where
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(myport);
If bind succedes
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("e.f.g.h");
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if( setsockopt(ls, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq)) < 0 )
{
LOG4CXX_ERROR(logger, "setsockopt multicast group add membership failed. Err: " << WSAGetLastError());
}
The box on which I need to receive packets has four NICs, network administrators told me that I have to use the third one, let's say that it has a.b.c.d IPv4 address
They told me also that mcast IP is e.f.g.h
If I run windump.exe -i 3 on my windows box I see something like this
... 12:53:58.454987 IP i.k.l.m.xxxxx > e.f.g.h.myport: UDP, length 58
...
After initializing my UDP socket I call recvfrom
sz = recvfrom(ls, buffer, DATA_BLOCK_SIZE, 0, reinterpret_cast<SOCKADDR*>(&client), &size);
where sz is an int, ls is my socket, buffer is a "suitable buffer", DATA_BLOCK_SIZE is buffer size, client is a SOCKADDR pointer to receive info from the sender, and size is the received message size.
My code stucks in the recvfrom call never receiving anything.
I'm clearly making a mistake somewhere but not understanding where and worse why.
If someone can explain me what's happening it will be very appreciated.
SOLVED ...
I changed these lines only
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("e.f.g.h");
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
to
struct ip_mreq mreq;
inet_pton(AF_INET, "e.f.g.h", &(mreq.imr_multiaddr));
inet_pton(AF_INET, "a.b.c.d", &(mreq.imr_interface));
As I have guessed I was not correctly indicating in my struct ip_mreq which was the network interface to use for multicast messages.
It was my fault. Sorry for the noise.
I am trying to set a 100ms timeout on a UDP Socket. I am using C. I have posted relavent pieces of my code below. I am not sure why this is not timing out, but just hangs when it doesn't receive a segment. Does this only work on sockets that are not bound using the bind() method?
#define TIMEOUT_MS 100 /* Seconds between retransmits */
if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
//set timer for recv_socket
static int timeout = TIMEOUT_MS;
setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(timeout));
if(recvfrom(rcv_sock, ackBuffer,sizeof(ackBuffer), 0,
(struct sockaddr *) &servAddr2, &fromSize) < 0){
//timeout reached
printf("Timout reached. Resending segment %d\n", seq_num);
num_timeouts++;
}
The SO_RCVTIMEO option expects a struct timeval defined in sys/time.h, not an integer like you're passing to it. The timeval struct has as field for seconds and a field for microseconds. To set the timeout to 100ms, the following should do the trick:
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000;
if (setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
perror("Error");
}
I have the same problem. I tried to adopt the solution you suggested, using the timeval struct. But it did not seem to work.
I have read on the Microsoft documentation and the time should be a DWORD with the number of milliseconds, but there is also another thing to do, If the socket is created using the WSASocket function, then the dwFlags parameter must have the WSA_FLAG_OVERLAPPED attribute set for the timeout to function properly.
Otherwise the timeout never takes effect.
I started to lock into socket programming an got into a little trouble:
I created the small program below which sends a message via udp an receives one if possible in a loop. I want to try that with multiple sockets later on, so I use select().
When I use my 127.0.0.1, select() gives a timeout in the first loop (after send()) but after that it always returns 1 indicating that the socket is readable without receiving a message:
//C++
WSADATA wsa;
SOCKADDR_IN RemoteAddr;
SOCKADDR_IN OwnAddr;
SOCKET UDP_Socket1;
fd_set m_Fds;
struct timeval m_Timeout;
int iRemoteAddrLenght = sizeof(SOCKADDR_IN);
int i = 0;
//--Init
WSAStartup (MAKEWORD (2,2), &wsa);
UDP_Socket1 = socket(AF_INET, SOCK_DGRAM, 0);
m_Timeout.tv_sec = 2;
m_Timeout.tv_usec = 0;
RemoteAddr.sin_family = AF_INET;
RemoteAddr.sin_port = htons (2002);
RemoteAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
OwnAddr.sin_family = AF_INET;
OwnAddr.sin_port = htons (2003);
OwnAddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(UDP_Socket1, (SOCKADDR*) &OwnAddr, sizeof(OwnAddr));
for(;;)
{
//..//
//--send
sendto(UDP_Socket1, sSend.c_str(), strlen(sSend.c_str()), 0 , (SOCKADDR*)&RemoteAddr, sizeof(RemoteAddr));
//--select & recv
FD_ZERO(&m_Fds);
FD_SET(UDP_Socket1,&m_Fds);
i = select(sizeof(m_Fds)*8, &m_Fds, NULL, NULL, &m_Timeout);
if(i > 0)
{
recvfrom(UDP_Socket1, m_szBuff, 256, 0, (SOCKADDR*) &RemoteAddr, &m_iRemoteAddrLenght);
} //if
else if(i < 1) // "0" in 1st loop, then "1" =(
{
cout << "Udp Timeout" << endl;
} //else if
} //for
send returns 56 (bytes send)
with WSAGetLastError: 0
recvfrom returns -1
with WSAGetLastError: 10054
I'd appreciate your help about why select() returns 1 when it should timeout
You can ignore these kinds of errors for UDP. Some operating systems report them, some don't. They're basically meaningless.
I have a connection protocol that has been defined by our customer. Data are sent between two linux computers using UDP and TCP protocols. The IP addresses and ports are fixed on startup.
We are sending messages at 200 Hz and I have been using connect to save some time on the transmissions.
My problem is that if there is a communication error, I need to tear down the connections and reinitialise.
I have a problem with one of the UDP connections as it will not rebind to the required address and returns errno 22.
The code I am using is something like:
int
doConnect(int& sock, int local_port, char *local_ip, int remote_port, char *remote_ip)
{
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in);
addr.sin_family = AF_INET;
addr.sin_port = htons(local_port);
inet_pton(local_ip,&addr.sin_addr.s_addr);
if (0 > bind(sock, (struct sockaddr*)&addr, sizeof(addr)))
{
printf("Bind Error errno = %d\n", errno);
return ERR_BIND;
}
memset(&addr, 0, sizeof(sockaddr_in);
addr.sin_family = AF_INET;
addr.sin_port = htons(remote_port);
inet_pton(remote_ip,&addr.sin_addr.s_addr);
if (0 > connect(sock, (struct sockaddr*)&addr, sizeof(addr)))
{
printf("Connect Error errno = %d\n", errno);
return ERR_CONNECT;
}
return ERR_OK;
}
The way that this is used is like this:
int s1(-1), s2(-1);
doConnect(s1, 31003, "172.17.21.255", 31006, "172.17.21.1");
doConnect(s2, 31001, "172.17.21.3", 31004, "172.17.21.1");
When an error occurs
close(s1);
close(s2);
doConnect(s1, 31003, "172.17.21.255", 31006, "172.17.21.1");
doConnect(s2, 31001, "172.17.21.3", 31004, "172.17.21.1");
Here the local address is 172.17.21.3 and I am connecting to 172.17.21.1. s1 listens to a broadcast message.
s1 successfully reconnects to the remote machine, but s2 fails with error 22 from the call to bind.
I have tried explicitly calling bind and connect to an AF_UNSPEC address immediately before I close the socket. This doesn't solve the problem.
Are there any options that I should be using?
Perhaps you could try:
int val = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
I also suggest you double check that you're not passing the same socket to the two consecutive doConnect() calls (as errno 22 = EINVAL, which in the case of bind() appears to mean that the socket is already bound to an address).
The underlying socket layer might hold the port & IP address still open, even after your call to close. Try some of the following:
do a sleep(10) (or more) between the close and the call to doConnect again
configure the sockets using setsockopt with the SO_LINGER set to off
This actually happens more commonly with TCP connections, but I see no reason UDP can't have this problem as well.