简体   繁体   English

C-首次调用recv()后,“传输端点未连接”

[英]C - “Transport endpoint is not connected” after first recv() call

I'm just starting to learn network programming in C. I did some tests, but i got stuck with an error. 我刚刚开始学习C语言中的网络编程。我做了一些测试,但遇到了错误。

I have a client: 我有一个客户:

client.c 客户端

#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

int main(void)
{
    struct addrinfo hints, *res;
    int sock;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    getaddrinfo("localhost", "5996", &hints, &res);
    sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    connect(sock, res->ai_addr, res->ai_addrlen);
    char data[64];
    int len = 13;
    int br = recv(sock, data, len, 0);
    printf("%s\n%s\n%d\n", strerror(errno), data, br);
    return 0;
}

and a server: 和一台服务器:

server.c 服务器

#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MYPORT "5996"
#define BACKLOG 10

int main(void)
{
    struct sockaddr_storage their_addr;
    socklen_t addr_size;
    struct addrinfo hints, *res;
    int sockfd, new_fd;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    getaddrinfo(NULL, MYPORT, &hints, &res);

    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    bind(sockfd, res->ai_addr, res->ai_addrlen);
    listen(sockfd, BACKLOG);

    addr_size = sizeof their_addr;
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
    char *msg = "Hello World!!";
    int len = strlen(msg);
    int bs = send(new_fd, msg, len, 0);
    close(sockfd);
}

When i start the server, it waits for a connection, and if i start the client i get the message "Hello World!!", but then, for a minute or so, if i try to run the server and then run the client again, i get the message "Transport endpoint is not connected" from the strerror() call. 当我启动服务器时,它会等待连接,如果我启动客户端,则会收到消息“ Hello World !!”,但是,如果要尝试运行服务器然后再运行客户端,请等待一分钟左右再次,我从strerror()调用中收到消息“传输端点未连接”

I did read other questions about this, but the problem was that the data should be sent to the socket returned from the accept() call ...but i think that's what i'm doing. 我确实读过其他有关此问题,但是问题是数据应该发送到从accept()调用返回的套接字中……但是我认为这就是我正在做的事情。 What am I doing wrong?? 我究竟做错了什么?? ...i know it is something stupid, but i'm a real beginner. ...我知道这有点愚蠢,但是我是一个真正的初学者。

If you restart your server on the same port every time, you may be finding that the port is still treated as being in use by the OS. 如果每次都在同一端口上重新启动服务器,则可能会发现该端口仍被OS视为正在使用。 This is to allow time for delivery of any in-progress requests for clients from the last server run. 这是为了允许有时间从上次服务器运行向客户端传递任何正在进行的请求。

You can over-ride this by calling setsockopt for opcode SO_REUSEADDR 您可以通过为操作码SO_REUSEADDR调用setsockopt来覆盖此设置

sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
int reuse = 1;
int err = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if (0 == err)
    err = bind(sockfd, res->ai_addr, res->ai_addrlen);

Note that the above snippet checks error returns. 请注意,以上代码段会检查错误返回。 You'd have discovered the problem (if not the solution) yourself here if you checked the return code of each call. 如果您检查了每个调用的返回码,您将自己在这里发现问题(如果不是解决方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM