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

前端 未结 3 2168
死守一世寂寞
死守一世寂寞 2021-02-20 04:42

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.

I get a \"Connectio

相关标签:
3条回答
  • 2021-02-20 05:13

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

    [root@mg0008 work]# nc -l 127.0.0.1 1234 &
    [1] 25380
    [root@mg0008 work]# ./socket
    Give message: Hello
    Hello
    
    0 讨论(0)
  • 2021-02-20 05:17

    According to the man page:

    ECONNREFUSED No-one listening on the remote address.


    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):

    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()
    
    0 讨论(0)
  • 2021-02-20 05:22

    probably no server listening port 1234 in your local host

    0 讨论(0)
提交回复
热议问题