Linux 套接字错误:连接被拒绝 - 我做错了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14344694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:40:48  来源:igfitidea点击:

Socket error: connection refused - what am I doing wrong?

clinuxsockets

提问by Kalec

I've just started learning the basics of sockets (Linux). I tried my hand at a small example, but it doesn't work and I have no idea what's wrong.

我刚刚开始学习套接字(Linux)的基础知识。我尝试了一个小例子,但它不起作用,我不知道出了什么问题。

I get a "Connection Refused" error message.

我收到“连接被拒绝”错误消息。



Here's my code:

这是我的代码:

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

int main() {
    int c;
    c = socket(AF_INET, SOCK_STREAM, 0);
    if (c < 0) {
        printf("Error in creating socket! %s\n", strerror(errno));
        return 1;
    }

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_port = htons(1234);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("127.0.0.1"); //local host
    if (connect(c, (struct sockaddr *)&server, sizeof(server)) < 0) {
        // Here is my error
        printf("Error when connecting! %s\n",strerror(errno)); 
        return 1;
    }

    while(1) {
        char msg[100];
        printf("Give message: ");
        fgets(msg, sizeof(msg), stdin); 
        send(c, &msg, sizeof(msg), 0);
        char resp[100];
        recv(c, &resp, sizeof(resp), 0);
        printf("Received: %s\n", resp);
    }
close(c);
}

EDIT

编辑

Of course ! the error was actually in the server. I simply found it weired that the client sent the message, so I narrowed my view, didn't even bother looking back at the server.

当然 !错误实际上是在服务器中。我只是发现客户端发送消息很奇怪,所以我缩小了视野,甚至懒得回头看服务器。

Since the error seems to be also in my server, I might end up asking another question and linking it here

由于错误似乎也在我的服务器中,我可能最终会问另一个问题并将其链接到这里

Server was listening to (12345) ...

服务器正在侦听 (12345) ...

采纳答案by moooeeeep

According to the man page:

根据手册页

ECONNREFUSEDNo-one listening on the remote address.

ECONNREFUSED无人监听远程地址。



In order to provide a simple remote endpoint that accepts your connection and sends back the received data (echo server), you could try something like this python server(or to use netcat):

为了提供一个简单的远程端点来接受您的连接并发回接收到的数据(回声服务器),您可以尝试像这个python 服务器这样的东西(或使用netcat):

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", 1234))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()

回答by Kalec

probably no server listening port 1234 in your local host

本地主机中可能没有服务器侦听端口 1234

回答by Satish

Your Answer is: You program is client and it need a server to connect. nccommand create server and your program can connect to it.

你的答案是:你的程序是客户端,它需要一个服务器来连接。nc命令 create server 并且您的程序可以连接到它。

[root@mg0008 work]# nc -l 127.0.0.1 1234 &
[1] 25380
[root@mg0008 work]# ./socket
Give message: Hello
Hello