lecture # 1 socket programming

11
Lecture # 1 Socket Programming Islamic University of Gaza Faculty of Engineering Computer Engineering Department Networks Discussion ECOM 4021 By Eng. Wafaa Audah Feb. 2013

Upload: others

Post on 22-May-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture # 1 Socket Programming

Lecture # 1

Socket Programming

Islamic University of Gaza

Faculty of Engineering

Computer Engineering Department

Networks Discussion ECOM 4021

By

Eng. Wafaa Audah

Feb. 2013

Page 2: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

Socket Programming

Note: this lecture covers sections 2.7, 2.8 at textbook.

What is socket

Socket represents a end point connection between two processes that

permits them to exchange data. It has two types:

- TCP Stream socket: reliable two-way connected communication

streams.

- Datagram socket: unreliable connectionless communication

datagram.

Socket-programming using TCP

- When a computer is connected to the internet, it gets an 'IP' address. This address uniquely identifies a computer on the net. In order for a process to connect to another, it must know the IP address of the

host machine. In addition to the IP address, the process must know

what 'port' the other process is listening on.

- TCP connection: direct virtual pipe between client's socket and

server's connection socket.

- A server is the process that is constantly running, listening for socket

connections. The client process, attempts to make contact with the

server, when needed.

General TCP client/server model description

1. Client initiates contact with server.

2. Server must be ready

- Not dormant (running).

- Have a socket that welcomes initial contact from client process

(welcoming socket).

2

Page 3: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

3. Client initiates TCP connection to the server

- Creates client socket (address of server process, port number).

4. Three-way handshake

- Client knocks server welcoming socket.

- Server hears the knock and creates new socket to particular

client (connection socket).

- TCP connection exist between client's socket and server's new

socket.

5. Client sends data via its client socket to server via connection socket

and TCP guarantees the delivery of data which means that TCP

provides reliable byte-stream service between client and service

processes.

Stream

Stream is a sequence of characters that flow into/out of the process, it has

two types:

- Input stream: attached to some input source for the process such:

standard input (keyboard) or socket into which data flows from the

internet.

- Output stream: attached to some output source for the process such:

standard output (monitor) or socket out of which data flows into the

internet.

3

Page 4: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

Detailed TCP client/server model diagram

Client-Server Application:

1. Client reads line from standard input (inFromUser stream) …… BufferedReader

2. Sends to server via socket (outToServer stream) …… DataOutputStream

3. Server reads line from socket (inFromClient stream) …… BufferedReader

4. Server converts line to uppercase, sends back to client (outToClient stream) …… DataOutputStream

5. Client reads, prints modified line from socket (inFromServer stream) …… BufferedReader

4

Page 5: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

Streams representation with client and server

JAVA Programming

outT

oCie

nt

to network from network

inFr

omC

lient

ServerProcess

connectionSocket

input

stream

output

stream

Input/output streams

socket/socketServer

5

Page 6: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

6

Page 7: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

Important notes

- Client has Socket type for clientSocket, server has ServerSocket type for

welcomingSocket and Socket type for connectionSocket.

- clientSocket needs hostname for server and port number, serverSocket

needs only port number.

- BufferedReader stream is used for reading data at client or server sides,

DataInput/OutputStream is used for sending data between client and server

through network.

Socket-programming using UDP

UDP

- Connectionless and unreliable service.

- There isn’t an initial handshaking phase.

- Doesn’t have a pipe.

- Transmitted data may be received out of order, or lost.

Socket Programming with UDP

- No need for a welcoming socket.

- No streams are attached to the sockets.

- The sending hosts creates “packets” by attaching the IP destination

address and port number to each batch of bytes.

- The receiving process must discover received packet to obtain the

packet’s information bytes.

- Sockets type is DatagramSocket

7

Page 8: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

Detailed UDP client/server model diagram

Input/output representation

Note:

General mechanisms of TCP and UDP models for socket programming are the

same with some differences that appears according to the differences

between TCP and UDP features. UDP Client/Server codes below have red-

note differences over TCP codes above.

8

Page 9: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

DNS_ Domain name system

Client has DNS lookup for matching every host name with its IP

address ('www.example.com' matches 192.0.24.10)

9

Page 10: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

10

Page 11: Lecture # 1 Socket Programming

Networks Discussion Eng. Wafaa Audah

Important notes

Client code

- DatagramSocket clientSocket = new DatagramSocket() line:

Doesn't initiate TCP connection, client host doesn't contact with the server

host according to creating the socket. DatagramSocket() doesn't take the

server hostname or the port number as arguments.

- InetAddress IPAddress = InetAddress.getByName("hostname")line:

Get the IP address of the destination host in order to attach it to every sent

packet.

- Sent packets has a type of DatagramPacket.

- Send/receive methods at UDP programming are instead of Input/Output

streams at TCP programming that attached to sockets.

Server code

- There is no new socket is created for new connection request from client.

- Unlike TCP, at UDP client code is executed before server code because

client doesn't initiate a connection with server when client code executed.

Good Luck

See You at lecture 2

Best Wishes

11