我在使用套接字连接时遇到问题 . 我正在尝试让客户端连接到服务器并向服务器发送一些数据 . 如果数据是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;
}
1 回答
错误发生在服务器线程中 . 您使用错误的套接字描述符 .
从 accept(2) 的联机帮助页:
因此,返回值(
remote_sockid
)应该用于read(2)/ write(2)调用: