select for timeout over TCP socket - sockets

I have a problem with this code:
FD_ZERO(&cset);
FD_SET(s, &cset);
tval.tv_sec = TIMEOUT;
tval.tv_usec = 0;
n = select(FD_SETSIZE, &cset, NULL, NULL, &tval);
if (n==-1) {
printf(" select() failed \n");
exit(-1);
}
if (n>0) {
check_control = connect(s,(struct sockaddr*)
&indirizzo_remoto,sizeof(indirizzo_remoto));
if (check_control == -1) {
printf("Errore connect()\n");
}
}else{
printf("Timeout. I'll shutdown the client");
exit(-1);
}
I want insert a timeout for the connect but it doesn't work:
I use the right IP address and port number of the Server but the connection goes to timeout.
Thank you very much for the help.

You are using select() to check if a given socket is in a readable state before calling connect() on that same socket. That will never work. An unconnected TCP socket will never be in a readable state, and cannot be used with select() until connect() has been called on it first.
The only way to implement a timeout for a connect() call is to put the socket into non-blocking mode first (sockets are blocking by default), then call connect() (which returns an EINPROGRESS error if the socket is attempting to connect), and then use select() to wait for the socket to enter a writable state, indicating the connection was successful, or an error state, indicating the connection failed.
Try this:
fcntl(s, F_SETFL, O_NONBLOCK);
Or:
flags = 1;
ioctl(s, FIOBIO, &flags);
Depending on your platform.
Then:
check_control = connect(s, (struct sockaddr*) &indirizzo_remoto, sizeof(indirizzo_remoto));
if (check_control == -1)
{
if (errno != EINPROGRESS)
{
printf("Errore connect()\n");
exit(-1);
}
FD_ZERO(&wset);
FD_SET(s, &wset);
FD_ZERO(&eset);
FD_SET(s, &eset);
tval.tv_sec = TIMEOUT;
tval.tv_usec = 0;
n = select(s+1, NULL, &wset, &eset, &tval);
if (n == -1)
{
printf(" select() failed \n");
exit(-1);
}
if (n == 0)
{
printf("Timeout. I'll shutdown the client");
exit(-1);
}
if (FD_ISSET(s, &eset))
{
printf("Cannot connect. I'll shutdown the client");
exit(-1);
}
int err = -1;
getsockopt(s, SOL_SOCKET, SO_ERROR, &err, sizeof(err));
if (err != 0)
{
printf("Cannot connect. I'll shutdown the client");
exit(-1);
}
}
// connected...

Related

socket connection lost using select function

