Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: IM Program Client Side Problems

  1. #1
    Junior Member drgy55's Avatar
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Mellow
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default IM Program Client Side Problems

    Hello! I have attempted to write a very basic IM program that requires a server and a client program. The problem I have run in to is that the client CAN connect to the server, but until the session is ended, the client is not able to see any of the information in the chat window (JTextArea) coming from the Server. After the connection is ended, all of that information appears and is added to the chat log. I have found only one way to remedy the problem, but it does not allow for the functionality that I want. Here is the code and then I will explain how I have been able to fix the problem:

    //SERVER PROGRAM
    import javax.swing.*;
     
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.text.*;
     
    public class IM 
    {
     
    //OBJECTS
     
    //GUI
    JFrame window = new JFrame("Rohan's IM Program - SERVER");
    JTextArea chatWindow = new JTextArea(20, 40);
    JScrollPane scrollPane = new JScrollPane(chatWindow);
    JLabel label1 = new JLabel("Start  chatting (type \"END\" to end session): ");
    JTextField userText = new JTextField(35);
    JButton send = new JButton("Send");
     
    //I/O
    ServerSocket serverSocket;
    Socket connection;
    ObjectOutputStream output;
    ObjectInputStream input;
     
    //OTHER
    String message;
    File f = new File("chat_log.txt");
    BufferedWriter write;
    StringBuilder sb = new StringBuilder();
     
     
    	//MAIN PROGRAM
    	//Main Method
    	public void initialize()
    	{
     
    		//Window settings
    		window.setSize(550, 550);
    		window.setLayout(new FlowLayout(FlowLayout.LEADING, 30, 10));
     
    		//Text area settings
    		chatWindow.setEditable(false);
     
    		//Text field settings
    		userText.setEditable(false);
     
    		//Send Button
    		send.addActionListener(
    				new ActionListener()
    				{
     
    					public void actionPerformed(ActionEvent e)
    					{
    						try {
    						sendMessage(userText.getText());
    						} catch (IOException e1) {
    							e1.printStackTrace();
    						}
    					}
    				});
     
    		//Window settings cont.
    		window.add(scrollPane, BorderLayout.NORTH);
    		window.add(label1, BorderLayout.SOUTH);
    		window.add(userText);
    		window.add(send);
     
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setVisible(true);
     
    		try {
    		startServer();
    		} catch (IOException e2){
    			e2.printStackTrace();
    		}
    	}
     
     
    	//START THE SERVER
    	public void startServer() throws IOException
    	{
     
    		try{
    			serverSocket = new ServerSocket(6789, 10);
     
    			while (true)
    			{
    				try {
     
    					waitForConnection();
    					setupIO();
    					whileChatting();
    				} catch (IOException e1) {
    					showMessage("\nConnection has ended!");
    				}finally {
    					closeEverything();
    					chatLog();
    				}
    			}
    		} catch (IOException e){
    			e.printStackTrace();
    		} 
    	}
     
    	//MAIN CHAT METHODS
    	//waitForConnection method
    	private void waitForConnection() throws IOException
    	{
     
    		showMessage("\nWaiting for a connection...");
    		connection = serverSocket.accept();
    		showMessage("\nConnected to " + connection.getInetAddress().getHostAddress() + " ! \nSetting up streams...");
    	}
     
    	//setupIO method
    	private void setupIO() throws IOException
    	{
     
    		output = new ObjectOutputStream(connection.getOutputStream());
    		output.flush();
    		input = new ObjectInputStream(connection.getInputStream());
     
    		showMessage("\nStreams set up!\n");
    	}
     
    	//whileChatting method
    	private void whileChatting() throws IOException
    	{
     
    		userText.setEditable(true);
     
    		do {
    			try {
    			message = (String) input.readObject();
    			showMessage("\nCLIENT - " + message);
    			} catch (ClassNotFoundException e) {
    				showMessage("\n <ERROR: Unknown message from client>");
    				}
    			}while(!message.equals("END"));
    	}
     
     
    	//OTHER METHODS
    	//sendMessage
    	public void sendMessage(String x) throws IOException
    	{
     
    		output.writeObject(x);
    		output.flush();
    		showMessage("\nSERVER - " + x);
     
    		if (userText.getText().equals("END")){
    			connection.close();
     
    			userText.setText("");
    			userText.updateUI();
    		}
    		else {
    			userText.setText("");
    			userText.updateUI();
    		}
    	}
     
    	//showMessage
    	public void showMessage(String x) throws IOException
    	{
     
    		chatWindow.append(x);
    		chatWindow.updateUI();
     
    		sb.append(x);
    		sb.append(System.getProperty("line.separator"));
    	}
     
    	//chatLog
    	public void chatLog()
    	{
     
    		try {
    			write = new BufferedWriter(new FileWriter(f, true));
     
    			String chat = sb.toString();
    			write.write(chat);
    			write.newLine();
    			write.write("-------------------------------------------------------------------------------------");
    			write.newLine();
    			sb.delete(0, sb.length());
    			write.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
     
    	//closeEverything
    	private void closeEverything() throws IOException
    	{
     
    		userText.setEditable(false);
    		showMessage("\nClosing connections...");
    		output.close();
    		input.close();
    		connection.close();
    		showMessage("\nSession terminated.\n");
    	}
     
    }

    //CLIENT PROGRAM
    import javax.swing.*;
     
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
     
    public class IM 
    {
     
    //OBJECTS
     
    //GUI
    JFrame window = new JFrame("Rohan's IM Program - CLIENT");
    JTextArea chatWindow = new JTextArea(20, 40);
    JScrollPane scrollPane = new JScrollPane(chatWindow);
    JLabel label1 = new JLabel("Start  chatting (type \"END\" to end session): ");
    JTextField userText = new JTextField(35);
    JButton send = new JButton("Send");
    JButton connect = new JButton("Connect");
     
    //I/O
    Socket connection;
    ObjectOutputStream output;
    ObjectInputStream input;
     
    //OTHER
    StringBuilder sb = new StringBuilder();
    String message;
    static String ip = "nothin";
     
    File f = new File("chat_log.txt");
    BufferedWriter write;
     
     
    	//MAIN PROGRAM
    	//Main Method
    	public void initialize()
    	{
     
    		//Window settings
    		window.setSize(550, 550);
    		window.setLayout(new FlowLayout(FlowLayout.LEADING, 30, 10));
     
    		//Text area settings
    		chatWindow.setEditable(false);
     
    		//Text field settings
    		userText.setEditable(false);
     
    		//Send Button
    		send.addActionListener(
    				new ActionListener()
    				{
     
    					public void actionPerformed(ActionEvent e)
    					{
    						try {
    						sendMessage(userText.getText());
    						} catch (IOException e1) {
    							e1.printStackTrace();
    						}
    					}
    				});
     
    		//Connect Button
    		connect.addActionListener(
    				new ActionListener()
    				{
     
    					public void actionPerformed(ActionEvent e1)
    					{
     
    						ip = JOptionPane.showInputDialog("Enter server IP address.");
    						startIM();
    					}
    				});
     
    		//Window settings cont.
    		window.add(connect);
    		window.add(scrollPane, BorderLayout.NORTH);
    		window.add(label1, BorderLayout.SOUTH);
    		window.add(userText);
    		window.add(send);
     
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setVisible(true);
    	}
     
     
    	//START THE SESSION
    	public void startIM()
    	{
     
    		try {		
    			connectToServer();
    			setupIO();
    			whileChatting();
    			}catch (IOException e) {
    				chatLog();
    				JOptionPane.showMessageDialog(null,"Invalid IP address or server closed.");
    				initialize();
    		} 
    	}
     
    	//MAIN CHAT METHODS
    	//connectToServer method
    	private void connectToServer() throws IOException
    	{
    		showMessage("\nTrying to connect to server at " + ip + " ...");
    		connection = new Socket(InetAddress.getByName(ip), 6789);
    		showMessage("\nConnected to " + connection.getInetAddress().getHostAddress() + " !");
    	}
     
    	//setupIO method
    	private void setupIO() throws IOException
    	{
     
    		output = new ObjectOutputStream(connection.getOutputStream());
    		output.flush();
    		input = new ObjectInputStream(connection.getInputStream());
     
    		showMessage("\nStreams set up!\n");
    	}
     
    	//whileChatting method
    	private void whileChatting() throws IOException
    	{
     
    		userText.setEditable(true);
     
    		do {
    			try {
    			message = (String) input.readObject();
    			showMessage("\nSERVER - " + message);
    			} catch (ClassNotFoundException e) {
    				showMessage("\n <ERROR: Unknown message from client>");
    			}
    				}while(!message.equals("END"));
     
    			chatLog();
    			JOptionPane.showMessageDialog(null,"Server has ended the session.");
    			initialize();
     
    	}
     
     
    	//OTHER METHODS
    	//sendMessage
    	public void sendMessage(String x) throws IOException
    	{
     
    		output.writeObject(x);
    		output.flush();
    		showMessage("\nCLIENT - " + x);
     
    		userText.setText("");
    		userText.updateUI();
    	}
     
    	//showMessage
    	public void showMessage(String x)
    	{
     
    		chatWindow.append(x);
    		chatWindow.updateUI();	
     
    		sb.append(x);
    		sb.append(System.getProperty("line.separator"));
    	}
     
    	//chatLog
    	public void chatLog()
    	{
     
    		try {
    			write = new BufferedWriter(new FileWriter(f, true));
     
    			String chat = sb.toString();
    			write.write(chat);
    			write.newLine();
    			write.write("-------------------------------------------------------------------------------------");
    			write.newLine();
    			sb.delete(0, sb.length());
    			write.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

    The only fix I have found is to take away the "Connect" button functionality, have the user input the ip right from the start of the initialize() method, and then call startIM() at the end of initialize(). Any attempts at getting the button to work result in the same problem of not being able to chat and not receiving any information in the chat window. Everything else works fine, including the chat log.
    I have even tried creating a boolean that is set to true when "Connect" is pressed that will do essentially the same thing as the solution I mentioned. I wrapped the prompt for the ip and the call for startIM() in "if" statements that are only fired when the boolean is set to true, making it so that when "Connect" is pressed initialize() will re-run and the user will be prompted for the ip and startIM() will run at the end. So, my only hypothesis is that something about the button is causing an issue, but I haven't been able to figure how this can be. Thank you for any help in advance!
    Last edited by drgy55; July 1st, 2014 at 12:35 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: IM Program Client Side Problems

     
    					public void actionPerformed(ActionEvent e1)
    					{
     
    						ip = JOptionPane.showInputDialog("Enter server IP address.");
    						startIM();
    					}

    Swing is single threaded, running all it's code on the EDT. The code above is calling startIM() on the EDT which in turn enters into a do/while loop, not allowing the EDT to do anything until complete (including refresh the 'log'). One solution: place the I/O into a separate thread.

  3. The Following User Says Thank You to copeg For This Useful Post:

    drgy55 (July 1st, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: IM Program Client Side Problems

    Where is the driver for executing these classes? Some class with a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Junior Member drgy55's Avatar
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Mellow
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: IM Program Client Side Problems

    Quote Originally Posted by copeg View Post
     
    					public void actionPerformed(ActionEvent e1)
    					{
     
    						ip = JOptionPane.showInputDialog("Enter server IP address.");
    						startIM();
    					}

    Swing is single threaded, running all it's code on the EDT. The code above is calling startIM() on the EDT which in turn enters into a do/while loop, not allowing the EDT to do anything until complete (including refresh the 'log'). One solution: place the I/O into a separate thread.
    Ah, perfect! That's exactly what I needed to know, many thanks! I will try to implement that solution in just a bit here.

    Norm, sorry I didn't include the Main class, this is all that's in it (for both server and client side programs):
    public class Main 
    {
     
    	public static void main(String[] args)
    	{
     
    		IM imo = new IM();
    		imo.initialize();
    	}
    }

Similar Threads

  1. Client side certificate authentication in J2EE
    By vishal.mestri in forum Web Frameworks
    Replies: 0
    Last Post: March 6th, 2014, 01:46 AM
  2. Replies: 1
    Last Post: October 7th, 2013, 11:36 AM
  3. Creating an EOF and Acknowledgement on client side
    By Jess17 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2013, 08:03 AM
  4. InputStream Problem at Client Side
    By pavan in forum Web Frameworks
    Replies: 1
    Last Post: March 26th, 2010, 03:21 AM
  5. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM

Tags for this Thread