How The Communication Occurs?: PC Client

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

How the Communication occurs?

PC client

Internet Server
Local Area Network

PD A
1

Communication between Client and Server


a client, a server, and network

Client Server Network

Client machine Server machine


2

7 layers of OSI Model

Open Systems Interconnection (OSI) is a set of internationally recognized, non-proprietary standards for networking and for operating system involved in networking functions.

Protocols for each Layers


Layer Example

7.) Application HTTP, FTP, SMTP 6.) Presentation ASCII, JPEG, PGP 5.) Session 4.) Transport BOOTP, NetBIOS, DHCP, DNS TCP, UDP, SPX

3.) Network
2.) Data Link 1.) Physical

IP, IPX, ICMP


Ethernet, Token Ring, Frame Relay Bits, Interfaces, Hubs
4

Networking Basics

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

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

HTTP FTP Telnet

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 application:

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

Ping

Understanding Ports

The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer. Ports are appln specific and process specific 16 bits b/w 1 to 65535

server

P o r t

TCP

Client

app

app

app

app

port

port

port

port

TCP or UDP Packet Data port# data


7

Sockets

Sockets provide an interface for programming networks at the transport layer. It is a combination of IP address and port number. 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.
8

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.

server

Connection request
Client

port

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 Connection


10

server port

Client

Sockets and Java Socket Classes

A socket is an endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent. Javas .net package provides two classes:

Socket for implementing a client ServerSocket for implementing a server


11

Java Sockets
Server

ServerSocket(1234)

Output/write stream Input/read stream

Client

Socket(128.250.25.158, 1234) It can be host_name like mandroo.cs.mu.oz.au 12

Implementing a Server
First we have to create the Server Socket by specifying the port number as the parameter. ServerSocketobj=new ServerSocket(port no) Socket obj=ss.accept();=> wait or listen for clients request Write operation can be done by OutputStream- DataOutputStream

13

Implementing a Client
Socket obj=new Socket(Server ip,port num) Client requires ipaddress of the server whereas server will not conrains the ipaddress of client InputStream-DataInputStream InputStream fis=s.InputStream

14

A simple server (simplified code)


// SimpleServer.java: a simple server program import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) throws IOException { // Register service on port 1234 ServerSocket s = new ServerSocket(1234); Socket s1=s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeUTF("Hi there"); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } }

15

A simple client (simplified code)


// SimpleClient.java: a simple client program import java.net.*; import java.io.*; public class SimpleClient { public static void main(String args[]) throws IOException { // Open your connection to a server, at port 1234 Socket s1 = new Socket("mundroo.cs.mu.oz.au",1234); // Get an input file handle from the socket and read the input InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In); String st = new String (dis.readUTF()); System.out.println(st); // When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); } } 16

Socket Exceptions
try { Socket client = new Socket(host, port); handleConnection(client); } catch(UnknownHostException uhe) { System.out.println("Unknown host: " + host); uhe.printStackTrace(); } catch(IOException ioe) { System.out.println("IOException: " + ioe); ioe.printStackTrace(); }

17

Data Transfer
The data can be communicated to other system by writing that particular data in the socket by, Dos.writeUTF(dfdf); dos.writeInt(2); (same for reading)

18

ServerSocket & Exceptions

public ServerSocket(int port) throws IOException

Creates a server socket on a specified port. A port of 0 creates a socket on any free port. You can use getLocalPort() to identify the (assigned) port on which this socket is listening. The maximum queue length for incoming connection indications (a request to connect) is set to 50. If a connection indication arrives when the queue is full, the connection is refused.
IOException - if an I/O error occurs when opening the socket. SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.

Throws:

19

Server in Loop: Always up


// SimpleServerLoop.java: a simple server program that runs forever in a single thead import java.net.*; import java.io.*; public class SimpleServerLoop { public static void main(String args[]) throws IOException { // Register service on port 1234 ServerSocket s = new ServerSocket(1234); while(true) { Socket s1=s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeUTF("Hi there"); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } } } 20

You might also like