c++ - 回声服务器示例

标签 c++ sockets network-programming

我编写了以下代码来制作回显服务器(我写入 stdout 的数据从我的 PC 移动到服务器并返回到我的 PC)。问题是 echo 没有显示在客户端的终端上。有 2 个 server.cpp 进程在运行,所以我知道我的连接已被服务器接受。完整代码是 here以便您可以直接复制并运行代码。代码的相关部分是:

服务器.cpp

void reflect(int x)
{
    int n;
    int m;
    char data[100];
    cout<<"Entered reflect function"<<endl; //this gets displayed

    n=read(x,data, 100); //***execution is not going beyond this point i.e. read is blocking***
    cout<<"Client sent "<<n<<endl; //this doesn't get displayed 

    if(n>0)
    {
        while(n>0)
        {
            m=write(x,data,n);
            n=n-m;
        }
    cout<<"Successfully echoed back to client"<<endl; //this doesn't get displayed 
    }
}

int main()
{
    sockaddr_in serv;
    bzero(&serv, sizeof(serv));
    serv.sin_family=AF_INET;
    serv.sin_port=htons(3345);
    inet_aton("127.0.0.1", &(serv.sin_addr));

    int servfd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    int x;
    x=bind(servfd, (sockaddr*)(&serv), sizeof(serv));

    cout<<"Bind returned"<<x<<endl; //this displays x as 0

    listen(servfd, 5);
    sockaddr cli;
    int connfd;
    pid_t id=-1;
    socklen_t siz=sizeof(cli);
    for(;;)
    {
        if(connfd=accept(servfd, &cli, &siz)>=0)
             id=fork();

        if(id==0)
             reflect(connfd);

        else 
             continue;
    }
}

客户端.cpp

int main()
{
    int clifd;
    clifd=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
    sockaddr_in serv;
    bzero(&serv, sizeof(serv));
    serv.sin_family=AF_INET;
    serv.sin_port=htons(3345);
    inet_aton("127.0.0.1", &(serv.sin_addr));

    connect(clifd, (sockaddr*)(&serv), sizeof(serv));//blocking call
    int n,m;
    char data[100];
    char recvd[100];
    for(;;)
    {
        fgets(data, 100,stdin );
        n=strlen(data);
        cout<<"You have written "<<n<<endl; //this returns the correct value

        if(n>0)
        {
            while(n>0)
            {  
                 m=write(clifd,data,n);
                 n=n-m;
            }
        }

        n=read(clifd, recvd, 100);
        cout<<"Server echoed back "<<n<<endl; //this doesn't get displayed

        if(n>0)
        {
            while(n>0)
            {
                m=fputs(data,stdout);
                fflush(stdout);
                n=n-m;
            }
            //cout<<data<<endl;
        }
    }
}

最佳答案

在 server.c 中,替换这个不正确的行:

if( connfd=accept(servfd, &cli, &siz) >=0 )

正确的是:

if( (connfd=accept(servfd, &cli, &siz)) >=0 )


旁白:您使用的是什么编译器? g++ 在看到该行时打印警告:

serv.cc: In function ‘int main()’:
serv.cc:45:43: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

在使用 g++ 或 gcc 进行编译时,我尝试始终使用 -Wall -Werror

关于c++ - 回声服务器示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11357620/

相关文章:

c++ - C++ 抽象类是不完整的类型吗?

具有非模板化类的 C++ 类模板?

javascript - 在执行 Javascript 内的函数之前,多次接收并等待具有相似 ID 的请求

c# - 网络流如何检测断开连接

c# - ftp上传太大

c++ - 在大型框架中定义和访问对象

c++ - 覆盖类成员变量

ios - IOS上使用BlueSocket框架的TCP连接问题

perl tcp 客户端不显示传入数据

c++ - 字符数组不会在我的服务器中打印