I am trying to make tcp client and server. When client recv() for second time the following error occures: "Resource temporary unavalible".
This is my client code:
int CreateSocket(int fd)
{
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
return fd;
}
int ConnectToServer(int sock)
{
int res;
struct sockaddr_in serv_addr;
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)
<= 0) {
printf(
"\nInvalid address/ Address not supported \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
while ((res= connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0)
{
sleep(1);
}
return res;
}
int client()
{
int socketId;
int fd;
uint8_t buffer[500];
struct sockaddr_in server ;
socketId = CreateSocket(fd);
ConnectToServer(socketId);
printf("After connect to server\n");
uint8_t response;
int send_ret;
int recv_ret;
while(true)
{
recv_ret = recv(socketId,buffer,500,0);
if(recv_ret < 0)
{
perror("recv()\n");
}
else if(recv_ret == 0)
{
printf("ZERO on recv");
}
else
{
printf("Received %d bytes \n",recv_ret);
}
response = Response(0);
send_ret = send(socketId,&response,1,0);
if(send_ret <0)
{
printf("Sent: %hhu\n",response);
}
if(send_ret == 0)
{
printf("Zero return\n");
}
else
{
printf("Returned : %d bytes",send_ret);
}
}
close(socketId);
}
int main(int argc, char const* argv[])
{
client();
return 0;
}
And the server if needed:
RelayServer::RelayServer(char* ip, short port)
{
this->ip = ip;
this->port = port;
}
int RelayServer::SetupServer(short port)
{
server_socket = socket(AF_INET , SOCK_STREAM , 0);
server_addr.sin_family = AF_INET;
server_addr.sin_addr. s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
bind(server_socket,(SA*)&server_addr, sizeof(server_addr));
listen(server_socket, BACKLOG);
printf("Relay server listen at port: %d \n", port);
return server_socket;
}
int RelayServer::AcceptNewConnection(int server_socket)
{
addr_size = sizeof(SA_IN);
client_socket = accept(server_socket, (SA*)&client_addr, (socklen_t*)&addr_size);
if(client_socket < 0)
{
perror("Perror()\n");
}
printf("Accepted new connection on socket %d\n", client_socket);
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 500000;
setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
return client_socket;
}
int RelayServer::HandleConnection(int client_socket, uint8_t* payload, size_t len)
{
if((send(client_socket,payload,len, 0 ))<0)
{
perror("Send()\n");
};
size_t recv_ret = recv(client_socket, buffer, sizeof(BUFFER_SIZE), 0);
if ( (recv_ret < 0 ) || ( recv_ret == EAGAIN) || ( recv_ret == EWOULDBLOCK ) )
{
return -1;
}
else
{
printf("Received %lu bytes from the board...\n", recv_ret);
printf( "<--- buff[0] = %hhu\n", buffer[0] );
return buffer[0];
}
}
int RelayServer::SendTcpPacket(uint8_t* payload, size_t len)
{
int max_socket_so_far = 0;
if(! server_is_set)
{
server_socket = SetupServer(port);
server_is_set = true;
cout<<"Relay server socket is set to: "<<server_socket<<endl;
}
FD_ZERO(¤t_sockets);
FD_SET(server_socket,¤t_sockets);
if(FD_ISSET(server_socket, ¤t_sockets))
{
cout<<"Current socket is set with value of server_socket"<<endl;
};
max_socket_so_far = server_socket + 2;
while(true)
{
ready_sockets = current_sockets;
cout<<"Before select"<<endl;
int numb_of_desc;
numb_of_desc = select(max_socket_so_far,&ready_sockets, NULL, NULL, NULL);
if(numb_of_desc < 0)
{
perror("select error");
}
for(int i=0; i < max_socket_so_far;i++)
{
if(FD_ISSET(i, &ready_sockets))
{
if(i == server_socket){
cout<<"FOund server socket"<<endl;
client_socket = AcceptNewConnection(server_socket);
FD_SET(client_socket, ¤t_sockets);
if(client_socket > max_socket_so_far) {
max_socket_so_far = client_socket;
}
}
else
{
int payload_ret = HandleConnection(i,payload,len);
if(payload_ret != -1)
{
FD_CLR(i,¤t_sockets);
return payload_ret;
}
else
{
}
}
}
}
}
What I have tried:
I am newly to socket programming and I just try to read the man pages for all the functions I was using but don't get whats going on.