1 / 7

Sockets

Sockets.

alyssa-duke
Télécharger la présentation

Sockets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sockets Sockets are an extension to the operating system's I/O system that enable communication between processes and machines. a socket can be treated the same as a standard file descriptor. On UNIX-like platforms, system calls such as read(), write(), dup(), dup2(), and close () will work for sockets as well as for standard file descriptors. However, there are ways that a socket is different. The most obvious is the way in which a socket is created. While many files are opened with the open() call, sockets are created with the socket() call and additional calls are needed to connect and activate them. The system calls recv() and send() exist as counterparts to read() andwriteQ.

  2. Sockets • The protocol family is typically either SOCK_STREAM for TCP communications or SOCK_DGRAM for UDP communications. For a TCP connection, creating a socket generally uses code like this: s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) • To connect the socket, you'll generally need to provide a tuple containing the remote hostname or IP address and the remote port. Connecting a socket typically looks like this: s.connect(("www.example.com", 80))

  3. Sockets program-01 import socket print "Creating socket...", s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) print "done." print "Connecting to remote host...", s.connect(("www.google.com", 80)) print "done."

  4. Sockets program-02 import socket print "Creating socket...", s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print "done." print "Looking up port number...", port = socket.getservbyname('http', 'tcp') print "done." print "Connecting to remote host on port %d..." % port, s.connect(("www.google.com", port)) print "done."

  5. Sockets program-03 import socket print "Creating socket...", s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print "done." print "Looking up port number...", port = socket.getservbyname('http', 'tcp') print "done." print "Connecting to remote host on port %d..." % port, s.connect(("www.google.com", port)) print "done." print "Connected from", s.getsockname print "Connected to", s.getpeername

  6. Socket errors • socket.error for general I/O and communication problems • socket.gaierror for errors looking up address information • socket.herror for other addressing errors (corresponding to h_errno in C) • socket.timeout for handling timeouts that occur after settimeout() has been called on a socket

  7. Socket errors - example import socket, sys, time host = sys.argv[i] textport = sys.argv[2] filename = sys.argv[3] try: s = socket.socket(socket.AF_INET,\ socket.SOCK_STREAM) except socket.error, e: print "Strange error creating \ socket: %s" % e sys.exit(1)

More Related