Socket Programming: Understanding Application Layer with TCP and UDP, Slides of Computer Networks

An in-depth exploration of socket programming at the application layer, focusing on tcp and udp. Topics include principles of network applications, web and http, ftp, electronic mail, dns, p2p file sharing, and building a web server. Learn about socket apis, client-server paradigm, and how to create java client and server applications using both tcp and udp.

Typology: Slides

2011/2012

Uploaded on 07/03/2012

abhae
abhae 🇮🇳

4.4

(9)

43 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Socket Programming
Computer Networks Lect-08
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Socket Programming: Understanding Application Layer with TCP and UDP and more Slides Computer Networks in PDF only on Docsity!

Socket Programming

Computer Networks

Lect-

SOCKET PROGRAMMING

SOCKET PROGRAMMING

Socket API

»introduced in BSD4.1 UNIX, 1981

»explicitly created, used, released

by apps

»client/server paradigm

»two types of transport service via

socket API:

» unreliable datagram

» reliable, byte stream-

oriented

a host-local ,

application-created ,

OS-controlled interface (a

“door”) into which

application process can both

send and

receive messages to/from

another application process

socket

Goal: learn how to build client/server application that communicate using sockets

Docsity.com

SOCKET-PROGRAMMING USING TCP

Socket: a door between application process and end-end-

transport protocol (UCP or TCP)

TCP service: reliable transfer of bytes from one process to

another

process

TCP with buffers, variables

socket

controlled by application developer controlled by operating system

host or

server

process

TCP with buffers, variables

socket

controlled by application developer

controlled by operating system

host or

server

internet

Docsity.com

2: Application Layer (^7)

STREAM JARGON

»A stream is a sequence of characters that flow into or out of a

process.

»An input stream is attached to some input source for the process,

eg, keyboard or socket.

»An output stream is attached to an output source, eg, monitor or

socket.

2: Application Layer (^8)

SOCKET PROGRAMMING WITH TCP

Example client-server app:

1) client reads line from standard

input ( inFromUser stream) , sends

to server via socket ( outToServer

stream)

2) server reads line from socket

3) server converts line to uppercase,

sends back to client

4) client reads, prints modified line

from socket ( inFromServer

stream) outToServer

to network from network

inFromServer

inFromUser

keyboard monitor

Process

clientSocket

input stream

input stream

output stream

TCP socket

Client

process

client TCP socket

2: Application Layer 10

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 clientSocket = new Socket("hostname", 6789);

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

Create input stream

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

2: Application Layer 11

EXAMPLE: JAVA CLIENT (TCP), CONT.

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

sentence = inFromUser.readLine();

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

modifiedSentence = inFromServer.readLine();

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

clientSocket.close();

} }

Create input stream attached to socket

Send line to server

Read line from server

2: Application Layer 13

EXAMPLE: JAVA SERVER (TCP), CONT

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence); } } }

Read in line from socket

Create output stream, attached to socket

Write out line to socket

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

2: Application Layer (^14)

CHAPTER 2: APPLICATION LAYER

»2.1 Principles of network

applications

»2.2 Web and HTTP

»2.3 FTP

»2.4 Electronic Mail

» SMTP, POP3, IMAP

»2.5 DNS

»2.6 P2P file sharing

»2.7 Socket programming with TCP

»2.8 Socket programming with UDP

»2.9 Building a Web server

2: Application Layer 16

CLIENT/SERVER SOCKET INTERACTION: UDP

close clientSocket

Server (running on hostid )

read reply from clientSocket

create socket, clientSocket = DatagramSocket()

Client

Create, address ( hostid, port=x, send datagram request using clientSocket

create socket, port= x , for incoming request: serverSocket = DatagramSocket()

read request from serverSocket

write reply to serverSocket specifying client host address, port number

2: Application Layer 17

EXAMPLE: JAVA CLIENT (UDP)

sendPacket

to network from network

receivePacket

inFromUser

keyboard monitor

Process

clientSocket

UDP packet

input stream

UDP packet

UDP socket

Output: sends packet (TCP sent “byte stream”)

Input: receives packet (TCP received “byte stream”)

Client

process

client UDP socket

2: Application Layer 19

EXAMPLE: JAVA CLIENT (UDP), CONT.

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

clientSocket.send(sendPacket);

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

clientSocket.receive(receivePacket);

String modifiedSentence = new String(receivePacket.getData());

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

Create datagram with data-to-send, length, IP addr, port

Send datagram to server

Read datagram from server

2: Application Layer 20

EXAMPLE: JAVA SERVER (UDP)

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

class UDPServer { public static void main(String args[]) throws Exception {

DatagramSocket serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024];

while(true) {

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket);

Create datagram socket at port 9876

Create space for received datagram

Receive datagram