[英]C client doesn't receive from server
I made a simple server client program with select()
but the problem is that the client doesn't receive the welcome message.我用
select()
制作了一个简单的服务器客户端程序,但问题是客户端没有收到欢迎消息。 I did this several times earlier without select()
and it worked, but all of a sudden it doesn't want to work when working with select()
.我之前在没有
select()
的情况下做了几次,它工作了,但是突然之间它不想在使用select()
时工作。 What am I doing wrong?我究竟做错了什么?
Server:服务器:
#define MAX_CLIENTS 10
int main()
{
system("clear");
printf("***SERVER STARTED***\n");
int master_socket = socket(AF_INET, SOCK_STREAM, 0);
fd_set fdsForReading;
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8888);
bind(master_socket, (struct sockaddr *)&address, sizeof(address));
listen(master_socket, 3);
while (1)
{
FD_ZERO(&fdsForReading); // Clearing the file descriptor set
FD_SET(master_socket, &fdsForReading); // Add master_socket to file descriptor set
printf("Monitoring for connections...\n");
select(master_socket + 1, &fdsForReading, NULL, NULL, NULL);
int accepted_socket = accept(master_socket, (struct sockaddr *)&address, sizeof(address));
printf("[+] New connection accepted\n");
char buffer[125] = "Welcome to my server";
send(accepted_socket, buffer, sizeof(buffer), 0);
printf("End of while loop\n");
}
}
Client:客户:
int main()
{
system("clear");
int clientSocket = socket(AF_INET, SOCK_STREAM, 0), x;
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8888);
serverAddress.sin_addr.s_addr = INADDR_ANY;
connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
char serverResponse[125];
recv(clientSocket, &serverResponse, sizeof(serverResponse), 0);
printf("test: %s\n", serverResponse);
scanf("%d", &x);
close(clientSocket);
}
&serverResponse
should be serverResponse
&serverResponse
应该是serverResponse
recv
may receive fewer characters than you sent so you should check how many characters you received and possibly concatenate what you receive until you have the full message. recv
收到的字符可能少于您发送的字符,因此您应该检查收到的字符数,并可能将收到的内容连接起来,直到您收到完整的消息。\0
if what you receive is to be used with functions like printf("%s", ...
that require null terminated strings. In your case you actually send 125
char
s even though the string is much shorter. You should probably not do that. This will send the string + the null terminating character:\0
如果您收到的内容将与printf("%s", ...
需要 null 终止字符串的函数一起使用。在您的情况下,您实际上发送了125
char
s 即使字符串短得多。你可能不应该这样做。这将发送字符串 + null 终止字符: send(accepted_socket, buffer, strlen(buffer) + 1, 0);
accept
should be a socklen_t *
- not sizeof(address)
. accept
的第三个参数应该是socklen_t *
- 而不是sizeof(address)
。 Example: socklen_t addrlen = sizeof address; int accepted_socket = accept(master_socket, (struct sockaddr *)&address, &addrlen);
With those changes, the client will receive the greeting fine given that no errors occurs - but when you've added error checking you will see where the problem is in that case.通过这些更改,如果没有发生错误,客户将收到很好的问候 - 但是当您添加错误检查时,您将看到在这种情况下问题出在哪里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.