C++ 服务器客户端聊天

[ad_1]

客户端服务器向同一台电脑发送消息,但不能在单独的计算机上工作。

我尝试过的:

客户端.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;
    }

服务器.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;     
                            
    }

解决方案1

为什么它不能在单独的 PC 上工作的答案很简单,与您的代码无关。

它称为防火墙,您正在使用端口 1500,您需要在两台 PC 的防火墙上打开该端口。

假设您在 Windows 上(如果在 Linux 上则搜索适当的):
在 Windows 防火墙中打开端口 – Windows 帮助[^]

[ad_2]

コメント

标题和URL已复制