I’m trying to make a simple chat program between to programs on a lan. My problem is I can easily connect and send message from the server to client but not the other way around. I’m trying to make it work like this. Server<->Client as in constantly sending and receiving between both programs, obviously that would require a loop for continuous input but I’m not quite sure how to do it. Here is the code for both server and client.
//THIS IS THE SERVER
int main(int argc, char *argv[])
{
WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
if (starterr != 0)
{
cout << "WSADATA Failed to startup!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "WSADATA Startup Successful!" << endl;
SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
if (mysock == INVALID_SOCKET)
{
cout << "Socket Creation Failed!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "Socket Creation Successful!" << endl;
sockaddr_in sin;
sin.sin_port = htons(80);
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_family = AF_INET;
if (bind(mysock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
cout << "Socket failed to bind!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "Socket Binded Successfuly!" << endl;
//Listen to the socket until successful on receiving input
while (listen(mysock, SOMAXCONN) == SOCKET_ERROR);
SOCKET client;
int lin = sizeof(sin);
client = accept(mysock,(sockaddr*) &sin, &lin);
cout << "Connection Established!" << endl;
char buf[200] = "Message from server to client\n";
//Send an initial message to the client
send(client, buf, sizeof(buf), 0);
//but then how do I wait for a message from the client again here??
//ive tried recv here before with no luck :/
closesocket(mysock);
closesocket(client);
WSACleanup();
system("pause >nul");
return 0;
}
//Client program here
int main(int argc, char *argv[])
{
WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
if (starterr != 0)
{
cout << "WSADATA startup has failed!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "WSADATA Startup Successful!" << endl;
SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
if (mysock == INVALID_SOCKET)
{
cout << "Socket Creation Failed!" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
WSACleanup();
return 0;
}
cout << "Socket Creation Successful!" << endl;
sockaddr_in sin;
sin.sin_port = htons(80);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");//ip for connection
sin.sin_family = AF_INET;
if (connect(mysock,(sockaddr*)&sin, sizeof(sin)) == INVALID_SOCKET)
{
cout << "Socket Connection Failed" << endl;
cout << "Error Code: " << WSAGetLastError() << endl;
system("pause >nul");
closesocket(mysock);
WSACleanup();
return 0;
}
cout << "Socket Has Connected Successfuly!" << endl;
//Same applies here, receive message but exactly how do I send one back
char buf[200];
recv(mysock, buf, sizeof(buf), 0);
cout << buf;
//send here seemed to bring up a bunch of symbols
system("pause >nul");
WSACleanup();
closesocket(mysock);
return 0;
}
If you want full-duplex communication (i.e. both server and client sides able to send and receive at any time), you can either use multiple threads (which I don’t recommend, since multithreading introduces race conditions and deadlocks unless you really know what you are doing), or use non-blocking I/O, multiplexed using select() or poll() or similar. With multiplexing, you basically tell the select()/poll() call “don’t return until something interesting happens”, where “something interesting” is defined as “data arrived on a socket”, or (if you have data to send) “there is now buffer space available on a socket to place some outgoing data into”. Then when select()/poll() returns, you examine what events have been flagged as ready, call send()/recv() as appropriate (these calls won’t ever block since you set the sockets to non-blocking-I/O mode), and then go back to sleep inside select()/poll() again until the next events occur.
In this way your program is able to handle both send() and recv() efficiently, without spinning the CPU and without every having recv() hold off send() (or vice versa).