CN Lab Ports, Sockets

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

Network Programming and

Port, Sockets

KNM
[email protected]

Internet Applications Serving Local


and Remote Users
PC client

Internet
Server
Local Area Network

PDA
2

Elements of C-S Computing

Client

Re
qu

es

a client, a server, and network

Server
Re
s

Client machine

Network
ul

Server machine
3

Networking Basics

Applications Layer

User apps
TCP
UDP
Programming Interface:

Sockets

Network Layer

HTTP
FTP
Telnet

Transport Layer

IP

Link Layer

TCP/IP Stack

Standard apps

Application
(http,ftp,telnet,)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)

Device drivers

Networking Basics

TCP (Transport Control


Protocol) is a
connection-oriented
protocol that provides a
reliable flow of data
between two computers.
Example applications:

HTTP
FTP
Telnet

TCP/IP Stack
Application
(http,ftp,telnet,)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)

Networking Basics

UDP (User Datagram


Protocol) is a protocol
that sends independent
packets of data, called
datagrams, from one
computer to another with
no guarantees about
arrival.
Example applications:

Clock server
Ping

TCP/IP Stack
Application
(http,ftp,telnet,)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)

Understanding Ports

The TCP and UDP


protocols use ports to
map incoming data to
a particular process
running on a
computer.

server

P
o
r
t

app

app

app

app

port

port

port

port

TCP or UDP
Data

TCP

Client

Packet
port# data7

Understanding Ports

Port is represented by a positive (16-bit) integer


value
Some ports have been reserved to support
common/well known services:

ftp 21/tcp
telnet 23/tcp
smtp 25/tcp
login 513/tcp

User level process/services generally use port


number value >= 1024
8

Sockets

Sockets provide an interface for programming networks


at the transport layer.
Network communication using Sockets is very much
similar to performing file I/O

In fact, socket handle is treated like file handle.


The streams used in file I/O operation are also applicable to
socket-based I/O

Socket-based communication is programming language


independent.

That means, a socket program written in Java language can


also communicate to a program written in Java or non-Java
socket program.

Socket Communication

A server (program) runs on a specific


computer and has a socket that is bound
to a specific port. The server waits and
listens to the socket for a client to make a
connection request.

port

server

Connection request
Client

10

Socket Communication
If everything goes well, the server accepts the
connection. Upon acceptance, the server gets a new
socket bounds to a different port. It needs a new socket
(consequently a different port number) so that it can
continue to listen to the original socket for connection
requests while serving the connected client.

port

port

server

Connection

port

Client

11

Java Sockets
Server

ServerSocket(1234)

Output/write stream

Client

Input/read stream
Socket(128.250.25.158, 1234)
It can be host_name like mandroo.cs.mu.oz.au

12

You might also like