An Introduction to Computer Networks - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

An Introduction to Computer Networks

Description:

An Introduction to Computer Networks Lecture 3: Socket Programming University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani * – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 17
Provided by: Larry433
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to Computer Networks


1
An Introduction to Computer Networks
Lecture 3 Socket Programming
  • University of Tehran
  • Dept. of EE and Computer Engineering
  • By
  • Dr. Nasser Yazdani

2
Outline
  • Socket programming
  • What is a Socket?
  • Why do we need sockets?
  • Types of sockets
  • Uses of sockets
  • Example applications
  • Code

3
Socket programming
  • Goal- Communication between two processes
  • They use interface/services from the transport
    layer. The interface is called Application
    Programming Interface, API.

Server process
Client process
TCP/UDP
TCP/UDP
Socket API
IP
IP
Ethernet Adaptor
Ethernet Adaptor
4
What are Sockets?
  • It is an API between applications and network
  • protocol software provided by the OS Functions it
    provides
  • Define an end- point for communication
  • Initiate and accept a connection
  • Send and receive data
  • Terminate a connection gracefully
  • Supports multiple protocol families
  • Examples Unix inter- process communication,
    TCP/ IP
  • Only Internet sockets will be covered in this
    lecture
  • Berkeley Sockets are the most common (from BSD
  • Unix)

5
Type of Sockets
  • Two different types of sockets
  • stream vs. datagram
  • Stream socket ( a. k. a. connection- oriented
    socket)
  • It provides reliable, connected networking
    service
  • Error free no out- of- order packets (uses
    TCP)
  • applications telnet/ ssh, http,
  • Datagram socket ( a. k. a. connectionless
    socket)
  • It provides unreliable, best- effort networking
    service
  • Packets may be lost may arrive out of order
    (uses
  • UDP)
  • applications streaming audio/ video
    (realplayer),

6
Socket usages
  • Network applications use sockets at some level,
    often using higher level protocols on top of
    sockets
  • File transfer apps (FTP), Web browsers (HTTP),
    Email (SMTP/ POP3), etc
  • Simplify and expedite application development
    process
  • Most exploits client-server model.

7
A generic server (FTP)
  • Wait for connections on a port
  • When a client connection comes in,
  • loop
  • Read in the clients request
  • Read data from a file
  • Send the data to the client
  • Disconnect when we have reached EOF

8
A generic client, high level
  • Connect to a given server
  • loop
  • Send a request to the server
  • Read servers response
  • Read servers data
  • Disconnect when we have reached EOF

9
Client- Server communication (TCP)
server
bind() to a receiving port
Accept() connection
socket()
listen () to socket
send() recv()
client
bind() to any port
send() recv()
socket()
connect () to server
10
Socket
  • Transport protocol needs the following
    information for multiplexing/demultiplexing.
  • (source port, source address, destination port,
    destination address)
  • Socket- the end point of connection. Process
    read/write from/to socket. A socket is specified
    by a socket descriptor.
  • struct sockaddr_in
  • u_char sin_family / Address Family /
  • u_short sin_port / Port number /
  • struct in_addr sin_addr / IP address /
  • char sin zero8 / unused /

11
TCP Server program
  • Make a socket
  • include ltsys/ types. hgt
  • include ltsys/ socket. hgt
  • int fd, newfd, nbytes, nbytes2
  • char buf512, response512
  • struct sockaddr_in srv
  • fd socket(AF_INET, SOCK_STREAM, 0)

12
TCP Server program
  • The socket was created now bind it to a port and
    host.
  • srv.sin_family AF_INET
  • srv.sin_port htons(80)
  • srv.sin_addr.s_addr inet_addr(128.2.15.9'')
  • bind(fd, (struct sockaddr) srv, sizeof(srv))
  • Now sit and listen
  • listen(fd, 5)
  • Now, accept any connection. First, clear the
    structure.
  • struct sockaddr_ in cli
  • int cli_ len
  • memset( cli, 0, sizeof( cli)) / clear it /
  • newfd accept(fd, (struct sockaddr) cli, cli
    len)

13
TCP Server program
  • Now it can read from socket, newfd, and write to
    socket, newfd.
  • int BUF_ SIZE 1024, bytesrecv 0
  • char buf BUF_ SIZE
  • / receives up to BUF_ SIZE bytes from sock and
    stores them in buf. /
  • bytesrecv recv( newfd, buf, BUF_ SIZE, 0)
  • / send up BUF_ SIZE bytes /
  • bytesrecv send( newfd, buf, BUF_ SIZE, 0)
  • At the end, we need to close both sockets by
    close command.
  • close( newfd) / closes the socket newfd /

14
TCP client program
  • int fd, newfd, nbytes, nbytes2
  • char buf512, response512
  • struct sockaddr_in srv
  • fd socket(AF_INET, SOCK_STREAM, 0)
  • The same as server. Now, it needs to connect to
    server.
  • srv.sin_family AF_INET
  • srv.sin_port htons(80)
  • srv.sin_addr.s_addr inet_addr(128.2.15.9'')
  • connect(fd, (struct sockaddr) srv,
    sizeof(srv))
  • Connect is blocking and send a SYN signal and is
    blocked until receive SYNACK, (three way
    handshaking)
  • It can start now reading and writing
  • sprintf(request, Here's my request'')
  • nbytes2 write(fd, request, strlen(response))
  • close(fd)

15
Client- Server communication (UDP)
server
recvfrom () sendto()
bind() to a receiving port
socket() to create socket
client
socket() to create scoket
recvfrom () sendto ()
bind() to any port
16
UDP Server/client program
  • / fill in declarations /
  • fd socket(AF_INET, SOCK_DGRAM, 0)
  • The socket was created now bind it to a port and
    host exactly the same way as TCP.
  • Now listen no connection, start reading from the
    port by
  • nbytes recvfrom(fd, buf, sizeof(buf), 0,
    (struct sockaddr) cli, cli len)
  • UDP client is the same TCP, except instead of
    read/write, it uses
  • nbytes sendto(fd, request, sizeof(request), 0,
    (struct sockaddr) srv, sizeof(srv))
Write a Comment
User Comments (0)
About PowerShow.com