I am new in Socket Programming. I am trying to create a Application that uses Sockets for Communication.
I have doubt in the Receive function because sometime it just hangs in recvfrom function.
I am using select function for polling. It works when Camera is connected but If I remove Camera it doesn't show the Error Message.
My Code for Polling:
FD_ZERO(&m_readFds);
FD_SET(Sock, &m_readFds);
m_timeInterval.tv_usec = 30; //30 Microseconds for Polling
m_socketLength = sizeof(m_cameraInfo);
m_lastBlockId = -1;
while (m_acquiringThreadStatus)
{
FD_CLR(Sock, &m_readFds);
FD_SET(Sock, &m_readFds);
m_receivingStatus = select(Sock + 1, &m_readFds, NULL, NULL, &m_timeInterval);
if (m_receivingStatus < 0)
{
std::cout << "No Data to Receive"<<std::endl;
}
else
{
if ((m_receivedBytes = recvfrom(Sock, m_packetBuffer, RECEIVING_BUFFER_SIZE, 0, (struct sockaddr*)&m_cameraInfo, &m_socketLength)) == SOCKET_ERROR)
{
std::cout << "ERROR" << std::endl;
}
else
{
std::cout<<"Data Received"<<std::endl;
}
}
}
An one more question is that when I continously printing the Data Received Statement after sometime It stop. So how can I increase the size of Socket Receiving Buffer.
Thanks in Advance
Edit
SOCKET m_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_sock == INVALID_SOCKET)
{
// Error
}
else
{
//In the else part bind the socket
}
If you read select()'s documentation, you would see that select() returns -1 on error, 0 on timeout, and > 0 on requested event(s). However, that is not how you are treating the return value. You are treating -1 as timeout and >= 0 as data events. So, you end up calling recvfrom() when there is nothing available to read. If the socket is in blocking mode (its default mode), recvfrom() will block the calling thread until data is actually available.
Try this instead:
m_lastBlockId = -1;
while (m_acquiringThreadStatus)
{
FD_ZERO(&m_readFds);
FD_SET(Sock, &m_readFds);
m_timeInterval.tv_sec = 0;
m_timeInterval.tv_usec = 30; //30 Microseconds for Polling
m_receivingStatus = select(Sock + 1, &m_readFds, NULL, NULL, &m_timeInterval);
if (m_receivingStatus == SOCKET_ERROR)
{
std::cout << "ERROR" << std::endl;
break;
}
if (m_receivingStatus == 0)
{
std::cout << "No Data to Receive" << std::endl;
continue;
}
m_socketLength = sizeof(m_cameraInfo);
if ((m_receivedBytes = recvfrom(Sock, m_packetBuffer, RECEIVING_BUFFER_SIZE, 0, (struct sockaddr*)&m_cameraInfo, &m_socketLength)) == SOCKET_ERROR)
{
std::cout << "ERROR" << std::endl;
break;
}
std::cout << "Data Received" << std::endl;
}
As for the size of the socket's receive buffer, you can set it via setsockopt() using the SO_RCVBUF option, eg:
int bufsize = ...;
setsockopt(Sock, SOL_SOCKET, SO_RCVBUF, (char*)&bufsize, sizeof(bufsize));
Related
I am using windows sockets with c++. In the following call I am trying to reply a message to the socket that just connected.
I tried connecting using a dummy client in c++. It would connect but the recv() would not receive anything.
Then I tried using telnet, it worked instantly, just as i wanted.
SOCKET s = accept(ls, (sockaddr*)&clientSin, &s_len);
if (s == INVALID_SOCKET) {
cerr << "Error in accept call: " << WSAGetLastError();
}
else {
cout << "Connection accepted at , socket no. :" << s << endl;
//adding to list of incoming sockets
inactiveList.push_back(s);
//send message to client requesting credentials
char buff[10];
// the character 'x' is a code to the client to provide the server with the credentials
buff[0] = 'x';
buff[1] = '\0';
//send(s, buff, 2, 0);
if (send(s, "From Vic: ", 10, 0) == INVALID_SOCKET)
{
int errorcode = WSAGetLastError();
cerr << "send to client failed: " << errorcode << endl;
closesocket(s);
continue;
}
Sleep(1000);
if (send(s, "From Vic: ", 10, 0) == INVALID_SOCKET)
{
int errorcode = WSAGetLastError();
cerr << "send to client failed: " << errorcode << endl;
closesocket(s);
continue;
}
}
the recv code is:
tnb = 0;
while ((nb = recv(s, &buff[tnb], LINESZ - tnb, 0)) > 0)
{
tnb += nb;
}
/* If there was an error on the read, report it. */
if (nb < 0)
{
printf("recv failed: %d\n", WSAGetLastError());
return 1;
}
if (tnb == 0)
{
printf("Disconnect on recv");
}
/* Make the response NULL terminated and display it. Using C output */
printf("tnb = %d\n", tnb);
buff[tnb] = '\0';
puts(buff);
Taking all my comments and turning it into an answer.
I suspect your recv loop is continuing forever because you haven't sent enough data to make it break out of the loop.
Change this:
while ((nb = recv(s, &buff[tnb], LINESZ - tnb, 0)) > 0)
{
tnb += nb;
}
To this: (notice that I'm allocating +1 for the array buff)
char buff[LINESZ+1]; // +1 for null terminator
buff[0] = '\0';
tnb = 0;
while (tnb < LINESZ)
{
nb = recv(s, &buff[tnb], LINESZ-tnb, 0);
if (nb < 0)
{
printf("Error on socket: %d\n", (int)WSAGetLastError());
break;
}
else if (nb == 0)
{
printf("Remote socket closed\n");
break;
}
printf("Received: %d bytes\n", (int)nb);
tnb += nb;
buff[tnb] = '\0'; // null terminate the end of the buffer so it will print reliably
}
I made a server socket in C++ for Unix (TCP), that accepted only one client socket. Today, I attempted to make it accept multiple ones. For some reason, it ends up only accepting 1, and it's not receiving messages sent by the client that IS able to connect. I believe that the select function isn't working properly. FYI: I'm new to socket programming, so please understand if it's a stupid mistake.
Here's the code:
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/select.h>
#define PORT 8080
#define MAXCLIENTS 30
int main()
{
//create a socket
int listening = socket(AF_INET, SOCK_STREAM, 0); //(returns int) - makes socket (returns what socket it is in terms of an int)
if(listening < 0) //check if we were able to make a socket!
{
std::cerr << "Can't create socket!" << std::endl;
return -1;
}
//bind the socket to an IP/Port
sockaddr_in hint{}; // This creates a structure for the ipv4 info of the socket.
hint.sin_family = AF_INET; //set the family to ipv4
hint.sin_port = htons(PORT); //set the port to the macro set above (use host-to-network-short to conver the int to the port)
hint.sin_addr.s_addr = INADDR_ANY; //set the ip to any address
if(bind(listening, (sockaddr*)&hint, sizeof(hint)) < 0) //attempt to bind socket (inticated by what number the `listening` socket is) to ip/port
{
std::cerr << "Can't bind to IP/Port" << std::endl;
return -1;
}
//mark the socket for listening
if(listen(listening, MAXCLIENTS) < 0 /*SOMAXCONN = maximum amount of connections, defined by sys/socket.h*/) //attempt to listen on the socket number indicated by `listening`
{
std::cerr << "Can't listen on the socket!" << std::endl;
return -1;
}
//FD_CLR() = Remove 1 from set
//FD_SET() = Add to set
//FD_ZERO() = Remove everything from set
//FD_ISSET() = Check if something is part of a set
fd_set master; //define the set
int max_sd;
int client_socks[MAXCLIENTS]{};
while (true)
{
FD_ZERO(&master); //make sure it's cleared
FD_SET(listening, &master); //add the listening socket (server) to the set
max_sd = listening; //max socket descriptor set to the listening socket (need this for the select func)
for (int i = 0; i < MAXCLIENTS; i++) {
if (client_socks[i] > 0) //make sure the particular socket exists
{
FD_SET(client_socks[i], &master); //add it to the set
}
if (client_socks[i] > max_sd) //if the socket is greater than our current maximum socket descriptor
{
max_sd = client_socks[i];
}
}
//wait for some action on any socket within the master fd (this will set the master fd_set to be equal to whatever socket had some action on it)
int activity = select(max_sd + 1, &master, nullptr, nullptr, nullptr);
if (activity < 0) //error!
{
std::cerr << "Error while trying to select!" << std::endl;
}
int addrlen = sizeof(hint);
if (FD_ISSET(listening, &master)) //if the select got that there was action on the listening (server) socket - most likely, a client socket is trying to connect!
{
int client_socket;
client_socket = accept(listening, (sockaddr *) &hint, &addrlen); //accept the first client "waiting to get in"
if (client_socket < 0) {
std::cerr << "Something went wrong when trying to accept a client socket!" << std::endl;
break;
}
std::cout << "New connection: " << inet_ntoa(hint.sin_addr) << " on port " << ntohs(hint.sin_port) << std::endl;
if (send(client_socket, "Welcome to the socket party!", strlen("Welcome to the socket party!"), 0) != strlen("Welcome to the socket party!")) { //greetings!
std::cerr << "Error when sending welcome message." << std::endl; //something went wrong ;(
}
for (int i = 0; i < MAXCLIENTS; i++) //for each index, set client_sock to the address of the index of client_socks, so that we can set the value of it!
{
if(client_socks[i] == 0) //if this position is null (0)
{
client_socks[i] = client_socket;
}
}
}
//else, there was action on a client socket (most likely a message is being sent!
char buffer[2048]; //we need somewhere to store clients messages!
for (int &client_sock : client_socks) //loop through the client sockets
{
if (client_sock != 0)
{
if (FD_ISSET(client_sock, &master) == 0) //check if the select got action on the particular index in the client_socks array
{
memset(&buffer, 0, sizeof(buffer)); //make sure the buffer is clear!
if (read(client_sock, &buffer, 2048) == 0) //check if nothing was recieved from the client
{
getpeername(client_sock, (sockaddr*)&hint, (socklen_t*)&addrlen) < 0; //gets networking info, based off of which socket is passed (the if statement checks for errors). In addition, it sets the values of hint to the info from the passed socket
//print that the client disconnected
std::cout << "A client has disconnected! IP: " << inet_ntoa(hint.sin_addr) << " Port: " << ntohs(hint.sin_port) << std::endl;
close(client_sock); //close the socket
client_sock = 0; //set its value in the array to 0, so that we can reuse it!
}
//else, we got message from the client
for (int &socket : client_socks)
//if (socket != client_sock)
send(socket , buffer , strlen(buffer) , 0 );
}
}
}
return 0;
}
}
Your client_socks variable must be initialised, as Igor said - just append braces: int client_socks[MAXCLIENTS]{};.
Then your for (int &client_sock : client_socks) loop doesn't ignore 0 values (which you obviously want to be a sentinel given "if(client_socks[i] == 0) //if this position is null (0)"). Inside that loop, add "if (client_sock[i] == 0) continue;. Because you try to read` from descriptor 0, it will block waiting for keyboard input.
Further, if you wanted your code to be robust, you would make the listening socket non-blocking, as it's possible for that socket to select readable, but by the time your application goes to accept from it, the client connection attempt has already been dropped: then you'd block waiting to accept another client connection attempt that might never come, and not be servicing existing clients.
The following is the client-side code for a UDP client-server echo program :
ret_val = sendmmsg(socket_id, msgs, no_of_packets, 0);
//I send message to the server
if(ret_val == -1)
std::cerr << "Message sending failed.\n";
else{
cout << ret_val << " messages sent\n";
/************************************************************************/
char buffers[no_of_packets][packet_size + 1];
msgs = new struct mmsghdr[no_of_packets];
iovecs = new struct iovec[no_of_packets];
memset(msgs, 0, sizeof(msgs));
memset(iovecs, 0, sizeof(iovecs));
for(int i = 0;i < no_of_packets;i++){
iovecs[i].iov_base = buffers[i];
iovecs[i].iov_len = packet_size;
msgs[i].msg_hdr.msg_iov = &iovecs[i];
msgs[i].msg_hdr.msg_iovlen = 1;
}
//and receive the packet here, but the program hangs here
ret_val = recvmmsg(socket_id, msgs, no_of_packets, 0, NULL);
My program hangs here, any idea why it's happening ? The following is the server-side code which first receives and then sends successfully, but after the server sends for the first time, my client isn't able to read it as it hangs.
ret_val = recvmmsg(socket_id, msgs, no_of_packets, 0, NULL);
if(ret_val < 0){
break;
}
else{
cout << ret_val << " messages received\n";
for(int i = 0;i < ret_val;i++){
buffers[i][msgs[i].msg_len] = 0;
printf("Trip %d : %s\n", trip, buffers[i]);
}
/************************************************************************/
if(connect(socket_id, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1){
perror("connect()");
exit(EXIT_FAILURE);
}
ret_val = sendmmsg(socket_id, msgs, no_of_packets, 0);
//This send is successful, but since my client hangs,
//the server hangs as well since the 'recvmmsg' at the top gets nothing from the client
if(ret_val == -1)
std::cerr << "Message sending failed.\n";
else
cout << ret_val << " messages sent\n";
This line in the server code looks suspicious:
if(connect(socket_id, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1)
What is server_addr? It's certainly not the source address of any of the packets return from the prior call to recvmmsg.
Just remove the connect call.
I could write more, but is there any particular reason you are using recvmmsg and sendmmsg instead of recvfrom and sendto ?
Below is a much simpler way of implementing an echo server with a udp socket:
const int MAX_UDP_MESSAGE_SIZE = 65535
unsigned char message[MAX_UDP_MESSAGE_SIZE+1];
int rcvReslt, sndResult;
sockaddr_in addr = {};
socklen_t addrLength = sizeof(addr);
rcvResult = recvfrom(socket_id, message, MAX_UDP_MESSAGE_SIZE, 0, (sockaddr*)&addr, &addrLength);
if (rcvResult > 0)
{
message[rcvResult] = 0; // null terminate the message
printf("Trip %d : %s\n", trip, message);
// echo back
sndResult = sendto(socket_id, message, rcvResult, 0, (sockaddr*)&addr, addrLength);
}
else
{
int error_code = errno;
printf("Error: %d\n", error_code);
}
Clearly you're connected to the wrong target. You don't need to connect at all. recvfrommsg() both return the source IP:port. Just send back to the same place.
I have a TCP application written in C++, where a client and a server exchange data. I've istantiated a socket, believing that it would have been blocking by default; on the contrary, after server waits for a client, I have that client calls the recv function without waiting for data. This is the code in which I inizialize the socket fr the client.
int TCPreceiver::initialize(char* address, int port)
{
sock = socket (AF_INET, SOCK_STREAM, 0);
cout << "Socket: " << sock << endl;
sockaddr_in target;
target.sin_family = AF_INET;
target.sin_port = htons (port);
target.sin_addr.s_addr = inet_addr(address);
int fails=0;
while (connect(sock, (sockaddr*) &target, sizeof(target)) == -1)
{
fails++;
if (fails==10)
{
close(sock);
cout << "Error with connection to the server, try again"<< endl;
exit(-1);
}
}
cout << "Client connected (control channel)" << endl;
unsigned char text[10]; //Request message
//fill text[]
if(send(sock, (char*)text, 10, 0)==-1)
{
printf("send() failed with error code : %d" , -1);
exit(EXIT_FAILURE);
}
return 0;
}
I've tried adding this code:
int opts;
opts = fcntl(sock,F_GETFL);
if (opts < 0) {
perror("fcntl(F_GETFL)");
exit(0);
}
opts = (opts & (~O_NONBLOCK));
if (fcntl(sock,F_SETFL,opts) < 0) {
perror("fcntl(F_SETFL)");
exit(0);
}
but it still doesn't work, and if I call the recv(), the application doesn't block (and recv() always returns 0). Here is the function where I call the recv():
void TCPreceiver::receive(char* text, int& dim)
{
int ret;
ret = recv(sock, text, dim, 0);
dim=ret;
if(ret == -1){
printf("recv() failed with error (%d)\n", ret);
//system("PAUSE");
exit(1);
}
}
Where am I wrong?
recv() returning zero indicates either (1) you passed a zero length, which is just a programming error which I won't discuss further here, or (2) end of stream. The peer has close the connection. This isn't a non-blocking situation, this is the end of the connection. You must close the socket and stop using it. It will never return anything. It zero ever again.
See the man pages.
I have a few questions about select function, i wrote this code:
void TCPSerwer::sel()
{
struct timeval tv = {1, 0};
fd_set temp_list = m_RecvList;
//if(select(m_fdmax + 1, &temp_list, NULL, NULL, &tv) == SOCKET_ERROR)
if(select(0, &temp_list, NULL, NULL, &tv) == SOCKET_ERROR)
{
perror( "select" );
exit( 1 );
}
for(int i = 0; i <= m_fdmax; i++ )
{
if(FD_ISSET(i, &temp_list))
{
// New connection
if(i == m_ListenSocket)
{
acceptClient();
}
// Data from client
else
{
PacketHeader header;
int nbytes = recv(i, (char*)(&header), sizeof(PacketHeader),
// Error
if(nbytes < 0)
{
disconnectClient(i);
}
// success
else
{
std::cout << "type: " << header.type << " len: " << header.length << std::endl;
}
}
}
}
}
I can give first arg to select function and i can don't do that, but why ? Why a should give first arg to select ? m_fdmax is highest number of socket, but this code working without this arg.
Next question is, why select need timeout ? When i don't give this arg select marks all socket as socket that can be readable but select doing this when socket haven't any data to read. When i give this arg i don't have this problem. But why ?
if m_fdmax is highest number of socket, i have to find next highest number of socket when i close connection, Right ? And i should doing this that:
int size = m_ClientVector.size();
for(int i = 0; i < size; i++)
{
if(m_ClientVector[i] > m_fdmax)
m_fdmax = m_ClientVector[i];
}
I can give first arg to select function and i can don't do that, but why ? Why a should give first arg to select ? m_fdmax is highest number of socket, but this code working without this arg.
Read the documentation. The select() function on Windows ignores the first parameter, so it does not matter what you pass to it.
Next question is, why select need timeout ?
It does not NEED a timeout, but you can OPTIONALLY provide a timeout if desired. This way, if the requested socket state is not reached before the timeout elapses, select() can still exit and not deadlock the calling thread indefinitely, allowing it to do other things.
When i don't give this arg select marks all socket as socket that can be readable but select doing this when socket haven't any data to read.
If you do not provide a timeout, select() waits indefinitely until the requested socket state actually occurs. A socket can be marked as readable if it has data to read, but it can also be marked as readible if it has been gracefully disconnected by the other party. A subsequent call to recv() will tell you which is the case (recv() returns -1 on error, 0 on disconnect, an >0 on data). Again, read the documentation.
if m_fdmax is highest number of socket, i have to find next highest number of socket when i close connection, Right ?
If you want to calculate the highest socket number (which Windows does not care about, but other platforms do), then you would have to re-calculate the highest socket number every time you call select(), or at least whenever you re-prepare the fd_set structure (which you need to do every time you call select() anyway).
And i should doing this that
On Windows, no. On other platforms, yes.
With that said, try this code on Windows instead:
void TCPSerwer::sel()
{
struct timeval tv = {1, 0};
fd_set temp_list = m_RecvList;
int ret = select(0, &temp_list, NULL, NULL, &tv);
if (ret == SOCKET_ERROR)
{
perror( "select" );
exit( 1 );
}
if (ret == 0) // timeout
return;
for(u_int i = 0; i < temp_list.fd_count; ++i)
{
SOCKET s = temp_list.fd_array[i];
// New connection
if (s == m_ListenSocket)
{
acceptClient();
continue;
}
// Data from client
PacketHeader header;
char *pheader = (char*) &header;
int nbytes = 0;
do
{
ret = recv(s, pheader, sizeof(PacketHeader)-nbytes, 0);
// success
if (ret > 0)
nbytes += ret;
}
while ((ret > 0) && (nbytes < sizeof(PacktHeader)));
// Error or disconnect
if (nbytes < sizeof(PacktHeader))
{
disconnectClient(i);
continue;
}
// success
std::cout << "type: " << header.type << " len: " << header.length << std::endl;
}
}
About the timeout:
select can use a struct timeval to timeout. If you pass a NULL pointer, select will wait until an event comes. If you pass an address to a struct timeval, select will return even if there's not event (in your code, select will return every second).
About the fdmax: Yes you have to find the highest socket, and your snippet is correct.
Other: You don't have any FD_SET in your code. Usually, the sockets are setted (via FD_SET so) in the loop which find the highest socket.
EDIT: My bad I didn't seen the fd_set temp_list = m_RecvList; in your code. We will need more code in order to analyze your problem with select.
Thanks for help, I want use this code on Windows and Linux, and now I doing that:
When I have a new connection:
bool TCPSerwer::acceptClient()
{
SOCKET new_client = accept(m_ListenSocket, 0, 0);
if(new_client == INVALID_SOCKET)
return false;
m_ClientVector.push_back(new_client);
// Add to FD
FD_SET(new_client, &m_RecvList);
if(new_client > m_fdmax)
m_fdmax = new_client;
return true;
}
When i want to close connection:
void TCPSerwer::disconnectClient(const SOCKET& client)
{
int size = m_ClientVector.size();
for(int i = 0; i < size; i++)
{
if(m_ClientVector[i] == client)
{
closesocket(m_ClientVector[i]);
// Delete from FD
FD_CLR(m_ClientVector[i], &m_RecvList);
m_ClientVector.erase(m_ClientVector.begin() + i);
break;
}
}
// re-calculateing the highest socket number
size = m_ClientVector.size();
for(int i = 0; i < size; i++)
{
if(m_ClientVector[i] > m_fdmax)
m_fdmax = m_ClientVector[i];
}
}
I havr one question to you Remy Lebeau, your recv function look that:
recv(s, pheader, sizeof(PacketHeader)-nbytes, 0);
but recv save data at the being of bufor ? Meybe this should look that:
recv(s, pheader + nbytes, sizeof(PacketHeader)-nbytes, 0);