服务器无法打印从客户端获取的数据

时间:2016-02-10 11:02:37

标签: c++ linux server client

我的客户端代码在连接到服务器时会向服务器发送一些数据,我希望服务器在屏幕上打印这些数据。但是,这并不是我发送的内容。这是我的服务器和客户端代码:

#include <iostream>
#include "server.h"

Server::Server()
{

}

void Server::CreateSocket()
{
server_sockfd = socket(AF_INET, SOCK_STREAM,0);
if(server_sockfd < 0)
{
    std::cout <<  "Error creating socket\n"<<std::endl;
    exit(1);
}
std::cout << "Server socket created\n" <<std::endl;
}

void Server::NameSocket()
{
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(1500);

if((bind(server_sockfd, (struct sockaddr *)& server_address, sizeof(server_address)))<0)
{
    std::cout<<"Error binding socket\n"<<std::endl;
    exit(1);
}

std::cout <<"Server naming done \n"<<std::endl;
}

void Server::CreateQueue()
{
listen(server_sockfd, 1);
std::cout <<"Server Create queue done \n"<<std::endl;
}

void Server::AcceptConnection()
{
size = sizeof(server_address);
client_sockfd = accept(server_sockfd,(struct sockaddr*)&server_address, &size);
if(client_sockfd < 0)
{
    std::cout << "Can't accept connection\n"<<std::endl;
    exit(1);
}

std::cout <<"Server accept connection done"<<std::endl;
}

void Server::ReadWriteSocket()
{
while(true)
{
    char *buffer;
    // Server reading data coming from the client
    read(client_sockfd, &buffer, sizeof(buffer));
    //std::cout <<buffer<<std::endl;( Problem Comes here )

    // Server writing data to the client
    //write(client_sockfd, & buffer1,sizeof(buffer1));
    //std::cout << buffer1<<std::endl;
    close(client_sockfd);
}
}

客户代码:

   #include <iostream>
   #include "client.h"


   Client::Client()
  {
    this->_Flag = false;
  }


// Create a socket for client
void Client::CreateSocket()
{
   if((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0)
{
    perror("Socket not created");
    exit(1);
}

std::cout << "Client Socket created \n"<<std::endl;
}
//Name socket as agreed with a server
void Client::NameSocket()
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(1500);
std::cout<<"Client name socket done \n"<<std::endl;
}

void Client::ConnectSocket()
{
 // Now connect out socket to server socket
result = connect(sockfd, (struct sockaddr*)&address, sizeof(address));
if(result == -1)
{
    std::cout << "Can't establish connection\n"<<std::endl;
    exit(1);
}

 std::cout << "Connect to server socket done \n"<<std::endl;
}
// We can now read and write via sockfd
void Client::ReadWriteSocket()
{
  char *buffer= "Hello from the client";
_Flag = true;
while(_Flag)
{
write(sockfd, &buffer, sizeof(buffer));
std::cout <<buffer<<std::endl;
//std::cout<<buffer1<<std::endl;
//read(sockfd,& buffer, sizeof(buffer));
close(sockfd);
}

}

输入:来自客户的Hello

输出应该相同,但我得到: UD $ UD $ UD $ UD $ UD $ UD $ UD $ UD $ UD $ UD $ UD $ UD $ UD $ UD $

1 个答案:

答案 0 :(得分:-1)

方法Server::ReadWriteSocket()

write(sockfd, &buffer, sizeof(buffer));
std::cout <<buffer<<std::endl;

你打印没有结束零的缓冲区来标记字符串结束(另一个问题是buffer没有保留内存,所以服务器可能会崩溃)。因此,它打印内容后缓冲区中找到的任何随机字节。另外,检查read返回的字节数,并使用它来确定buffer的结尾。 同样适用于Client::ReadWriteSocket()