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;
}
The error is in the server thread. You use the wrong socket descriptor.
From the manpage of : 的联机帮助页:
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");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.