Sunday, 8 July 2012

Create Server and clent Side application in java

import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ServerTest
{
    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static BufferedReader bufferedReader;
    private static String inputLine;
    public static void main(String[] args)
    {
        // Wait for client to connect on 63400
        try
        {
            serverSocket = new ServerSocket(63400);
            clientSocket = serverSocket.accept();
            // Create a reader
            bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            // Get the client message
            while((inputLine = bufferedReader.readLine()) != null)
            System.out.println(inputLine);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}



And client side
import java.net.Socket;
import java.io.PrintWriter;
public class ClientTest
{
    private static Socket socket;
    private static PrintWriter printWriter;
    public static void main(String[] args)
    {
        try
        {
            socket = new Socket("localhost",63400);
            printWriter = new PrintWriter(socket.getOutputStream(),true);
            printWriter.println("Hello Socket");
            printWriter.println("EYYYYYAAAAAAAA!!!!");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

No comments:

Post a Comment