I'm newbee in socket program.
I made my server program with good sample program using select function.
It works well about 20,000 connections over.
But, in some case, connection accept twice consequence without
receive data from first socket.
Only data received from second socket connection.
After that, first socket resource cannot release.
FD_SET and FD_ISSET are not working with first socket in case of consequence accept I think.
Working clients are 6.
Before this situation,
accept, receive data, and close socket, accept, rcv
data, close, ...
In case, accept,
accept, receive data from second socket, and close second socket.
Lost first socket connection.
After that, accept function assign second socket descriptor.
What is problem?
How can release fisrt socket?
BR
Paul
My code is as follow:
while(1)
{
//clear the socket set
FD_ZERO (&readfds);
//add master socket to set
FD_SET (sever_socket, &readfds);
max_sd = sever_socket;
//add child sockets to set
for ( i = 0 ; i < MAX_CLIENT ; i ++)
{
//socket descriptor
sd = client_socket [i];
//if valid socket descriptor then add to read list
if (sd > 0)
{
FD_SET( sd , &readfds);
}
//highest file descriptor number, need it for the select function
if(sd > max_sd)
{
max_sd = sd;
}
}
//wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
activity = select ( max_sd + 1 , &readfds , NULL , NULL , NULL);
if ((activity < 0) && (errno!=EINTR))
{
LOG_F (WARNING, "select error");
}
//If something happened on the master socket, then its an incoming connection
if (FD_ISSET(sever_socket, &readfds))
{
if ((new_socket = accept (sever_socket, (struct sockaddr *) &address, (socklen_t*) &addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
LOG_F (INFO, "New connection, socket fd is %d, ip is : %s, port : %d",
new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
//add new socket to array of sockets
for (i = 0; i < MAX_CLIENT; i++)
{
//if position is empty
if( client_socket[i] == 0 )
{
client_socket[i] = new_socket;
LOG_F (INFO, "Adding to list of sockets as %d" , i);
break;
}
}
}
for (i = 0; i < MAX_CLIENT; i++)
{
sd = client_socket[i];
if (FD_ISSET (sd , &readfds))
{
memset (&rcvBuf, 0x00, sizeof(rcvBuf));
if ((fp = fdopen (sd, "r")) == NULL)
{
LOG_F (WARNING, "TCP_SOCKET FD_OPEN Error");
close (sd);
client_socket[i] = 0;
}
else
{
ret = ioctl (sd, FIONREAD, &nread);
if (nread == 0)
{
fclose (fp);
close (sd);
client_socket[i] = 0;
LOG_F (WARNING, "Client disconnected(as %d, fd %d)", i, sd);
}
else
{
len = recv (sd, rcvBuf, nread, 0);
if (len > 0)
{
LOG_F (INFO, "RECV size %d" , len);
...
do_msg_handler ()
}
}
...

How to use select() function for both TCP & UDP connection?

I have a functional server code with only TCP connection. Now I want the server to receive from UDP connection. I have used port 2000 for TCP & port 2001 for UDP. Here is a snippet of my code
struct timeval timeout; // timeout for select(), 1ms
timeout.tv_sec = 0;
timeout.tv_usec = 1000;
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
// TCP port setup
int sockfd; // listening socket descriptor
int newsockfd; // newly accept()ed socket descriptor
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;
char buf_tcp[256]; // buffer for client data
char buf_copy_tcp[256];
int recv_bytes;
char remoteIP[INET6_ADDRSTRLEN];
int yes=1; // for setsockopt() SO_REUSEADDR
int i, k, rv_getaddrinfo, rv_setsockopt, rv_bind, rv_listen, rv_select;
struct addrinfo hints, *servinfo, *ptr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
rv_getaddrinfo = getaddrinfo(NULL, "2000", &hints, &servinfo);
for(ptr=servinfo; ptr!=NULL; ptr=ptr->ai_next)
{
sockfd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
rv_setsockopt = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
rv_bind = bind(sockfd, ptr->ai_addr, ptr->ai_addrlen);
break;
}
if (ptr == NULL)
{
fprintf(stderr, "CLI Server error: failed to bind\n\r");
exit(2);
}
freeaddrinfo(servinfo); // all done with this
rv_listen = listen(sockfd, 10);
////////////////////////////////////////////////////////////////////////////////////////////////
// UDP port setup
int sockfd_udp; // listening socket descriptor
struct sockaddr_storage remoteaddr_udp; // client address
socklen_t addrlen_udp;
char buf_udp[256]; // buffer for client data
char buf_copy_udp[256];
int recv_bytes_udp;
char remoteIP_udp[INET6_ADDRSTRLEN];
int yes_udp=1; // for setsockopt() SO_REUSEADDR
int j, rv_getaddrinfo_udp, rv_setsockopt_udp, rv_bind_udp;
struct addrinfo hints_udp, *servinfo_udp, *ptr_udp;
memset(&hints_udp, 0, sizeof(hints_udp));
hints_udp.ai_family = AF_UNSPEC;
hints_udp.ai_socktype = SOCK_DGRAM;
hints_udp.ai_flags = AI_PASSIVE;
rv_getaddrinfo_udp = getaddrinfo(NULL, "2001", &hints_udp, &servinfo_udp);
for(ptr_udp=servinfo_udp; ptr_udp!=NULL; ptr_udp=ptr_udp->ai_next)
{
sockfd_udp = socket(ptr_udp->ai_family, ptr_udp->ai_socktype, ptr_udp->ai_protocol);
rv_setsockopt_udp = setsockopt(sockfd_udp, SOL_SOCKET, SO_REUSEADDR, &yes_udp, sizeof(int));
rv_bind_udp = bind(sockfd_udp, ptr_udp->ai_addr, ptr_udp->ai_addrlen);
break;
}
if (ptr_udp == NULL)
{
fprintf(stderr, "CLI UDP Server error: failed to bind\n\r");
exit(2);
}
freeaddrinfo(servinfo_udp); // all done with this
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// add the listener to the master set
FD_SET(sockfd, &master);
FD_SET(sockfd_udp, &master);
// keep track of the biggest file descriptor
if(sockfd > sockfd_udp)
fdmax = sockfd; // so far, it's this one
else
fdmax = sockfd_udp; // so far, it's this one
do
{
read_fds = master; // copy it
rv_select = select(fdmax+1, &read_fds, NULL, NULL, &timeout);
// run through the existing connections looking for data to read
for(i=0; i<=fdmax; i++)
{
if (FD_ISSET(i, &read_fds))
{ // we got one!!
if (i == sockfd)
{
// handle new connections
addrlen = sizeof(remoteaddr);
newsockfd = accept(sockfd, (struct sockaddr *)&remoteaddr, &addrlen);
FD_SET(newsockfd, &master); // add to master set
if (newsockfd > fdmax) // keep track of the max
fdmax = newsockfd;
inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN);
fprintf(stdout, "CLI Server: new connection from %s on socket %d\n\r", remoteIP, newsockfd);
}
else if (i == sockfd_udp)
{
// handle new udp connections
addrlen_udp = sizeof(remoteaddr_udp);
recv_bytes_udp = recvfrom(i, buf_udp, sizeof(buf_udp), 0, (struct sockaddr *)&remoteaddr_udp, &addrlen_udp);
inet_ntop(remoteaddr_udp.ss_family, get_in_addr((struct sockaddr*)&remoteaddr_udp), remoteIP_udp, INET6_ADDRSTRLEN);
for(j=0; j<=recv_bytes_udp; j++)
{
if( (buf_udp[k] == '\r') | (buf_udp[k] == '\n') )
buf_udp[k] = '\0';
}
fprintf(stdout, "CLI UDP Server: received %s from connection %s\n\r", buf_udp, remoteIP_udp);
}
else
{ // handle data from a client
if ((recv_bytes = recv(i, buf_tcp, sizeof(buf_tcp), 0)) <= 0)
{ // got error or connection closed by client
if (recv_bytes == 0) // connection closed
{
fprintf(stdout, "CLI Server: socket %d hung up\n\r", i);
}
else
{
perror("CLI Server error: recv");
exit(6);
}
close(i); // bye!
FD_CLR(i, &master); // remove from master set
}
else
{
for(k=0; k<=recv_bytes; k++)
{
if( (buf_tcp[k] == '\r') | (buf_tcp[k] == '\n') )
buf_tcp[k] = '\0';
}
fprintf(stdout, "CLI Server: received %s from socket %d\n\r", buf_tcp, i);
}
} // END handle data from client
} // END got new incoming connection
} // END looping through file descriptors
} while(QUIT);
I am doing error checking at each stage but didn't include it in the snippet. When I compile & run this, I can connect to port 2000 but not to 2001, my Tera term terminal closes with connection refused message. Why is the client not able to connect to port 2001 (UDP socket) but connects to port 2000 (TCP socket). The server just responds to client messages until client enters QUIT.
I have modeled this code from Beej's Guide to Network Programming selectserver.c code.
The program that I wrote above was correct but my understanding of socket programming was not. It was a fluke that I managed to write correct code but thanks to #EJP for having an extended discussion in the comments to clear up my doubt.
My mistake was using Teraterm's TCP client to connect to a UDP server. Both communications are mutually exclusive & hence can't communicate with one another. So I had to use a UDP client. Netcat provides an option of UDP client using netcat -u <ip address> <port>. Then my UDP server was able to receive messages from UDP client.
Another mistake was confusing bind() with connect() in DATAGRAM sockets. A connected DGRAM is when I use connect on both server & client.
I thought the problem was with select(), as I wrongly thought that UDP & TCP sockets can't be used simultaneously in select(). But the above code is how you write a UDP/TCP server for multiple client.
Thanks again to Beej & #EJP

WinSock Error 10061

I have written a simple client/server application using winsock. The server and client connect and communicate over TCP port 76567 (just a random number I chose) on the localhost. I've tested it on three desktops, two running XP and the other running Win7, I've also tested it on four laptops, three running Win7 and one running XP. The application works fine on all the desktop machines and on the XP laptop, but on all three Win7 laptops I get Error 10061 when the client tries to connect to the server!
I've turned off the firewall but the problem persists, I've also looked around to see what causes this error and it looks like the client is trying to connect to a non-listening server. However, the server call to listen() returns succesfully! It's very odd that the problem only seems to happen on Win7 laptops, any ideas?
Here's my socket initialisation code:
// Initialise Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
}
// Create a server socket
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if(iResult != 0)
{
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
}
// Create a socket to listen for clients
listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if(listenSocket == INVALID_SOCKET)
{
printf("Error at socket(): %d\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
}
// Bind socket to ip address and port
iResult = bind(listenSocket, result->ai_addr, (int) result->ai_addrlen);
if(iResult == SOCKET_ERROR)
{
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(listenSocket);
WSACleanup();
}
freeaddrinfo(result);
// Listen for connection requests
if(listen(listenSocket, SOMAXCONN) != 0)
{
printf("Listen failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
}
Many thanks :)
IP port is 16-bit integer, therefore the maximum allowed port number is 0xFFFF (65535). What happens here is kind of integer overflow. Since your desired port number (76567) does not fit into 16 bits, the number is truncated and only lowest 16 bits are used. This gives you port number 11031. The line addr.sin_port = htons(76567); should give you a compiler warning since argument to htons() cannot fin into uint16_t.
getaddrinfo() returns a linked list of all available addresses for the given hints criteria. Even if the machine only has one network adapter, it could have multiple IP addresses assigned to it, even for localhost. You are binding the server socket to the first IP/Port pair that getaddrinfo() found, so it is possible that the client could be trying to connect to a different IP/Port that really is not listening on the server, for instance if the server bound to your LAN/Internet IP but the client is connecting to 127.0.0.1 instead. A client cannot connect to 127.0.0.1 unless the server is bound to 127.0.0.1.
In a multi-homed/multi-IP environment, you should use the wildcard 0.0.0.0 IP (aka INADDR_ANY) when calling bind(), instead of result->ai_addr. That will bind the socket to all available IPs of all installed network adapters. That way, the client can connect to any IP the server is bound to, including 127.0.0.1. In fact, given the code you have shown, you don't even need to use getaddrinfo() at all:
// Initialise Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
}
// Create an IPv4 server socket to listen for IPv4 clients
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listenSocket == INVALID_SOCKET)
{
printf("Error at socket(): %d\n", WSAGetLastError());
WSACleanup();
}
// Bind socket to IPv4 address and port
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(76567);
addr.sin_addr.s_addr = INADDR_ANY;
iResult = bind(listenSocket, (sockaddr*)&addr, sizeof(addr));
if(iResult == SOCKET_ERROR)
{
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
}
// Listen for IPv4 connection requests
if(listen(listenSocket, SOMAXCONN) != 0)
{
printf("Listen failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
}

Can anyone guide me or suggest how to make a client for live media server streaming over HTTP

Can anyone guide me or suggest how to make a client using sockets for live media server streaming over HTTP, as I tried a lot but was not successful.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
//************All Declarations**********//
#define DEFAULT_BUFLEN 512000
#define DEFAULT_PORT "8000"
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Validate the parameters
if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
getch();
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
getch();
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
getch();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
getch();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
getch();
return 1;
}
// Send an initial buffer
/*iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
getch();
return 1;
}*/
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
getch();
return 1;
}
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while( iResult > 0 );
// cleanup
getch();
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
I tried the above code, it works well with a server I create. The problem is I am streaming an transport stream file from live media server through http,but when I try to receive the data from my code of client I am able to connect at specific url but could not receive anything.
The easiest solution for you would be to use the openRTSP client from liveMedia as a starting point. It pretty much comes down to implementing the entire RTSP stack as well interleaving the media over the TCP connection. By passing in the "-T " as a command line parameter, you can configure openRTSP to stream over RTSP over TCP. You can then write your own application based on openRTSP in which you can then handle incoming media samples as desired.
I would advise you against implementing this functionality yourself from a socket-level. You would need to implement RTSP, RTP, RTCP, RTSP over HTTP tunneling, SDP, the various RTP payload formats e.g. for H.264. You socket-related code segment above doesn't begin to touch the surface.
If you want to see what the protocol exchange looks like, sniff the traffic from openRTSP to an RTSP server using wireshark. You can also find an RTSP server at liveMedia.
If it's over HTTP you first have to send an HTTP request (and then parse the response). Example.
You don't receive anything because the server is waiting for the request.

SO_REUSEADDR with UDP datagrams - Resource unavailable

I'm using SO_REUSEADDR option, but I'm not sure why am getting
Resource temporary unvailable option.
I'm testing client server code on 127.0.0.1
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket() error!!\n");
exit(1);
}
if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) {
perror("SO_REUSEADDR failed::");
exit(1);
}
while(1) {
nbytes_read = recvfrom(sockfd, (void *)&recvd_msg, sizeof(recvd_msg),
flags, &from, &from_len);
printf("nbytes_read = %d\n", nbytes_read);
if(nbytes_read == -1) {
perror("client: recvfrom() failed");
return FAILED;
}
if (nbytes_read > 0) {
if(recvd_msg.hdr.msgtype == DATA)
printf("recvd %d bytes from server\n", recvd_msg.hdr.payload_size);
ftp_show_payload(&recvd_msg);
}
if(recvd_msg.hdr.is_last == TRUE) {
break;
}
}
Error message:
" client: recvfrom() failed: Resource temporarily unavailable"
errno:11
After trying to run client for 3-4 times, I get the data, I'm not sure whats happening.
Also, this problem is on Ubuntu Linux, but when I run the same client server on Solaris,
it works fine!!
SO_REUSEADDR is useful when you use bind(), but here you are not using bind.
I dont see any problem if recvfrom() returns -1
Use bind() and replace your call recvfrom() with recv(). recv() will receive all the packets at the port you used in your bind call.
Are you trimming out any other socket configuration? EAGAIN is typically returned when you read a non-blocking socket and there's no data available. The manpage for recvfrom lists the possible errnos that will be set on failure with an explanation for each one.
Your test is invalid. recvfrom() can return zero, which doesn't indicate an error. It is only valid to call perror() if you get -1. So you may not have a problem at all ..
I don't see why you're using SO_REUSEADDR at all here, as you're not binding to a specific port.