sockets n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Sockets PowerPoint Presentation
Download Presentation
Sockets

Loading in 2 Seconds...

play fullscreen
1 / 7

Sockets - PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on

Sockets.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Sockets' - alyssa-duke


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
sockets
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.

sockets1
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))

sockets program 01
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."

sockets program 02
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."

sockets program 03
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

socket errors
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
socket errors example
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)