TCP Socket在C程序中因“端点未连接”而失败(TCP Socket fails with “endpoint is not connected” in C program)

编程入门 行业动态 更新时间:2024-10-14 22:21:31
TCP Socket在C程序中因“端点未连接”而失败(TCP Socket fails with “endpoint is not connected” in C program)

我在使用套接字连接时遇到问题。 我正在尝试让客户端连接到服务器并向服务器发送一些数据。 如果数据是1个字节,服务器将ping回该字节。 该代码应该用于对网络进行基准测试。

目前代码死了,出现以下错误:

TCP server recv() failed: Transport endpoint is not connected TCP Sever send failed: Connection reset by peer

为什么连接失败?

这是服务器线程的代码。

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; }

以下是客户端线程。

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; }

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. 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; }

最满意答案

错误发生在服务器线程中。 您使用错误的套接字描述符。

接受(2)的联机帮助页:

成功时,这些系统调用返回一个非负整数,它是接受套接字的文件描述符。 出错时,返回-1,并正确设置errno。

所以返回值( 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"); } }

The error is in the server thread. You use the wrong socket descriptor.

From the manpage of 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.

So the return value (remote_sockid) should be used for read(2)/write(2) calls:

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"); } }

更多推荐

本文发布于:2023-08-06 20:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中因   程序   Socket   TCP   program

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!