0

I'm trying to run the below program but I keep getting connection error's:

from socket import *
from codecs import decode

HOST = 'localhost'
PORT = 5000
BUFSIZE = 1024
ADDRESS = (HOST, PORT)

server = socket(AF_INET, SOCK_STREAM)
server.connect(ADDRESS)
dayAndTime = decode(server.recv(BUFSIZE), 'ascii')
print(dayAndTime)
server.close()

ERROR: ConnectionRefusedError: [Errno 61] Connection refused

Any idea what's going on?

6
  • 2
    is there anything listening on port 5000? Commented Jul 21, 2018 at 20:57
  • This is my first time coding using ports and host and this code was copied from my textbook so no, I'm not sure. How would I configure this? Commented Jul 21, 2018 at 21:13
  • You have to start a server first. Read the textbook more carefully. Commented Jul 21, 2018 at 21:15
  • I've read the chapter three times and it does not talk about creating a server. Am I not able to connect to my own computer to get the data/file needed? Commented Jul 21, 2018 at 21:24
  • 1
    You can only connect to a port your computer if some other program is listing on that port and ready to communicate with you. Were you shown how to set up a server in a previous chapter? Perhaps you're intended to keep it running. Commented Jul 21, 2018 at 21:47

1 Answer 1

3

If your book doesn't mention the other half of sockets, you need a better book.

Socket basics are easy. You have one process listen on a port, waiting for connections. Commonly we'll call this a 'server'. Another process (perhaps on the same machine, perhaps remote) attempts to connect to that port. We'll call that the client.

If no one is listening, then when the client attempts to connect they'll get your error Connection Refused.

So, set up a listening process. Below, on the left is server code; on the right is client code. Top-to-bottom is the "flow".

server = socket(AF_INET, SOCK_STREAM)  # <- just like your example
server.bind(ADDRESS)                   # rather than 'connect', we 'bind' to the port
server.listen(1)                       # bind "claims" the port, so next we call listen & wait...

# Meanwhile...
                                       # Your client process
                                       client = socket(AF_INET, SOCK_STREAM)
                                       client.connect(ADDRESS)
# It's only at this moment that the client reaches across the network to the server...
# On connect, the listening server wakes up, and needs to "accept" the connection
(s, remote_addr) = server.accept()

Once accepted, you can now send/recv on the s socket on the server-side, and send/recv from the client socket on the client side. Note that the server variable is not the socket to communicate on -- it's used to listen for new connections. Instead, you read/write on the socket object returned as first item of accept().

There's lots more to consider but this is at the heart of the Internet and has been pretty much unchanged since the 1980s.

Image from wikipedia entry for Berkeley Sockets: TCP Socket flow (from wikipedia "Berkeley Sockets")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.