cosc 2p91
play

COSC 2P91 Sockets Week 10a Brock University Brock University - PowerPoint PPT Presentation

COSC 2P91 Sockets Week 10a Brock University Brock University (Week 10a) Sockets 1 / 14 Socket programming Network programming is (of course) a pretty large topic, with numerous levels of protocols, and multiple options at each level. Peer


  1. COSC 2P91 Sockets Week 10a Brock University Brock University (Week 10a) Sockets 1 / 14

  2. Socket programming Network programming is (of course) a pretty large topic, with numerous levels of protocols, and multiple options at each level. Peer to peer, or client-server? Reliable or unreliable communication? A single connection between two hosts, or multiple connections? Connection-oriented or connectionless? Checkpointing? Authentication? Security? ASCII text or Unicode? Raw byte streams? Serialization? Time-sensitive? Lockstepping? Obviously, we don’t have a chance of covering all aspects of socket programming. If you’re interested in knowing some of the more intricate details of network communications, you should consider taking COSC 3P01 4P14 2P13. Brock University (Week 10a) Sockets 2 / 14

  3. Socket Programming Let’s step back a bit First, let’s focus on the client-server model. A client contacts a server to make one or more requests A server waits for requests from clients, and fulfills them Typically, if a server could potentially be servicing more than one request at a time, it will usually be threaded. Let’s assume a connection-oriented service ◮ Connection-oriented typically means a byte stream approach (as opposed to the datagram approach typical to connectionless communication) ◮ Compare the operations of web servers to (simple) DNS servers Brock University (Week 10a) Sockets 3 / 14

  4. Socket Programming So, what do we need? For the simplest of socket programming, we’re going to neet a few things: A socket ◮ Think of a socket as an end-point handle for communication ◮ It’s similar in use to a file handle, except it’s an entry/exit point for a network (or internet ) layer A socket’s identified by an IP address and port number ◮ Technically, the connection will be identified by the IP addresses and port numbers on both ends of said connection A buffer ◮ Of course, this is an integral part of any other remotely useful IO. Perhaps we should talk more about that some time? Brock University (Week 10a) Sockets 4 / 14

  5. Socket Programming So, how do we do it? This is where I would normally walk you through precisely how to do whatever technique we’re trying to learn. Except... effective network programming in C is a nightmare. It’s also tedious ◮ It also isn’t very portable ⋆ I’m super cereal here So, we can take a look at an example or two, but we won’t be writing any variations of our own. Brock University (Week 10a) Sockets 5 / 14

  6. Socket Programming Managing communication As mentioned, servers tend to need to be threaded, so that multiple requests can be serviced simultaneously. This also helps us have the flexibility of deciding whether or not we want to use blocking IO ◮ If we aren’t dealing with blocking IO, we can potentially maintain a data structure of all current connections, and then repeatedly iterate through them checking for additional bytes The alternative is to avoid a connection-oriented service entirely ◮ I should have already explained datagrams by now ◮ If not, imagine writing down messages onto paper airplanes and then chucking them out the window ⋆ Maybe they get to their intended recipient ⋆ Maybe they fall into the mud ⋆ If you want to send another message, it’s an independent event from the first ⋆ If you throw two airplanes, there’s no way to be certain which will arrive first Brock University (Week 10a) Sockets 6 / 14

  7. So, what’s the alternative? If you still want to use C (as opposed to C#, C++, The Old Man and the C, etc.), you’ll probably want to rely on a third-party library. Since the primary problems are portability and tediousness, a decent library can simply abstract most of that away Of course, the other alternative would be to... not use C for socket programming? Brock University (Week 10a) Sockets 7 / 14

  8. Why would we want to bother with it, then? Besides the obvious answer of, because we need it ? We’ve actually already covered this before. We can use networking as an alternate mechanism for interprocess communication ◮ Basically, the server and client reside on the same computer A great example of this is the X window system Brock University (Week 10a) Sockets 8 / 14

  9. So... is networking always so hard? Well, it’d take great effort to make it any harder, eh? Most modern languages tend to include libraries for at least socket programming ◮ They might even provide additional libraries for specific uses, like webpage scraping, sending emails, etc. Java is really fantastic for networking, but since this isn’t a networking course (because, why would we want to have one of those?), let’s focus on a language we’ve already used: Python Brock University (Week 10a) Sockets 9 / 14

  10. Socket programming in Python Ostensibly, Python’s socket library is somewhat based on C’s. In practice though, it’s orders of magnitude simpler We still need to decide whether we want to write a socket for use in a server or a client, and whether it will deal with datagrams or a stream Let’s look at a very simple example. Brock University (Week 10a) Sockets 10 / 14

  11. ... a client in four lines of code? What did you expect? It’s Python Brock University (Week 10a) Sockets 11 / 14

  12. Socket programming in Python Of course, ‘real’ uses will obviously be noticeably more complicated. Whether C or Python, we’ll likely still need to deal with threading As always, the official documentation is pretty good: https://docs.python.org/2/library/socket.html https://docs.python.org/2/library/socketserver.html#module-SocketServer https://docs.python.org/2/howto/sockets.html Brock University (Week 10a) Sockets 12 / 14

  13. Using existing protocols Of course, many commonly-used internet application protocols are based on plaintext transmissions. As such, it isn’t hard to write a simple program that can make use of these protocols. Writing a webpage scraper to extract data in Python is trivial It’s easy to write a script to send email ◮ You could either explicitly communicate with the SMTP server, or just use the built-in SMTP library ◮ Combine it with CGI, and you can very easily create things like automated mailing lists, help ticket systems, etc. You could even create your own basic FTP client if you wanted to Neat reading: http://en.wikibooks.org/wiki/Python Programming/Networks http://en.wikibooks.org/wiki/Python Programming/Internet Brock University (Week 10a) Sockets 13 / 14

  14. Funny anecdotes? Comments? Questions? Brock University (Week 10a) Sockets 14 / 14

Recommend


More recommend