Home/Support/Support Forum/Connection failure in TCPIP socket program in C running 88 threads
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Connection failure in TCPIP socket program in C running 88 threads

0 votes
Hello,

I am using this below mentioned TCP IP socket code to transfer data between 88 threads of client and server machine.
Each thread is assigned to a different core. Yes, It is an 88 core machine.
Each socket is creating a socket and performing send or receive operation.
Each socket assigned to a different port number. (Port number starting from 8020 to 8108)

The below mentioned code is used in each of server and client code thread respectively.

The issue here is, For few executions, in some sockets or threads, data received at client is matching with data sent by server.
However in some executions, the data is not matching in few sockets out of 100. say i am finding, for around 50 sockets/threads data buffer are matching but rest, they do not.

Those which are not matching, sometimes post an error saying "connection reset by peer" or "connection failed", then return from the function.

Why is this error occurring randomly for few sockets and in few executions only?

This sample program implemented for a single thread works fine. The issue is occurring in multi-threaded environment when 88 threads are running.

Tried few troubleshooting steps:
1) Changing port number range.
2) Verified in sytem monitor that data rate is around 117 MB/s so included usleep to bring down the data rate.
3) Verified that there are no errors when "send" operation is performed in server code. Data read failure occurs only in the client code.

Please help.



====================================================
Client code //this code is placed in each thread of 88 threads in client machine
{
/* there may be variables which are unused */
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
samplestruct tcp1_buffer = {0}; //structure which has two float values
int count = 0, bufferwrite = 0, filenumber= 0,index=0;
/* Socket creation */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket creationfailed\n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT0);// PORT0 is enum for 8020

// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "192.168.1.5", &serv_addr.sin_addr)<=0)
{
printf("The error in inet_pton of thead1 is %s\n",strerror(errno));
return -1;
}
/* Used for connection of the socket to the server machine */
if ((connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0))
{
printf("\nConnection Failed %s \n", strerror(errno));
return -1;
}
while(1) // Continously reading data from server - send
{
valread = read( sock , &tcp1_buffer, sizeof(samplestruct));
if(valread <= 0 )
{
printf("Read failed in Client 1 with error msg %s\n",strerror(errno));
return 0;
}

valread = 0;
clientBuffer1[count].iVal = tcp1_buffer.iVal; //clientBuffer1 is a global buffer. Decalaration of that not shown here.
clientBuffer1[count].qVal = tcp1_buffer.qVal;
if((count > 0 ) && (clientBuffer1[count].iVal == clientBuffer1[count-1].iVal))
{
printf(" count ::::::: %d ClientBufferValue 1::::::: %f,%f\n",count,clientBuffer1[count].iVal,clientBuffer1[count].qVal);
}
count++;

if(count == BUFFERLIMIT) //BUFFERLIMT IS SET TO 500 - MACRO
{
count = 0;
}

}

shutdown(sock, 2);
close(sock);
return EXIT_SUCCESS;
}



==============================================================

SErver code:
/* This code is used in each thread of server machines - 88 threads are created */
{
/* Some variables may not be used */
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
samplestruct cp1_buffer = {0}; /* structure which holds two float values */
/* Reading values from a file and storing it in a buffer so that it can be send to client machine */
int i = 0,l_count=0;
while(l_count < BUFFERLIMIT) //BUFFERLIMIT is 500
{
fscanf(fp,"%f,%f\n",&serverBuffer1.iVal,&serverBuffer1.qVal);
l_count++;
i++;
}
/* creation of socket */
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("192.168.1.5");
address.sin_port = htons( PORT0 ); //PORT0 is set to 8020

// Forcefully attaching socket to the port 8020
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}

if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}

if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
l_count = 0;

while(1) //continously sending data to client
{
valread = send(new_socket ,serverBuffer1+l_count, sizeof( samplestruct), 0 );
if (valread <= 0)
printf("Error in sending in thread 1 - %s\n", strerror(errno));
l_count++;
if(l_count == BUFFERLIMIT ) //* BUFFERLIMIT set to 500 */
l_count = 0;
}


shutdown(server_fd,2);
close(server_fd);


}
asked Feb 16, 2018 in ConnectPort Display by phon35tuff New to the Community (0 points)

Please log in or register to answer this question.

...