How The Communication Occurs?: PC Client
How The Communication Occurs?: PC Client
How The Communication Occurs?: PC Client
PC client
Internet Server
Local Area Network
PD A
1
Open Systems Interconnection (OSI) is a set of internationally recognized, non-proprietary standards for networking and for operating system involved in networking functions.
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
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,..)
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
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
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.
server port
Client
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:
Java Sockets
Server
ServerSocket(1234)
Client
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
15
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
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