Home Java Socket Programming Java Networking

Socket Programming Java Networking

3 min read
0
0
1,105

This tutorial will teach you Java Networking programming step step.the  TCP protocol starts only when the connection between client and server sockets is established.The server socket listens for a request for connection sent by client sockets and establishes the connection.once the client and server applications are connected,they can communicate with each other.i wrote the example which will help you to understand. in order to run the application first you must run the Server.Java first after that run the Client.Java.

Server.Java

import java.io.*;
import java.net.*;

public class server {
    
//Create the ServerSocket Object
       ServerSocket socket1;

	public server( )
	{
	         try{
                  /* object which accepts connection request from the client at the 
                     port number 2000 */

		 socket1=new ServerSocket( 2000);

		 System.out.println("server is now listening for client request ");

                 //Receive the coonection requests from client
		 Socket client_request = socket1.accept( );
		//create Input stream to retieve the data from the client
		 BufferedReader reader1= new BufferedReader ( new InputStreamReader (client_request.getInputStream( )) );
		//create Output stream to send the data to the client
                PrintWriter  out =  new PrintWriter(  client_request.getOutputStream( ) );

		 String client_message = reader1.readLine( );
		 out.println( "Good morning " + client_message);
		 out.flush( );

		}catch(IOException ioe)
		   {
			System.out.println("error in creating sockets");
			}
	     }

	public static void main(String[ ] args)
	{
		server server1=new server( );		
	}

Client.Java

import java.io.*;
import java.net.*;

public class client {
    
    public client( )
	{
	         try{
                 //create the socket object to connect with the server
		 Socket socket1=new Socket( "localhost", 2000);
//create Output stream to send the data to the server
		 System.out.println("client is now connected with server");
		 BufferedReader reader1= new BufferedReader ( new InputStreamReader ( socket1.getInputStream( )) );
		
 PrintWriter  out =  new PrintWriter(  socket1.getOutputStream( ) );
		 out.println( "Raja ");
		 out.flush( );
		 String message_in = reader1.readLine( );
		 System.out.println( message_in);

		}catch(IOException ioe)
		   {
			System.out.println("error in creating sockets" + ioe);
			}
	     }

	public static void main(String[ ] args)
	{
		client client1=new client( );
	}


 

    Load More Related Articles
    • Conditional Statements in Python

      Conditional statements in Python allow us to control the flow of execution based on condit…
    • Java Beans

      A Java Bean is a reusable software component that follows a specific set of conventions. J…
    • Java String Methods

      Java provides a rich set of built-in methods for handling String operations efficiently. S…
    Load More By admin
    • Java Beans

      A Java Bean is a reusable software component that follows a specific set of conventions. J…
    • Java String Methods

      Java provides a rich set of built-in methods for handling String operations efficiently. S…
    • Java Developer Jobs

      Java remains one of the most in-demand programming languages worldwide, powering everythin…
    Load More In Java

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Check Also

    Conditional Statements in Python

    Conditional statements in Python allow us to control the flow of execution based on condit…