Click here to Skip to main content
15,900,461 members

Comments by Member 4099447 (Top 6 by date)

Member 4099447 6-Mar-20 9:38am View    
Here is the running code the main
the thread part receive_cmds(LPVOID lpParam) its too long ,..

`each socket twice:` it was my mistake when posting the code.

Sorry I was afraid to post my real code .I asked my n+ 1 and it is ok


int main()
{
    printf("Starting up multi-threaded TCP server \r\n");
    FILE *fileStream_SQL; char fileText_SQL [240];
    fileStream_SQL = fopen ("PATH_sql.txt", "r");
    fgets (fileText_SQL, 240, fileStream_SQL);
    CONN_STR = fileText_SQL;
    CONN_STR_READ_ONLY =ReplaceString(CONN_STR,"Uid=sa;","Uid=alex_ro;");
    fileText_SQL[0] ='\0';
    fclose(fileStream_SQL);

 // our masterSocket(socket that listens for connections)
 SOCKET sock;

 // for our thread
 //DWORD thread;

 WSADATA wsaData;
 sockaddr_in server;

 // start winsock
 int ret = WSAStartup(0x101,&wsaData); // use highest version of winsock avalible

 if(ret != 0)
 {
	return 0;
 }

 // fill in winsock struct ...
 server.sin_family=AF_INET;
 server.sin_addr.s_addr=INADDR_ANY;
 server.sin_port=htons(23); // listen on telnet port 23

 // create our socket
 sock=socket(AF_INET,SOCK_STREAM,0);

 if(sock == INVALID_SOCKET)
 {
	return 0;
 }

 // bind our socket to a port(port 23)
 if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 )
 {
	return 0;
 }

 // listen for a connection
 if(listen(sock,5) != 0)
 {
	return 0;
 }

 printf("TCP server sin_port 23 \r\n");

 // socket that we snedzrecv data on
 SOCKET client;

 sockaddr_in from;
 int fromlen = sizeof(from);


 std::thread th_overCPU(overCPU);
th_overCPU.detach();

 // loop forever
  while ((client = accept(sock, (struct sockaddr*)&from,&fromlen))!= INVALID_SOCKET)
    {
    Sleep(250);
    std::thread th_client(receive_cmds,(LPVOID)client);
    th_client.detach();

 }
 // shutdown winsock
 closesocket(sock);
 WSACleanup();

 // exit
 return 0;
}

Member 4099447 6-Mar-20 8:52am View    
Not true
App starts with 2 threads own and one msvcrt.dll
When I connect 3 clients its growing with 3

https://docs.google.com/spreadsheets/d/e/2PACX-1vT4ovUEMwOehRZmbx5bLrKmh3wCNzP2XX_aas3MMDSTazTH4lLumxa4VlLXqEqmyw/pubhtml

And when I disconnect one its decreasing with 1 as expected.
But I will put a limit just in case.
Member 4099447 6-Mar-20 8:36am View    
Thank you.
I will do
Member 4099447 5-Mar-20 12:18pm View    
I am not allowed to post full code.
Member 4099447 5-Mar-20 11:04am View    
Number of threads not increase do to the fact that we have only 7 hand held devices that communicate with the server.
When I said in time I mean hours even days.
Maybe because that app runs on s virtual server.I maid an additional thread that one every 10 min checks CPU if over 85 it is restarting app.
My company won't allow to post my code.