[英]Server is not able to print data getting from client
My client code sends some data to the server when it connects to it and I want that data to be printed by the server on the screen.But it is giving quite a strange out not what I am sending. 我的客户端代码在连接到服务器时会向服务器发送一些数据,我希望该数据由服务器在屏幕上打印出来。 Here is my server and client code:
这是我的服务器和客户端代码:
#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);
}
}
Client Code: 客户代码:
#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);
}
} }
input : Hello from the client 输入:您好,客户
output should be same but instead I am getting: U D$ U D$ U D$ U D$ U D$ U D$ U D$ U D$ U D$ U D$ U D$ U D$ U D$ U D$ 输出应该是相同的,但我得到的是: U D$ U D$ U D$ U D$ U D$ UU D$ U D$ U D$ U D$ U D$ U D$$ U D$ U D$ U D$
In the method Server::ReadWriteSocket()
在
Server::ReadWriteSocket()
write(sockfd, &buffer, sizeof(buffer));
std::cout <<buffer<<std::endl;
you print the buffer which has no ending zero to mark string end (another problem is that buffer
does not have memory reserved, so the server could crash). 您打印不带零结尾的缓冲区以标记字符串结尾(另一个问题是
buffer
没有保留内存,因此服务器可能会崩溃)。 Thus, it prints any random bytes found in buffer after the content. 因此,它将在内容之后打印在缓冲区中找到的任何随机字节。 Also, check the number of bytes returned by
read
and use it to determine end of the buffer
. 另外,检查
read
返回的字节数,并用它来确定buffer
末尾。 The same holds for Client::ReadWriteSocket()
. Client::ReadWriteSocket()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.