Socket Programming - Computer Networks - Lecture Slides, Slides of Computer Networks

During the first semester of our degree program, we study Computer Networks Fundamentals. These lecture slides are very informative for me. The major points which are core of course are:Socket Programming, Roadmap, Application Level, Human Readable, Protocol Layering, Telnet, Directly, Traced, Application Level Protocols, Server Code

Typology: Slides

2012/2013

Uploaded on 04/25/2013

avantika
avantika 🇮🇳

4.3

(22)

153 documents

1 / 51

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2: Application Layer 1
6: Socket Programming
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33

Partial preview of the text

Download Socket Programming - Computer Networks - Lecture Slides and more Slides Computer Networks in PDF only on Docsity!

2: Application Layer 1

6: Socket Programming

2: Application Layer 2

Roadmap

 We’ve looked at a bunch of application level

protocols (HTTP, DNS, FTP, SMTP, POP, IMAP,, ..)

– Lessons?

 Many were human readable – why?

 High level examples of protocol layering (SMTP, HTTP)

 Some ran on TCP, some on UDP, one on both – why?

 Used telnet/nslookup to interact with these protocols

more directly

 Traced them (What went in clear text?!)

 Other application level protocols?

 Lots more but can’t stay at app level all semester

 How to investigate on your own (RFCs, run app, trace, get

client/server code)

 Next.. How would we implement an application level

protocol ourselves?

 Socket API

 After that down to transport layer Docsity.com

Well known port numbers

 Both TCP and UDP have 16 bits for port

 So 2^16 - 1 = 65535 possible ports

 System ports: 1- 1023

 IETF assigned for standards track protocols

 User ports: 1024- 49151

 Assigned by IANA based on expert review process

 Dynamic ports:49152- 65535

 Not assigned used for ephemeral ports or private

services

 http://www.iana.org/assignments/service-

names-port-numbers/service-names-port-

numbers.xml

2: Application LayerDocsity.com 4

2: Application Layer 5

Sockets Specify Transport

Services

 Sockets define the interfaces between an

application and the transport layer

 Applications choose the type of transport

layer by choosing the type of socket

 UDP Sockets – called DatagramSocket in Java,

SOCK_DGRAM in C

 TCP Sockets – called Socket/ServerSocket in

Java, SOCK_STREAM in C

 Client and server agree on the type of

socket, the server port number and the

protocol

2: Application Layer 7

Socket API

 Introduced in BSD4.1 UNIX, 1981

 Sockets are explicitly created, used, released

by applications

 client/server paradigm

 two types of transport service via socket API:

 unreliable datagram

 reliable, byte stream-oriented

2: Application Layer 8

Languages and Platforms

Socket API is available for many languages on many

platforms:

 C, Java, Perl, Python,…

 *nix, Windows,…

Socket Programs written in any language and running

on any platform can communicate with each other!

Writing communicating programs in different

languages is a good exercise

Lean how to build client/server application that

communicate using sockets

2: Application Layer 10

Decisions

 Before you go to write socket code, decide

 Do you want a TCP-style reliable, full duplex,

connection oriented channel? Or do you want a

UDP-style, unreliable, message oriented

channel?

 Will the code you are writing be the client or

the server?

  • Client: you assume that there is a process already

running on another machines that you need to connect

to.

  • Server: you will just start up and wait to be

contacted

2: Application Layer 11

OVERVIEW: TCP vs UDP

TCP service:

 connection-oriented: setup

required between client,

server

 reliable transport between

sending and receiving process

 flow control: sender won’t

overwhelm receiver

 congestion control: throttle

sender when network

overloaded

 does not provide: timing or

minimum bandwidth

guarantees

UDP service:

 unreliable data transfer

between sending and

receiving process

 does not provide:

connection setup,

reliability, flow control,

congestion control, timing,

or bandwidth guarantee

2: Application Layer 13

Pseudo code TCP client

Create socket, connectSocket

Do an active connect specifying the IP

address and port number of server

Read and Write Data Into connectSocket to

Communicate with server

Close connectSocket

2: Application Layer 14

Example: Java server (TCP)

import java.io.; import java.net.;

class TCPServer {

public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence;

ServerSocket doorbellSocket = new ServerSocket(6789);

while(true) {

Socket connectSocket = doorbellSocket.accept();

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectSocket.getInputStream()));

Create welcoming socket at port 6789

Wait, on welcoming socket for contact by client

Create input stream, attached to socket

2: Application Layer 16

Example: Java client (TCP)

import java.io.; import java.net.; class TCPClient {

public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket connectSocket = new Socket("hostname", 6789);

DataOutputStream outToServer = new DataOutputStream(connectSocket.getOutputStream());

Create input stream

Create client socket, connect to server Create output stream attached to socket

2: Application Layer 17

Example: Java client (TCP), cont.

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(connectSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence );

connectSocket.close();

Create input stream attached to socket

Send line to server

Read line from server

2: Application Layer 19

Example: C server (TCP)

#include <sys/socket.h> #include <netinet/in.h>

Int main(int argc, char **argv) { int doorbellSocket, connectSocket; char clientSentence[MAX_LINE];; struct sockaddr_in servaddr;

doorbellSocket = socket(AF_INET, SOCK_STREAM, 0);

servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(6789);

bind(doorbellSocket, (struct sockaddr *) &servaddr,
sizeof(servaddr)); listen(doorbellSocket , LISTENQ);

Create welcoming socket at port 6789

Warning: Should check return codes of major functions!! Omitted for space here

2: Application Layer 20

Example: C server (TCP), cont

for ( ; ; ) {

connectSocket = accept(doorbellSocket , (struct sockaddr *) NULL, NULL);

bytesRead = read(connectSocket, clientSentence, MAXLINE);

//would have to write the capitalize procedure capitalize(clientSentence);

write(connectSocket, clientSentence, MAXLINE);

close(connectSocket); } close(doorbellSocket); }

Read in line from socket

Write out line to socket

End of while loop, loop back and wait for another client connection

Wait, on welcoming socket for contact by client