The Sockets Server

Please follow along with Kyle's starter code which is modified from Rohan's starter code. You probably won't code much in the server, but it's definitely helpful to know about it. The file we're looking at is dots_server.py.

The Code (dots_server.py)

import socket
import threading
from queue import Queue

HOST = ""
PORT = 50003
BACKLOG = 4

Why did I just import threading? How do sockets even work? Why isn't "queue" spelled "q" instead?

... are some questions you probably have ... so lets get to answering them! (We might not get to the last one)

Threads

Basically, threading is where the computer does multiple things at once. So if I'm on my laptop playing my fav game flappy bird, the computer might use threading to

  1. Run the game (moving the bird, generating pipes)
  2. Play the sick beats (music) in the background

and it will appear to be doing both at the same time! In reality, the computer can only do one thing at once, so it switches between the two really quickly, so fast that you don't even notice.

Queues

Queues are just a fancy CS term for a line. So at my favorite store Trader Joe's when I go to buy crunchy organic non-salted peanut butter and organic reduced-sugar strawberry jelly, I have to get in the back of the line (called an enqueue) and wait until everyone in front of me has paid for their groceries (or are dequeued) before its my turn.

Like a queue, the Trader Joe's checkout line has a couple of rules. For one, everyone must enter from the back (no cutting!) and exit from the front. Also, in theory, a queue has unlimited length (as I found out the hard way when I went to TJ's on a Sunday). Python's queue module has you specify the length of a queue, however, so it is not unlimited.

Host

HOST = ""

Since we are not using physical servers to host our sockets game, we designate a "host" or computer that will run the server for us. If you are unfamiliar with what a server is, it is basically a common meeting place where all messages go through.

If you are running all the windows on your own computer, you can just leave host as an empty string, but if you are playing with your friends in California, you'll need to appoint a host and fill in their IP address instead.

Port

PORT = 50003

In terms of sockets, a port is basically like a TV channel. It specifies which port to use (channel to watch) so that all connections will go through that channel. For example, say I want to watch some sports, and ESPN is on port 50001. Lets say that's giving me a bad signal, so I go to ESPN2 on 50002, but that is also fuzzy. Then I'll try ESPN Deportes on 50004, ESPN Movies on 50005 and so on until I get my sports. This is how you should view ports when running your code. Change it till it works! (DO NOT use 8000 or anything less than five digits though, those are the naughty channels and your TV has parental controls activated. Bad things happen)

Backlog

BACKLOG = 4

This is the amount of people that are allowed to join the server. So if you made Scrabble for your TP, you'd want this to be 4 because Scrabble has a max of 4 players. But if you made club penguin, it would be 0 because that game no longer exists.

results matching ""

    No results matching ""