Java Socket Programming (Java Networking Tutorial)
Java Socket Programming using swing. instant messaging program.
Server
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class server extends JFrame
- {
- private JTextField userText;
- private JTextArea chatWindow;
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private ServerSocket servers;
- private Socket connection;
- public server()
- {
- super("Chat Messenger");
- userText=new JTextField();
- userText.setEditable(false);
- userText.addActionListener(
- new ActionListener(){
- public void actionPerformed(ActionEvent event){
- sendMessage(event.getActionCommand());
- userText.setText("");
- }
- }
- );
- add(userText,BorderLayout.NORTH);
- chatWindow=new JTextArea();
- chatWindow.setEditable(false);
- add(new JScrollPane(chatWindow));
- setSize(300,150);
- setVisible(true);
- }
- public void startRunning()
- {
- try{
- servers=new ServerSocket(6789,100);
- while(true)
- {
- try{
- waitForConnection();
- setupStreams();
- whileChatting();
- }catch(EOFException eofException)
- {
- showMessage("\n Server Ended the connection! ");
- }
- finally
- {
- closeCrap();
- }
- }
- }catch(IOException ioException)
- {
- ioException.printStackTrace();
- }
- }
- private void waitForConnection() throws IOException
- {
- showMessage("wating for someone for connect ....\n");
- connection=servers.accept();
- showMessage("Now Connected to "+connection.getInetAddress().getHostName());
- }
- private void setupStreams()throws IOException
- {
- output=new ObjectOutputStream(connection.getOutputStream());
- output.flush();
- input=new ObjectInputStream(connection.getInputStream());
- showMessage("\n Streams are now setup !!\n");
- }
- private void whileChatting()throws IOException
- {
- String message="Your are Connected!!!";
- sendMessage(message);
- ableToType(true);
- do
- {
- try
- {
- message=(String)input.readObject();
- showMessage("\n"+message);
- }
- catch(ClassNotFoundException classNotFoundException)
- {
- }
- }
- while(!message.equals("CLIENT - END"));
- }
- private void closeCrap()
- {
- showMessage("\n Clossing Connection!!!!!!!! \n");
- ableToType(false);
- try{
- output.close();
- input.close();
- connection.close();
- }catch(IOException ioException)
- {
- ioException.printStackTrace();
- }
- }
- private void sendMessage(String message)
- {
- try{
- output.writeObject("SERVER - "+ message);
- output.flush();
- showMessage("\n SERVER - " + message);
- }catch(IOException ioException)
- {
- chatWindow.append("\n your can't send Message");
- }
- }
- private void showMessage(final String text)
- {
- SwingUtilities.invokeLater(
- new Runnable()
- {
- public void run()
- {
- chatWindow.append(text);
- }
- }
- );
- }
- private void ableToType(final boolean tof)
- {
- SwingUtilities.invokeLater(
- new Runnable()
- {
- public void run()
- {
- userText.setEditable(tof);
- }
- }
- );
- }
- }
ServerTest
- import javax.swing.JFrame;
- public class ServerTest {
- public static void main(String[] args) {
- server sally=new server();
- sally.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- sally.startRunning();
- }
- }
Client
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Client extends JFrame{
- private JTextField userText;
- private JTextArea chatWindow;
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private String message="";
- private String serverIP;
- private Socket connection;
- public Client(String host)
- {
- super("Client ");
- serverIP=host;
- userText= new JTextField();
- userText.setEditable(false);
- userText.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent event)
- {
- sendMessage(event.getActionCommand());
- userText.setText("");
- }
- });
- add(userText,BorderLayout.NORTH);
- chatWindow=new JTextArea();
- chatWindow.setEditable(false);
- add(new JScrollPane(chatWindow),BorderLayout.CENTER);
- setSize(300,150);
- setVisible(true);
- }
- public void startRunning()
- {
- try{
- connectToServer();
- setupStreams();
- whileChatting();
- }catch(EOFException eofException)
- {
- showMessage("\n Client Treminated connection !!!!!!");
- }catch(IOException ioException)
- {
- ioException.printStackTrace();
- }finally{
- closeCrap();
- }
- }
- private void connectToServer() throws IOException
- {
- showMessage(" Attempting Connection........\n");
- connection=new Socket(InetAddress.getByName(serverIP),6789);
- showMessage("Connected to : " + connection.getInetAddress().getHostName());
- }
- private void setupStreams() throws IOException
- {
- output=new ObjectOutputStream(connection.getOutputStream());
- output.flush();
- input=new ObjectInputStream(connection.getInputStream());
- showMessage("\n your Message are Now good to go !!!!!!!!! \n");
- }
- private void whileChatting() throws IOException
- {
- ableToType(true);
- do{
- try{
- message=(String)input.readObject();
- showMessage("\n " + message);
- }catch(ClassNotFoundException classNotFoundException)
- {
- showMessage("\n Dont know the Object type ");
- }
- }while(!message.equals("SERVER - END"));
- }
- private void closeCrap()
- {
- showMessage("\n Closing crop down");
- ableToType(false);
- try{
- output.close();
- input.close();
- connection.close();
- }catch(IOException ioException)
- {
- ioException.printStackTrace();
- }
- }
- private void sendMessage(String message)
- {
- try{
- output.writeObject("CLIENT - " + message);
- output.flush();
- showMessage("\n CLIENT - " + message);
- }catch(IOException ioException)
- {
- chatWindow.append("\n Something messed up sending message Host !!!!!!");
- }
- }
- private void showMessage(final String m)
- {
- SwingUtilities.invokeLater(
- new Runnable(){
- public void run()
- {
- chatWindow.append(m);
- }
- });
- }
- private void ableToType(final boolean tof)
- {
- SwingUtilities.invokeLater(
- new Runnable(){
- public void run()
- {
- userText.setEditable(tof);
- }
- });
- }
- }
ClientTest
- import javax.swing.JFrame;
- public class ClientTest {
- public static void main(String[] args) {
- Client charlie;
- charlie=new Client("192.168.1.6");
- charlie.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- charlie.startRunning();
- }
- }
Comments
Post a Comment