C++のクライアント/サーバーソリューションで作業しています。Recvをクライアントからサーバーソケットに一度だけ接続することでTCP接続を確立します。
クライアントからは、私のサーバーにデータを送信しています。このサーバーから別のサーバーに送信しています。私はポートとIPアドレスを設定することができ、正常に送信することができます。
しかし、私の側にない他のサーバは、私の側から1つだけのTCP接続を確立する必要があります。その後、送受信が必要になります。
2つのクライアント(同時に2つのクライアントから)に接続すると、接続が拒否されたことが示されます。
コードの一部を以下に示す:
while ((len = stream->receive(input, sizeof(input)-1)) > 0)
{
input[len] = NULL;
//Code Addition by 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 by Srini ends here
stream->send(line, len);
printf("thread %lu, echoed '%s' back to the client\n",
(long unsigned int)self(), line);
}
、クライアントから受信サーバへの送信、サーバから受信し、クライアントへの送信は、以下のリンクに示されている完全なスレッドコード:
デザインフローを変更するにはどうすればよいですか?クライアント/サーバープログラムの基本図でさえ。クライアントがconnect()
を呼び出すと、サーバーは毎回accept()
を呼び出し、送受信が行われます。だから、クライアントが一度だけ接続できるようにフローを変更するには何ができるのですか?