[英]Unable to Send data from Server to client on local network in C++
I need to communicate between two systems on local server. 我需要在本地服务器上的两个系统之间进行通信。 So, I decided to use "Socket" programming in C++. 因此,我决定在C ++中使用“套接字”编程。 For this I follow this tutorial. 为此,我遵循本教程。 I make some changes according to my need like changing port no, IP etc which are working fine. 我根据需要进行了一些更改,例如更改端口号,IP等,这些都可以正常工作。 Now the problem is I can send data from Server to Client but I am unable to receive any data from Client to Server. 现在的问题是我可以将数据从服务器发送到客户端,但无法接收任何数据从客户端到服务器。 Here is my code for Client:- 这是我给客户的代码:
int main()
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char *message , server_reply[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("192.168.1.4");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
{
puts("recv failed");
}
puts("Reply received\n");
server_reply[recv_size] = '\0';
puts(server_reply);
message = "Cleint says:- I am Client";
if( send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
puts("sending data complete\n");
system("pause");
return 0;
}
Here is My code for Server:- 这是我的服务器代码:-
int main()
{
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
char *message , cleint_reply[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//Listen to incoming connections
listen(s , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
{
puts("Connection accepted");
//Reply to the client
message = "Server sends:- I am Server\n";
send(new_socket , message , strlen(message) , 0);
while(1)
{
if((recv_size = recv(s , cleint_reply , 2000 , 0)) == SOCKET_ERROR)
{
// puts("recv failed");
}
else
puts("data received");
}
}
if (new_socket == INVALID_SOCKET)
{
printf("accept failed with error code : %d" , WSAGetLastError());
return 1;
}
closesocket(s);
WSACleanup();
return 0;
}
This is the console messages I am getting on Client:- 这是我在客户端上收到的控制台消息:
And these are the messages I am getting on Server console:- 这些是我在服务器控制台上收到的消息:
Connection accepted 接受连接
So, please tell we why my server is not able to receive any thing... 所以,请告诉我们为什么我的服务器无法接收任何东西...
Found the error. 发现错误。
Once you use accept()
you have a new file descriptor which you need to use send and receive data [lets say for that specific CLIENT]. 一旦使用accept()
您将拥有一个新的文件描述符,您需要使用该文件描述符来发送和接收数据(对于特定的CLIENT说)。
The accept() function shall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket. accept()函数将提取未决连接队列中的第一个连接,创建一个具有与指定套接字相同的套接字类型协议和地址族的新套接字,并为该套接字分配一个新的文件描述符。
In your server when you get a new connection you correctly use new file descriptor recv_size
to send data but you do not use that new connection's file descriptor to receive data. 在服务器中,当您获得新的连接时,可以正确使用新的文件描述符recv_size
来发送数据,但不要使用该新连接的文件描述符来接收数据。
Wrong code line 代码行错误
if((recv_size = recv(s , cleint_reply , 2000 , 0)) == SOCKET_ERROR)
Correction 更正
if((recv_size = recv(new_socket , cleint_reply , 2000 , 0)) == SOCKET_ERROR)
this should work now 这应该现在工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.