hi guys/girls, im trying to learn C++ and followed a video from youtube (https://www.youtube.com/watch?v=IydkqseK6oQ) to write some code only the program doesnt behave as required and the server is always looking for connections and never finds one....... does anyone know why this might be i dont get compile time errors.
Here's the code for the server:
Code:
#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<netinet/ip.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;
// init socket
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
cout << "Error establishing connection" << endl;
exit(1);
}
cout << "Server Socket connection created..." << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(portNum);
// Binding socket
if (bind(client, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
{
cout << "Error binding socket...." << endl;
exit(1);
}
size = sizeof(server_addr);
cout << "Looking for clients..." << endl;
// Listening socket
listen(client, 1);
// Accept client
server = accept(client, (struct sockaddr*)&server_addr, &size);
if (server < 0)
{
cout << "Error on accepting.." << endl;
exit(1);
}
while (server > 0)
{
strcpy(buffer, "Server connected...\n");
send(server, buffer, bufsize, 0);
cout << "Connected with client..." << endl;
cout << "Enter # to end the connection" << endl;
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << "buffer" << " ";
if (*buffer == '#')
{
*buffer = '*';
isExit = true;
}
}
while (*buffer != '*');
do{
cout << "\nServer: ";
do{
cin >> buffer;
send(server, buffer, bufsize, 0);
if (*buffer == '#')
{
send(server, buffer, bufsize, 0);
*buffer = '*';
isExit = true;
}
}while (*buffer != '*');
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if(*buffer == '#')
{
*buffer == '*';
isExit = true;
}
}
while (*buffer != '*');
}
while (!isExit);
cout << "Connection terminated..." << endl;
cout << "Goodbye..." << endl;
isExit = false;
exit(1);
}
close(client);
return 0;
}
here's the code for the client:
Code:
#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, server;
int portNum = 1500;
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
char *ip = "127.0.0.1";
struct sockaddr_in server_addr;
// Init socket
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
cout << "Error creating socket" << endl;
exit(1);
}
cout << "Client socket created" << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portNum);
// Connecting socket server
if (connect(client, (struct sockaddr*)&server_addr, sizeof(server_addr)) == 0)
{
cout << "Connecting to server..." << endl;
}
recv(client, buffer, bufsize, 0);
cout << "Connection confirmed" << endl;
cout << "Enter # to end the connection" << endl;
do {
cout << "Client: ";
do {
cin >> buffer;
send(client, buffer, bufsize, 0);
if (*buffer == '#')
{
send(client, buffer, bufsize, 0);
*buffer = '*';
isExit = true;
}
}
while (*buffer != 42);
cout << "Server: ";
do{
recv(client, buffer, bufsize, 0);
cout << buffer << " ";
if(*buffer == '#')
{
*buffer == '*';
isExit = true;
}
}
while (*buffer != 42);
cout << endl;
}
while(!isExit);
cout << "Connection terminated..." << endl;
cout << "Goodbye..." << endl;
close(client);
return 0;
}
thanks in advance guys/girls....