I am trying to implement a simple chat using socket programming where the 'enter' key sends the response to the from the client to the server or vice versa. The code below works as expected with a special character such as '*' as the buffer but when I try to use '\n' the messages are never sent. I must be implementing the 'enter' portion incorrectly. Any ideas on how to fix this issue?
Server:
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main()
{
int client, server;
int portNum = 1500;
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
struct sockaddr_in server_addr;
socklen_t size;
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
cout << "\nError establishing socket..." << endl;
exit(1);
}
cout << "\n=> Socket server has been created..." << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(portNum);
if ((bind(client, (struct sockaddr*)&server_addr,sizeof(server_addr))) < 0)
{
cout << "=> Error binding connection, the socket has already been established..." << endl;
return -1;
}
size = sizeof(server_addr);
cout << "=> Looking for clients..." << endl;
listen(client, 1);
int clientCount = 1;
server = accept(client,(struct sockaddr *)&server_addr,&size);
// first check if it is valid or not
if (server < 0)
cout << "=> Error on accepting..." << endl;
while (server > 0)
{
strcpy(buffer, "=> Server connected...\n");
send(server, buffer, bufsize, 0);
cout << "=> Connected with the client #" << clientCount << ", you are good to go..." << endl;
cout << "\n=> Enter # to end the connection\n" << endl;
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
do {
cout << "\nServer: ";
do {
cin >> buffer;
send(server, buffer, bufsize, 0);
if (*buffer == '#') {
send(server, buffer, bufsize, 0);
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
} while (!isExit);
cout << "\n\n=> Connection terminated with IP " << inet_ntoa(server_addr.sin_addr);
close(server);
cout << "\nGoodbye..." << endl;
isExit = false;
exit(1);
}
close(client);
return 0;
}
Client:
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
using namespace std;
int main()
{
int client;
int portNum = 1500; // NOTE that the port number is same for both client and server
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
char* ip = "192.168.0.8";
struct sockaddr_in server_addr;
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
cout << "\nError establishing socket..." << endl;
exit(1);
}
cout << "\n=> Socket client has been created..." << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portNum);
if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
cout << "=> Connection to the server port number: " << portNum << endl;
cout << "=> Awaiting confirmation from the server..." << endl; //line 40
recv(client, buffer, bufsize, 0);
cout << "=> Connection confirmed, you are good to go...";
cout << "\n\n=> Enter # to end the connection\n" << endl;
do {
cout << "Client: ";
do {
cin >> buffer;
send(client, buffer, bufsize, 0);
if (*buffer == '#') {
send(client, buffer, bufsize, 0);
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
cout << "Server: ";
do {
recv(client, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
cout << endl;
} while (!isExit);
cout << "\n=> Connection terminated.\nGoodbye...\n";
close(client);
return 0;
}
EDIT: I made the change referenced in the answer below and I am still not getting the expected results.
Here is what happens when I run the Server and Client:
When I press Enter the message is not being sent to the Server. However, when I ran this same code with "buffer = '*'" it works as expected.