[英]C Client Server doesn't work as expected
I have this simple client-server written in C
below. 我下面有用
C
编写的简单客户端服务器。 The client sends 2 numbers to the server and the server sends back to the client the sum of the 2 numbers. 客户端向服务器发送2个数字,服务器将2个数字的总和发送回客户端。 My problem is with sending back the sum from server to the client, I can't see why it does not work.
我的问题是将总和从服务器发送回客户端,我不明白为什么它不起作用。 it doesn't give any error, just the
receivedData
variable is not filled with the desired int
. 它没有给出任何错误,只是
receivedData
变量未填充所需的int
。 The client successfully sends the packet with the 2 numbers to the server and the server successfully receives them. 客户端成功将带有2个数字的数据包发送到服务器,服务器成功接收到它们。
Please note that this is just a didactic example, and I didn't use threads for handling clients. 请注意,这只是一个教学示例,我没有使用线程来处理客户端。 It's just one client and one server.
它只是一个客户端和一台服务器。
Here is the server: 这是服务器:
// SERVER
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#define PORT 2012
struct Packet
{
int a;
int b;
};
int main()
{
struct sockaddr_in clientAddress;
int sd;
if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("Error creating socketsss\n");
}
memset(&clientAddress, 0, sizeof(clientAddress));
clientAddress.sin_family = AF_INET;
clientAddress.sin_port = htons(PORT);
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sd, (struct sockaddr*)&clientAddress, sizeof(clientAddress)) < 0)
{
printf("Bind error\n");
}
if(listen(sd, 5) < 0)
{
printf("Listen error\n");
}
unsigned int len = 0;
int fd = accept(sd, (struct sockaddr*)&clientAddress, &len);
struct Packet received;
if(recv(fd, &received, sizeof(struct Packet), 0) < 0)
{
printf("Receive error");
}
int sum = received.a + received.b;
printf("Numarul ce trebui trimis este %d\n", sum);
if(send(sd, &sum, sizeof(sum), 0) < 0)
{
printf("Error sending data\n");
}
return 0;
}
And here is the client: 这是客户:
// CLIENT
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SERVER_IP "127.0.0.1"
#define PORT 2012
struct Packet
{
int a;
int b;
};
int main()
{
struct sockaddr_in serverAddress;
int sd;
if((sd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("Error creating socket\n");
}
memset((char *)&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP);
serverAddress.sin_port = htons(PORT);
if ((connect(sd,(struct sockaddr *)&serverAddress, sizeof(serverAddress))) < 0)
{
printf("Error connectiong to server!\n");
}
struct Packet packToSend;
packToSend.a = 14;
packToSend.b = 21;
if(send(sd, &packToSend, sizeof(packToSend), 0) < 0)
{
printf("Error sending data\n");
}
int receivedData = 0;
if(recv(sd, &receivedData, sizeof(int), 0) < 0)
{
printf("Receive error\n");
}
printf("I received %d \n", receivedData);
return 0;
}
Thanks! 谢谢!
您正在发送监听套接字,而不是您接受的套接字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.