Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.12/5 (4 votes)
See more:
client server messaging same pcs but doesn`t work seperate computers.

What I have tried:

Client.cpp
#include<iostream>
#include<string.h>
#include
#include
#include
#include
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
using namespace std;

#define IP "192.168.56.1"
int main(){
     int client,server;
    int portNum=1500;
    bool isExit=false;
    int bufsize=1024;
    char buffer[bufsize];

   // char *ip= "192.168.1.101";
     struct sockaddr_in server_addr;
     client=socket(AF_INET,SOCK_STREAM,0);
     
     if(client<0){
        cout<< "Error createing socket." <<endl;
        exit(1);
        
        }
        
         cout<< "Client Socket created." <<endl;
        server_addr.sin_family=AF_INET;
        
     server_addr.sin_addr.s_addr = inet_addr(IP);
        server_addr.sin_port=htons(portNum);
        if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
        cout << "=> Connection to the server " << inet_ntoa(server_addr.sin_addr) << " with port number: " << portNum << 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; 
                close(client);
                return 0;
    }

Server.cpp

#include<iostream>
#include<string.h>
#include
#include
#include
#include<stdlib.h>
#include<unistd.h>
#include

#define IP "127.0.0.1"

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<< "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; 
                 isExit=false;
                 exit(1) ;
             }
close(client);
return 0;     
                            
    }
Posted
Updated 27-Oct-22 20:45pm
v5
Comments
Jochen Arndt 3-Mar-16 9:52am    
Has the server the IP 192.168.56.1 and is the client in the same subnet (has an IP 192.168.56.x)?
Member 11351616 4-Mar-16 4:16am    
The client and server has to be same wifi but different computers.So my client ip and server ip different.How can i connect server from client?
Jochen Arndt 4-Mar-16 4:39am    
You did not really answered my question.

However, when they are in the same subnet ensure that:
- The client uses the correct server IP (when using WiFi use the IP address of the WiFi interface)
- There is no firewall blocking your packets (check on the server and the WiFi router)
Member 11351616 4-Mar-16 5:56am    
okey i got it.I will check it.Also server_addr.sin_addr.s_addr = inet_addr(IP); when i use the this code on the client.cpp , it does not work correctly. Can u check my code ? I am new C++, its my first codes.
Jochen Arndt 4-Mar-16 6:03am    
What do you mean by "not work correctly"?

1 solution

The answer to why it wont work on seperate PC's is simple and nothing to do with your code.

It's called a FIREWALL you are using port 1500 you will need to open that port on the firewalls on both PC's.

Assuming you are on windows, (search appropriate if on linux):
Open a port in Windows Firewall - Windows Help[^]
 
Share this answer
 
v2
Comments
[no name] 22-Feb-22 8:52am    
Hi, Leon de boer Thanx for Solution 1. It was the same i ussue lately i have, i worked on firewall settings as in solution 1, it helps for LAN. What about both computers connected internet through different internet service providers? Frankly i did test it but it did not work!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900