Sockets for TCP/IP

Home  »  Java  »  Advance Java  »  Socket Programming  »  Sockets for TCP/IP


Sockets are used for making a Connection with a Server and in this after Making a Connection With a Server , The Server will Make the Packets by Dividing a Large data into the Small Packets. The Socket of Client is used to Connect With a Server and Socket of Server is used to Send the data to Client. JAVA Contains Socket Class For Making a Connection or for Sending the data from a Client to Server by using the TCP/IP Connection.

The Various Constructors of Socket Class are –
  1. Socket ( String HostName, int PortNumber) First Argument Specify the Name of Server and Second is the Port Number Which we wants to use.

  2. ServerSocket(int PortNumber,int number ) The First Arguments Specify the Number of Port which will be used by Client to Make a Connection With a Server and Second Argument Specify the Number of Users those may Request to a Server at a Single Time.

The Socket Class Contains Various Methods Like
MethodsDescription
getPort()Retrieves the Port Number which a Client has used .
getInetAddress()Gives Address of Host or Computer on The Network.
getOutputStream() Returns the Outputstream which Writes the data on the Socket.
Close() To Close the Socket after Transmission.


Creating a Server Program

The Server program creates a server socket that can communicate with the client socket using TCP/IP. The TCPServer class creates a server socket by using an object of the ServerSocket class. The following program code shows how to use ServerSocket class to create a server socket:

Creating a Server Socket


C:\>jdk1.4\bin>javac ServerSocketProgram.java
C:\>jdk1.4\bin>java ServerSocketProgram




Creating a Client Program

The client program creates a client socket that can communicate with the server socket using TCP/IP. The TCPClient class creates a client socket using an object of the socket class. The following program code show how to use the Socket class to create the client socket:

Creating a Client Socket


C:\>jdk1.4\bin>javac ClientSocketProgram.java
C:\>jdk1.4\bin>appletviewer ClientSocketProgram.html

The try...finally block causes an unusual thing to happen when the return statement is encountered. As the return statement is within the try...finally block, the statement within the finally block are executed no matter how the try block is exited. The "last number read" text always is displayed.


Download Complete Program

  Server Program
  -  
  Client Program
  -    -  HTML File



Socket Programming


Sockets for TCP/IP Sockets of UDP




No comments:

Post a Comment