Socket Server

Do you want to make a socket server app?

A server can be created using the module socket.

If you make a socket server, you can define your own application protocol. You can also use it to work with existing apps.

Related Courses

The Algorithm

Sockets work on the application layer, it does not specify any protocol and on this level you’d define an application protocol yourself.

Creation of a socket server needs these steps:

  • bind socket to port
  • start listening
  • wait for client
  • receive data

Example socket server

The example below starts a socket server on port 7000. You can use telnet or a socket client to connect to this port.

Once run, a server will be running on localhost port 7000.

# Socket created
# Socket bind complete
# Socket now listening
# Connected to 127.0.0.1:40499

Once running it will wait for messages. To connect with it, use telnet or modify the socket client from the previous section.

Previous Post
Next Post

4 Replies to “Socket Server”

  1. what should i do in order to send an http response (html code), which includes photos. so that i can get or download the image from other pc.

    • upload the photo to a server and attach a link in the html response. You can also include it in binary, but think a link is a better way.

  2. Would the “while True” block not execute indefinitely? Would s.close() statement ever be executed? Would this script ever listen for a new client ever again with “conn, addr = s.accept()” being outside the loop?

Leave a Reply

Your email address will not be published. Required fields are marked *