problems with client server achitecture: client does not wait the reply of the server
Good evening,
I would like to ask your help. I work in Windows 10 with Visual Studio.
I would like to write a piece of software (Soft1) that receives text messages from a program (Soft2) and depending on the message received sends a text message to another program (Soft3).
Soft3 is already available, I am writing Soft1 and Soft2.
my idea is to use a server client architecture. Soft1 will create a server socket, waits for incoming connection (from Soft2) and after Soft2 is connected creates another client socket and connects to Soft3.
Soft 2 will create a client socket, connect to Soft1and start to send messages. Soft2 should send a message, wait for the reply and then send the next message.
To keep it simple, in this first version Soft1 should just forward messages from Soft2 to Soft3 and forward back the reply from Soft3 to Soft2 till a specific message (Mes1) is received. In order to identify the incoming messages, Soft1 looks at the first 4 characters and compares them with some test strings (Mes1, Mes2,...).
I start the execution of Soft1, then I start the execution of Soft2. Soft2 can create the client socket and send a first message to Soft1 and wait for a reply before sending a second message to Soft1. Till this point everything is ok.
The problem arrives after the second message sent by Soft2: Soft2 does not wait for the reply of Soft 1 and send immediately the other messages.
I writes here the codes for Soft1 and Soft2 that create problems (I did not put the part that creates the sockets because it works).
Soft1
Code:
#include <stdio.h>
#include <winsock2.h>
# include <WS2tcpip.h>
# include <iphlpapi.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char *argv[])
{
/*Initialize Winsock*/
/*Create a TCP Socket: for server communication with Soft2 */
//Bind
//listen to incoming connection
//Accept connection
c = sizeof(struct sockaddr_in);
Soft2 = accept(new_socket, (struct sockaddr *)&client, &c);
if(new_socket == INVALID_SOCKET)
{
printf("Accept failed : %d", WSAGetLastError());
}
puts("Connection accepted");
/*Create a TCP Socket for connecting to Soft3*/
/*Connect to Soft3 server*/
if (connect(Soft3, (struct sockaddr *)&serverSoft3, sizeof(serverIPG)) < 0)
{
//puts("connect error");
printf("Connect Error : %d \n", WSAGetLastError());
return 1;
}
/*Start of the while loop that receives and forwards messages*/
char Test[] = "xxxx";
Mes1[] = "Mes1";
Mes2[] = "Mes2";
Mes3[] = "Mes3";
While (1)
{
Test[0] = 'p';
while (strcmp(Test, Mes1) != 0)
{
if ((recv_size = recv(Soft2, server_reply, 2000, 0)) == SOCKET_ERROR)
{
return 1;
}
else
{
memcpy(Test, server_reply, 4);
counter++;
printf("received message %d \n", counter);
if (send(Soft3, server_reply, strlen(server_reply), 0) < 0)
{
return 1;
}
printf("Sent message to Soft3 %d \n", counter);
if ((recv_size = recv(Soft3, server_reply2, 2000, 0)) == SOCKET_ERROR)
{
return 1;
}
printf("Received reply from Soft3 %d \n", counter);
if (send(Soft2, server_reply2, strlen(server_reply2), 0) < 0)
{
return 1;
}
printf("Sent to Soft2 reply by Soft3 %d \n", counter);
}
}
}
closesocket(Soft2);
closesocket(Soft3);
WSACleanup();
return 0;
}
Soft2:
Code:
#include <stdio.h>
#include <winsock2.h>
# include <WS2tcpip.h>
# include <iphlpapi.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char *argv[])
{
/*Initialize Winsock*/
/*Create a TCP Socket */
/*Connect to a server*/
if (connect(Soft1, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Connect Error : %d", WSAGetLastError());
return 1;
}
/*Send message 1*/
message = "Mes2 do something \n";
if (send(Soft1, message, strlen(message), 0) < 0)
{
puts("Send failed");
return 1;
}
/* Receive reply for message 1*/
/*at the following "if" Soft2 stops correctly and wait for Soft1 reply*/
if ((recv_size = recv(Soft1, server_reply, 2000, 0)) == SOCKET_ERROR)
{
puts("Reception failed0");
return 1;
}
/*Send message 2*/
message = "Mes2 do something \n";
if (send(Soft1, message, strlen(message), 0) < 0)
{
puts("Send failed");
return 1;
}
/* Receive reply for message 2*/
/*here Soft2 does not stop correctly and does not wait for Soft1 reply, as Soft2 could still read the reply from the previous message 1, and sends message 3*/
/*Soft1 then will receive message 2 and message 3 together */
if ((recv_size = recv(Soft1, server_reply, 2000, 0)) == SOCKET_ERROR)
{
puts("Reception failed0");
return 1;
}
/*Send message 3*/
message = "Mes3 do something \n";
if (send(Soft1, message, strlen(message), 0) < 0)
{
puts("Send failed");
return 1;
}
if ((recv_size = recv(Soft1, server_reply, 2000, 0)) == SOCKET_ERROR)
{
puts("Reception failed");
return 1;
}
/*Send message 4*/
message = "Mes1 stop the While loop \n";
if (send(Soft1, message, strlen(message), 0) < 0)
{
puts("Send failed");
return 1;
}
return 0;
}
I would like to ask you what is the correct manner to write Soft1 so that Soft2 waits the arriving of a reply before sending the next message.
thank you!