java ssl socket example
ssl socket connection with multi threading application.
Server
- package Test2;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.InetAddress;
- import java.security.cert.Certificate;
- import java.security.cert.X509Certificate;
- import java.util.ArrayList;
- import javax.net.ssl.SSLServerSocket;
- import javax.net.ssl.SSLServerSocketFactory;
- import javax.net.ssl.SSLSession;
- import javax.net.ssl.SSLSocket;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class Server {
- public static ArrayList<String> list_user = new ArrayList<String>();
- public static ArrayList<String> list_verify = new ArrayList<String>();
- public static int port = 8005;
- public static void main(String[] args) {
- list_verify.add("101");
- list_verify.add("102");
- list_verify.add("103");
- list_verify.add("104");
- list_verify.add("105");
- try {
- SSLServerSocketFactory factory=(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
- SSLServerSocket sslserversocket = (SSLServerSocket) factory.createServerSocket(port, 0, InetAddress.getLocalHost());
- System.out.println("Server running in :" + InetAddress.getLocalHost() + ":" + port);
- String[] suites = sslserversocket.getSupportedCipherSuites();
- /*for(String s:suites){
- System.out.println(s);
- }*/
- sslserversocket.setEnabledCipherSuites(suites);
- while (true) {
- SSLSocket socket=(SSLSocket) sslserversocket.accept();
- /*
- SSLSession session = ((SSLSocket) socket).getSession();
- Certificate[] cchain2 = session.getLocalCertificates();*/
- Runnable accept = new Runnable() {
- @Override
- public void run() {
- JFrame frame = new JFrame("Server");
- String currentUser="";
- try {
- ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
- String username = (String) ois.readObject();
- ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
- if (list_verify.contains(username)) {
- if (!list_user.contains(username)) {
- list_user.add(username);
- currentUser=username;
- oos.writeObject("1");
- System.out.println("connected to : " + username);
- //JFrame frame = new JFrame("Server");
- frame.addWindowListener(new java.awt.event.WindowAdapter() {
- @Override
- public void windowClosing(java.awt.event.WindowEvent windowEvent) {
- //frame.dispose();
- try {
- oos.close();
- ois.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- frame.setVisible(true);
- JTextField userText = new JTextField();
- userText.setEditable(false);
- userText.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- sendMessage(oos, event.getActionCommand());
- userText.setText("");
- }
- });
- ;
- frame.add(userText, BorderLayout.NORTH);
- JTextArea chatWindow = new JTextArea();
- chatWindow.setEditable(false);
- frame.add(new JScrollPane(chatWindow));
- frame.setSize(300, 150);
- chatWindow.append("connected to : " + username);
- userText.setEditable(true);
- do {
- try {
- username = (String) ois.readObject();
- chatWindow.append("\n " + username);
- } catch (ClassNotFoundException classNotFoundException) {
- System.out.println("close");
- chatWindow.append("\n Dont know the Object type ");
- chatWindow.append("\n connection close.. ");
- }
- } while (!username.equals("END"));
- } else {
- oos.writeObject("2");
- }
- } else {
- oos.writeObject("0");
- }
- } catch (IOException e) {
- System.out.println("client disconnected ");
- frame.dispose();
- list_user.remove(currentUser);
- //Thread.currentThread().interrupt();
- // chatWindow.append("Diss");
- // userText.setEditable(false);
- // e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }};
- new Thread(accept).start();
- }
- } catch (IOException e) {
- JOptionPane.showMessageDialog(null, "Server already running in the same port");
- System.out.println("Ip and port Already bind");
- }
- }
- public static void sendMessage(ObjectOutputStream oos, String message) {
- try {
- oos.writeObject(message);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Client
- package Test2;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.UnknownHostException;
- import javax.net.ssl.HandshakeCompletedEvent;
- import javax.net.ssl.HandshakeCompletedListener;
- import javax.net.ssl.SSLSocket;
- import javax.net.ssl.SSLSocketFactory;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class Client {
- public static void main(String[] args) {
- String host = "localhost";
- int port = 8005;
- String username = JOptionPane.showInputDialog(null, "IP: " + host + ":" + port);
- JFrame frame = new JFrame("Client");
- try {
- SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
- SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
- String[] suites = socket.getSupportedCipherSuites();
- socket.setEnabledCipherSuites(suites);
- socket.addHandshakeCompletedListener(new MyHandshakeListener());
- socket.startHandshake();
- System.out.println("Just connected to " + socket.getRemoteSocketAddress());
- ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
- oos.writeObject(username);
- ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
- String response = (String) ois.readObject();
- //JFrame frame = new JFrame("Client");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- JTextField userText = new JTextField();
- userText.setEditable(false);
- userText.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- sendMessage(oos, event.getActionCommand());
- userText.setText("");
- }
- });
- ;
- frame.add(userText, BorderLayout.NORTH);
- JTextArea chatWindow = new JTextArea();
- chatWindow.setEditable(false);
- frame.add(new JScrollPane(chatWindow));
- frame.setSize(300, 150);
- if (response.equalsIgnoreCase("1")) {
- chatWindow.append("connected......");
- userText.setEditable(true);
- do {
- try {
- response = (String) ois.readObject();
- chatWindow.append("\n " + response);
- } catch (ClassNotFoundException classNotFoundException) {
- chatWindow.append("\n Dont know the Object type ");
- chatWindow.append("\n connection close.. ");
- }
- } while (!response.equals(" END"));
- }
- if (response.equalsIgnoreCase("0")) {
- chatWindow.append("\n refused , not authorised....");
- }
- if (response.equalsIgnoreCase("2")) {
- frame.dispose();
- JOptionPane.showMessageDialog(null, "already connected");
- }
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- frame.dispose();
- }
- catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void sendMessage(ObjectOutputStream oos, String message) {
- try {
- oos.writeObject(message);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- class MyHandshakeListener implements HandshakeCompletedListener {
- public void handshakeCompleted(HandshakeCompletedEvent e) {
- System.out.println("Handshake succesful!");
- System.out.println("Using cipher suite: " + e.getCipherSuite());
- }
- }
Comments
Post a Comment