Professional Documents
Culture Documents
Internet
Server
Local Area Network
PD
A
1
Client
Server
Network
Client machine
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
Device drivers
Application
(http,ftp,telnet,)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
Networking Basics
HTTP
FTP
Telnet
TCP/IP Stack
Application
(http,ftp,telnet,)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
Networking Basics
Clock server
Ping
TCP/IP Stack
Application
(http,ftp,telnet,)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
Understanding Ports
server
P
o
r
t
app
app
app
app
port
port
port
port
TCP
Client
TCP or UDP
Packet
Data
port# data
7
Understanding Ports
ftp 21/tcp
telnet 23/tcp
smtp 25/tcp
login 513/tcp
Sockets
10
1.TCP Service
2.UDP Service
11
Socket Communication
port
server
Connection request
Client
12
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
port
Client
Connection
13
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
15
process
process
socket
TCP with
buffers,
variables
socket
TCP with
buffers,
variables
host or
server
internet
controlled by
application
developer
controlled by
operating
system
host or
server
16
17
server TCP
When contacted by client,
server TCP creates new
18
client socket
19
Client
(running on hostid)
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
Implementing a Server
1. Open the Server Socket:
ServerSocket server;
DataOutputStream os;
DataInputStream is;
server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to the client
is = new DataInputStream( client.getInputStream() );
os = new DataOutputStream( client.getOutputStream() );
4. Perform communication with client
Receive from client: String line = is.readLine();
Send to client: os.writeBytes("Hello\n");
5. Close sockets: client.close();
For multithreaded server:
while(true) {
i. wait for client requests (step 2 above)
ii. create a thread with client socket as parameter (the thread creates streams (as in step
(3) and does communication as stated in (4). Remove thread once service is provided.
}
21
Implementing a Client
1. Create a Socket Object:
client = new Socket( server, port_id );
2. Create I/O streams for communicating with the server.
is = new DataInputStream(client.getInputStream() );
os = new DataOutputStream( client.getOutputStream() );
os.writeBytes("Hello\n");
4. Close the socket when done:
client.close();
22
23
Server Process
Client 1 Process
Server
Threads
Internet
Client 2 Process
25
Conclusion