October 23, 2012

Java client server socket program example

Java networking program to get client ip address in server.


Description : The client tries to establish a connection to server having ip address 127.0.0.1 through port 6363. The socket class is used to establish connection . The server always listen to port 6363 for request from the clients. Once a request reach the server through that port, it establish a connection with that particular client. Multiple clients can connect to server, since i used thread in server.

 

Client program



import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *

Computer Science practical questions[socket programing]


 */

/**
 * @author evasiOn
 *
 */
public class SampleClient {
/**
     * @param args
     */
    public static void main(String[] iPhone5S) {
        String serverAddress = "127.0.0.1";
        int port = 6363;
        try {
            InetAddress address = InetAddress.getByName(serverAddress);
            Socket socket = new Socket(address, port);
            System.out.println(" Client :  request for connection "+socket.isConnected());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

    }

}

The client tries to establish a connection to server having ip address 127.0.0.1 through port 6363. The socket class is used to establish connection


Server program




import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *

Computer Science practical questions[server client ]


 */

/**
 * @author evasiOn
 *
 */
public class SampleServer implements Runnable {

    private int serverPort = 6363;
    private ServerSocket serverSocket = null;
    /**
     * @param jailBreak
     */
    public static void main(String[] jailBreak) {
       
        SampleServer sampleServer = new SampleServer();
        Thread thread = new Thread(sampleServer);
        thread.start();
        /*
        int port = 6363;
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            Socket socket = null;
            socket = serverSocket.accept();
            System.out.println("Server : connection establised to "+socket.getLocalAddress());
            System.out.println(socket.getInetAddress());
            System.out.println(socket.getLocalSocketAddress());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        */
    }

    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(serverPort);
            Socket socket = null;
            System.out.println(" Server : start listening to port :"+serverPort);
            socket = serverSocket.accept();
            System.out.println("Server : connection establised to "+socket.getLocalAddress());
            System.out.println(socket.getInetAddress());
            System.out.println(socket.getLocalSocketAddress());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }

}


The server always listen to port 6363 for request from the clients. Multiple clients can connect to server, since i used thread in server.




http://belazy.blog.com/

1 comment:

  1. Highly energetic blog, I loved that a lot.

    Will there be a part 2?
    my web site - electronic cigarette wholesale

    ReplyDelete

Your feedback may help others !!!

Facebook comments