CSC323 2010S, Class 17: Sockets
Overview:
* Sockets: Background.
* Building Server Sockets.
* Building Client Sockets.
* Sending and Receiving Data.
* Avoiding Blocking.
* Detour: Representing Numbers.
* Exercises.
Admin:
* Welcome back! I hope you had a great break.
* For Thursday: Please skim An Introductory 4.4BSD Interprocess Communication Tutorial and submit two questions.
* Please work hard on your projects. (Remember, at least ten hours per person, unless you've arranged with me and your colleagues in advance.)
* EC for Today's CS and Disability talk (4:30 in Science 3821).
* EC for Wednesday's CS and Disability talk (4:15 in JRC101).
* EC for Thursday's convocation.
* EC for Thursday's "Teaching Millenials" CS Extra (4:30 in Science 3821).
* EC for Friday's "Computational Games" CS Extra (noon in Science 3821; Free Pizza; I need an approximate count).
* About ten
* EC For Ian Bone's informal (brown bag) lunch on Technical Interview Tips Thursday at noon in the Commons
* Or "My travels from one evil empire to another"
Sockets: Everyone's favorite networking technology
* A library for networking, developed as part of BSD 4.2
* An old networking technology
* Probably a stable technology (bugs worked out)
* Something has to be good about it for it to have survived so long
+ Flexible?
+ Easy to use?
+ Simple?
* May not be perfectly applicable to modern networks
* Designed to support multiple models of communcation
* Streams (continuous connectiosn)
* Datagrams (packet-style connections)
* Closely aligned with UNIX and IP
* We'll focus on stream-style connections
Set up Servers
* Create a socket and specify the kind of socket
* Attach that socket to a port on the server
* /etc/services lists common ports
* Indicate how many connections you're willing to queue
* Get information on one of the queued connections
* On Internet connections: Address of the client, and a socket to use to communicate
Set up Clients
* Create a socket and specify the kind
* Connect the socket to the server
Read/Write (Send/Receive)
* send(bytes)
* recv(numbytes)
Other Stuff
* What do we do about blocking?
* Multithreading
* Non-blocking versions of send and recv
* Socket libraries normally let you set a timeout for send/recv
* Clever precondition checking in the library
select
* select.select(SOCKETS-TO-CHECK-FOR-READ-OR-ACCEPT-AVAILABILITY,
SOCKETS-TO-CHECK-FOR-WRITE-AVAILABILITY,
SOCKETS-WE-WONT-WORRY-ABOUT,
SECONDS-TO-WAIT-BEFORE-GIVING-UP)
* Returns a tuple of three lists, each of which consists of only the
available things.
def slurp():
while (True):
(readies,writies,other) = select.select([s,s1,s2,s3], [], [])
if (len(readies) == 0):
print("Done")
return
for r in readies:
print(r.recv(128).decode())
How do we send numeric data?
* struct.pack('i', integer)
* struct.unpack('i', bytes)
Converting from host to network to host
socket.htonl
socket.ntohl
---
---
=Exercises=
One-shot responses
* Write a server that
+ Accepts a connection
+ Reads string of length up to 128 from that connection
+ Generates an "interesting" response string (e.g., you can
prefix the string with "Hello", play "The Name Game",
or whatever.
+ Sends back the response.
+ Closes the connection.
* Write a client that
+ Gets a string from the user
+ Creates a connection
+ Sends the string over the connection
+ Reads a response from the connection (no more than 256 characters)
+ Prints out the response
+ Closes the connection
One-shot responses, revisited
* Update your previous program so that the client can accept an arbitrary
length response from the server
A dialog
* Extend your prior program so that the client can send multiple requests
to the server through one connection.
+ The user provides each request (after prompting)
+ The client closes the connection when the user no longer provides
input.
* Note that the client still needs to accept arbitrarily long responses
from the server.
A numeric dialog
* Rewrite your prior program so that the dialog uses four-byte numbers, rather
than strings. For example, your client might send an integer and the
server could respond with the square of that integer.
* Please don't print and parse your strings.
* Note that for safety, you should use socket.htonl
and
socket.ntohl
.
A chat server
* Write a program in which the server accepts multiple simultaneous
connections, reads text from any of the active connections, and
sends that text to all of the connections (except the one from which
it received).
* If you have time, add support for disconnection.