1 Dec 2013

Client and Server Socket example code in java

/* Client and Server Socket example code in java, 
Client-Server Programming in Java code, server and client program in java, Server Socket example code */

import java.io.*;
import java.net.*;
class ServerSocketDemo
{
    public static void main(String []args)
    {
        java.net.ServerSocket ss=null;
        java.io.PrintWriter    pw = null;
        
java.io.BufferedReader br = null;

        int i=0;
        try
        {
            //server running at 40000 port number
            ss = new 
java.net.ServerSocket(40000);

        }
        catch(
java.lang.Exception e)

        {
            System.out.println("Exception in Server while creating connection"+e);
            e.printStackTrace();
        }
        System.out.print("Server is ready");
        while (true)
        {
            System.out.println ("  Waiting for connection....");
            
java.net.Socket s=null;

            try
            {
                System.out.println("connection "+s+ "\n printwriter "+pw+"\n bufferedreader "+br);
                s = ss.accept();
                System.out.println("Connection established with client");
                pw = new 
java.io.PrintWriter(s.getOutputStream());

                br = new java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream()));
                System.out.println("connection "+s+ "\n printwriter "+pw+"\n bufferedreader "+br);
                i = new Integer(br.readLine());
                System.out.println("i is "+i);
            }
            catch(
java.lang.Exception e)

            {
                System.out.println("Exception in Server "+e);
                e.printStackTrace();
            }
            System.out.println("Connection established with "+s);
            i*=i;
            pw.println(i);
            try
            {
                pw.close();
                br.close();
            }
            catch(
java.lang.Exception e)

            {
                System.out.println("Exception while closing streams");
            }
        }
    }
}




//Client Socket example code
import java.net.*;
import java.io.*;
class ClientSocketDemo
{
    public static void main(String []arga) throws 
java.lang.Exception

    {
        
java.net.Socket          s = null;

        java.io.PrintWriter    pw = null;
        java.io.BufferedReader br = null;
        System.out.println("Enter a number one digit");
        int i=(System.in.read()-48); // will read only one character
        System.out.println("Input number is "+i);
        try
        {
            //sending request to the server running at ip 192.16.0.100 and port 40000
            s  = new 
java.net.Socket("192.168.0.100",40000);

            System.out.println(s);
            pw = new 
java.io.PrintWriter(s.getOutputStream());

            System.out.println(pw);
            br = new 
java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream()));

            System.out.println(br);
            System.out.println("Connection established, streams created");
        }
        catch(
java.lang.Exception e)

        {
            System.out.println("Exception in Client "+e);
        }
        pw.println(i);
        pw.flush();
        System.out.println("Data sent to server");
        String str = br.readLine();
        System.out.println("The square of "+i+" is "+str);
    }
}



Output:


Client and Server Socket example code in java



Client and Server Socket example code in java




Client and Server Socket example code in java

0 comments:

Post a Comment