简体   繁体   English

TCP套接字,“无法分配请求的地址”和“传输端点已连接”错误

[英]TCP Sockets, “cannot assign requested address” and “ Transport endpoint is already connected” errors

I am new to working with TCP sockets and I do not understand the errors I am getting when running my programs. 我是使用TCP套接字的新手,我不理解运行程序时遇到的错误。 I have two programs that when run simultaneous I would like to be able to pass messages between. 我有两个程序,当同时运行时,我希望能够在它们之间传递消息。 The initial message gets through but then when trying to return another message I get two errors. 初始消息通过,但是随后尝试返回另一条消息时,出现两个错误。 When I run my first program the output is: 当我运行第一个程序时,输出为:

Input Message: hello

Waiting for incoming connections...

Connection accepted

Message Sent

Connection error: Transport endpoint is already connected 

Now running the other program simultaneously gives: 现在,同时运行其他程序将给出:

Input Message: Hello Back

Connected

Message received

hello

Bind error: Cannot assign requested address

If anyone could explain these "cannot assign requested address" and " Transport endpoint is already connected" errors I would greatly appreciate it!! 如果有人能解释这些“无法分配请求的地址”和“传输端点已连接”错误,我将不胜感激! Thanks! 谢谢!

The first program is: 第一个程序是:

int main(int argc, char *argv[]) {

    int socket_info, new_socket;
    struct sockaddr_in server, client;
    char message[100];
    char incoming_message[100];

printf("Input Message: ");

fgets(message, 100, stdin);

        //create socket
        socket_info = socket(AF_INET, SOCK_STREAM, 0);
        if (socket_info == -1) {
        printf("Could not create socket");
        }

        //assign values
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_family = AF_INET;
        server.sin_port = htons( 1100 );

    int y=1;
        if(setsockopt(socket_info, SOL_SOCKET, SO_REUSEADDR, (char*)&y, sizeof(y)) == -1) {
    perror("set reuseaddr");
    return -1;
        }

        //binds socket
        if (bind(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
        perror("Bind error");
        return 1;
        }

        //listen
        listen(socket_info , 5);

        //waiting for connection
        puts("Waiting for incoming connections...");
        int c = sizeof(struct sockaddr_in);

        //accept connection
        new_socket = accept(socket_info, (struct sockaddr *)&client, (socklen_t*)&c);
        if (new_socket < 0){
        perror("accept failed");
        return 1;
        }
        puts("Connection accepted");

        //send message
        if( send(new_socket , message , strlen(message) , 0) < 0) {
        perror("Send failed");
        return 1;
        }
        puts("Message Sent");

        //connects
        if (connect(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
        perror("Connection error");
        return 1;
        }
        puts("Connected");

        //Receive an incoming message
        if( recv(socket_info, incoming_message , sizeof(incoming_message) , 0) < 0) {
        perror("Received failed");
        return 1;
        }
        puts("Message received");
    incoming_message[strlen(incoming_message)-1]=0;
        puts(incoming_message);

close(socket_info);
}

The second program is: 第二个程序是:

int main(int argc, char *argv[]) {

    int socket_info, new_socket;
    struct sockaddr_in server, client;
    char incoming_message[100];
    char message[100];

printf("Input Message: ");

fgets(message, 100, stdin);

        //create socket
        socket_info = socket(AF_INET, SOCK_STREAM, 0);
        if (socket_info == -1) {
        printf("Could not create socket");
        }

        //assign values
        server.sin_addr.s_addr = inet_addr("172.21.8.178");
        server.sin_family = AF_INET;
        server.sin_port = htons( 1100 );

        //connects
        if (connect(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
        perror("Connection error");
           return 1;
        }
        puts("Connected");

        //Receive an incoming message
        if( recv(socket_info, incoming_message , sizeof(incoming_message) , 0) < 0) {
        perror("Received failed");
        return 1;
        }
        puts("Message received");
    incoming_message[strlen(incoming_message)-1]=0;
        puts(incoming_message);

    int y=1;
       if(setsockopt(socket_info, SOL_SOCKET, SO_REUSEADDR, (char*)&y, sizeof(y)) == -1) {
    perror("set reuseaddr");
    return -1;
        }

        //binds socket
        if (bind(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
        perror("Bind error");
        return 1;
        }

        //listen
        listen(socket_info , 5);

        //waiting for connection
        puts("Waiting for incoming connections...");
        int c = sizeof(struct sockaddr_in);

        //accept connection
        new_socket = accept(socket_info, (struct sockaddr *)&client, (socklen_t*)&c);
        if (new_socket < 0){
        perror("accept failed");
        return 1;
        }
        puts("Connection accepted");

    //send message
        if( send(new_socket , message , strlen(message) , 0) < 0) {
        perror("Send failed");
        return 1;
        }
        puts("Message Sent");

close(socket_info);
}
  if (connect(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("Connection error");
    return 1;
    }

Connection error: Transport endpoint is already connected 

I asssume socket_info above should be new_socket ? 我假设上面的socket_info应该是new_socket吗?

You can't connect a listening socket. 您无法连接监听插座。

You don't need to connect the listening socket. 不需要连接侦听套接字。 You have just accepted a socket from a client connection. 您刚刚接受了来自客户端连接的套接字。 You should do your I/O to that client with that socket. 您应该使用该套接字对该客户端进行I / O。

In your second program: 在第二个程序中:

if(setsockopt(socket_info, SOL_SOCKET, SO_REUSEADDR, (char*)&y, sizeof(y)) == -1) {
perror("set reuseaddr");
return -1;
    }

This is futile. 这是徒劳的。 The socket is already bound, implicitly, via the connect() call preceding. 已通过前面的connect()隐式绑定了套接字。

    //binds socket
    if (bind(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("Bind error");
    return 1;
    }

Bind error: Cannot assign requested address

You can't bind a socket that is already connected. 您无法绑定已连接的套接字。

Nor can you bind a socket to a remote address. 您也不能将套接字绑定到远程地址。

Nor can you listen on a connected socket. 您也不能在连接的插座上收听。

Nor can you accept from it. 您也不能接受。 You don't need to bind, or listen, or accept from it. 不需要绑定,听或接受它。 You are already connected to the peer. 您已经连接到对等方。

In short your code doesn't make any sense whatsoever. 简而言之,您的代码毫无意义。 You need to find a proper tutorial and study it. 您需要找到适当的教程并进行研究。

incoming_message[strlen(incoming_message)-1] = 0;

This makes even less sense. 这更没有意义。 Here you are searching for a trailing null byte (which may not even be there) and replacing the byte before it by ... a null byte. 在这里,您正在寻找一个尾随的空字节(可能甚至不存在),并将其之前的字节替换为...一个空字节。 Why? 为什么?

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

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