c++ - Send Recv from client to server socket by establish TCP Connection only once -


i working on client/server solution in c++.

from client, sending data server, , server sending server. able configure port , ip address, , able send successfully.

but, other server (which not on side) needs establish 1 tcp connection side, after sending , receiving needs happen.

if connecting twice (say 2 clients @ same time), shows connection refused.

part of code shown below:

while ((len = stream->receive(input, sizeof(input)-1)) > 0 ) {     input[len] = null;     //code addition srini starts here      //client declaration     tcpconnector* connector_client = new tcpconnector();     printf("ip_client = %s\tport_client = %s\tport_client_int = %d\n", ip_client.c_str(), port_client.c_str(),atoi(port_client.c_str()));     tcpstream* stream_client = connector_client->connect(ip_client.c_str(), atoi(port_client.c_str()));      //client declaration ends     if (stream_client)     {         //message = "is there life on mars?";         //stream_client->send(message.c_str(), message.size());         //printf("sent - %s\n", message.c_str());          stream_client->send(input, sizeof(input));         printf("sent - %s\n", input);         len = stream_client->receive(line, sizeof(line));         line[len] = null;         printf("received - %s\n", line);         delete stream_client;     }      //code additon srini ends here      stream->send(line, len);     printf("thread %lu, echoed '%s' client\n",         (long unsigned int)self(), line); } 

the full thread code receiving client, sending server, receiving server, , sending client shown in below link:

https://pastebin.com/umpqj70w

how can change design flow? in basic diagram of client/server program. when client calls connect(), server calls accept() every time, sending/receiving happens. so, can done modify flow client can connect once?

your intermediate server (which acting proxy, lets call that) needs maintain single connection other server , delegate messaging in parallel messaging being done between proxy , clients.

i suggest creating separate thread sole task maintain connection other server, , send/receive messages it.

when client sends message proxy, place message in thread-safe queue somewhere. have thread check queue periodically , send queued messages other server.

when other server sends message proxy, thread can receive , forward appropriate client.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -