[英]TCP Socket fails with “endpoint is not connected” in C program
I'm having problems getting a socket connection to work. 我在使用套接字连接时遇到问题。 I'm trying to have a client connect to a server and to send the server some data. 我正在尝试让客户端连接到服务器并向服务器发送一些数据。 If the data is 1 byte, the server will ping pong the byte back. 如果数据是1个字节,服务器将ping回该字节。 This code is supposed to benchmark a network. 该代码应该用于对网络进行基准测试。
Currently the code dies with the following error: 目前代码死了,出现以下错误:
TCP server recv() failed: Transport endpoint is not connected
TCP Sever send failed: Connection reset by peer
Why is the connection failing? 为什么连接失败?
This is the code for the server thread. 这是服务器线程的代码。
void *tcp_server(void * context){
printf("TCP Server has started\n");
struct thread_parameters * read_params = context;
int block_size = read_params->block_size;
int thread_id = read_params->thread_id;
int base_port = read_params->base_port;
struct sockaddr_in remote_address;
struct sockaddr_in my_address;
int recvMsgSize;
my_address.sin_family = AF_INET;
my_address.sin_port = htons(base_port + thread_id);
my_address.sin_addr.s_addr = htonl(INADDR_ANY);
int sockid = socket(PF_INET, SOCK_STREAM, 0);
int remote_sockid;
read_params->sockid = sockid;
char buffer[block_size];
socklen_t size_remote_address = sizeof(remote_address);
if (bind(sockid, (struct sockaddr *) &my_address, sizeof(my_address))<0)
DieWithError("TCP server Unable to bind socket.\n");
printf("TCP Server has bound\n");
if (listen(sockid, 5) < 0)
DieWithError("TCP server Listen Failed");
while(!read_params->work_done){
if ((remote_sockid=accept(sockid,(struct sockaddr *)&remote_address,&size_remote_address))<0)
DieWithError("TCP server accept() failed");
if ((recvMsgSize = recv(sockid, buffer, block_size, 0)) < 0)
DieWithError("TCP server recv() failed");
if (block_size == 1){
if (send(sockid, buffer, recvMsgSize, 0)!= recvMsgSize){
DieWithError("TCP server send() failed");
}
}
close(remote_sockid);
}
return 0;
}
The following is the client thread. 以下是客户端线程。
void *tcp_client(void * context){
printf("TCP Client has started\n");
struct thread_parameters * read_params = context;
int block_size = read_params->block_size;
char *work = read_params->work;
long work_size = read_params->size_of_subset;
int thread_id = read_params->thread_id;
int iterations = read_params->iterations;
int base_port = read_params->base_port;
char * server_address = read_params->server_address;
struct sockaddr_in my_address;
struct sockaddr_in remote_address;
my_address.sin_addr.s_addr = htonl(INADDR_ANY);
my_address.sin_family = AF_INET;
remote_address.sin_family = AF_INET;
my_address.sin_port = htons(base_port + thread_id);
remote_address.sin_port = htons(base_port + thread_id);
int sockid = socket(PF_INET, SOCK_STREAM, 0);
int sendMsgSize = block_size;
char buffer[block_size];
char * message;
socklen_t size_remote_address = sizeof(remote_address);
inet_pton(AF_INET, server_address, &(remote_address.sin_addr));
if (bind(sockid, (struct sockaddr *) &my_address, sizeof(my_address))<0){
DieWithError("TCP Client Unable to bind socket.\n");
}
printf("TCP Client TCP Client has bounded\n");
if (connect(sockid, (struct sockaddr *) &remote_address, size_remote_address) < 0)
DieWithError("TCP Client connect() failed");
int j;
int i;
int sent_size;
for (j=0; j<iterations; j++){
for(i=0;i<work_size; i=i+block_size){
message = &work[i];
sent_size = sendMsgSize;
while (sent_size > 0) { /* zero indicates end of transmission */
sent_size = send(sockid, message, block_size, 0);
}
if(sent_size != 0) DieWithError("TCP Sever send failed");
if (block_size == 1){
if (recv(sockid, buffer, sendMsgSize, 0) > 0){
DieWithError("TCP Client recv() failed");
}
}
}
}
close(sockid);
printf("iterations: %i\n", j);
printf("work size: %i\n", i);
return 0;
}
The error is in the server thread. 错误发生在服务器线程中。 You use the wrong socket descriptor. 您使用错误的套接字描述符。
From the manpage of accept(2) : 从accept(2)的联机帮助页:
On success, these system calls return a nonnegative integer that is a file descriptor for the accepted socket. 成功时,这些系统调用返回一个非负整数,它是接受套接字的文件描述符。 On error, -1 is returned, and errno is set appropriately. 出错时,返回-1,并正确设置errno。
So the return value ( remote_sockid
) should be used for read(2)/write(2) calls: 因此,返回值( remote_sockid
)应该用于read(2)/ write(2)调用:
if ((remote_sockid=accept(sockid,(struct sockaddr*)&remote_address,&size_remote_address))<0)
DieWithError("TCP server accept() failed");
if ((recvMsgSize = recv(remote_sockid, buffer, block_size, 0)) < 0)
DieWithError("TCP server recv() failed");
if (block_size == 1){
if (send(remote_sockid, buffer, recvMsgSize, 0)!= recvMsgSize) {
DieWithError("TCP server send() failed");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.