Introduction
A computer network is an interconnected collection of computers. The node of a network communicates others using a wired/wireless medium. This communication is actually carried between two processes residing in two different machines. The processes on one machine initiates the request and another responds to the request and another responds to the requests. The initiator is the client and the responder is the server.
TCP Protocol Suite
The TCP/IP protocol suite contains a number of protocols. Its a four layer model followed by the network s. The internet also follows the same model. The client and server applications created by the user reside at the Application layer. They interact with the transport layer using the socket API. Java provide a package java.net. The transport layer take the data from takes data from application layer (sender) and breaks up in to segment attached the port number used by application to the data and passes in to the network layer. Depending upon the protocols used.
TCP/IP models figure
Socket Programming in Java
Network programming revolves around the concept of sockets. A socket is one end-point of a two-way communication link between two programs running on the network. In Java sockets are created with the help of Socket classes and these class are found in the java.net package. There are separate classes in the java.net package for creating TCP sockets and UDP sockets. The client socket is created with the help of the Socket class and the server socket is created using the ServerSocket class.
socket figure
Syntax
// for tcp client side
Socket s= new Socket (InetAddress adr, int port);
//for server side
ServerSocket ss= new ServerSocket (InetAddress adr, int port)
Step 1 - Creating a Socket on the client side (code):
-
- import java.net.*;
- import java.io.*;
- class MySocket {
- public static void main(String[] args) {
- try {
-
- Socket s = new Socket(InetAddress.getLocalHost(), 15);
- System.out.println("your socket is created");
-
- System.out.println("Local Address :" + s.getLocalAddress());
-
- System.out.println("Local Host :" + InetAddress.getLocalHost());
-
- System.out.println("Local Port :" + s.getLocalPort());
-
- System.out.println("Inet Address :" + s.getInetAddress());
-
- InputStream in = s.getInputStream();
-
- InputStreamReader
- BufferedReader br = new BufferedReader(new InputStreamReader( in ));
- String str = null;
- while ((str = br.readLine()) != null)
- System.out.println(str);
- in .close();
- s.close();
-
- } catch (Exception e)
- {
- System.out.println(e);
- }
- }
- }
Step 2 - Creating a TCP Server (code):
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class MyServerSocket
- {
- public static void main(String[] args)
- {
- try {
- ServerSocket ss=new ServerSocket(15);
- while(true)
- {
- Socket client =ss.accept();
- System.out.println("socket is created");
-
- System.out.println("client inet Address :"+client.getInetAddres());
-
- System.out.println("client Port number :"+client.getPort() );
-
- OutputStream out=client.getOutputStream();
-
- Calendar c=Calendar.getInstance();
- System.out.println("server date and time :"+c.getTime());
- PrintWriter pw = new PrintWriter(out,true);
- pw.close();
- }
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- }
- }
Output
First the server is started and waits for a client.
Then a Client is started and the output is as follows:
After the client starts then the server output is:
Resources