Results 1 to 2 of 2
  1. #1
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Simple chat using threads and MVC pattern

    Hey all!
    Been some time since I asked a question here, for good and bad.. :P

    Right now I'm working on my final assignments where we are supposed to use threads and/or communicate between programs. What better thing to do than code a simple chat using threads that listen for incoming messages? =)

    I've set it up accordingly to the MVC design pattern. My View package contains a simple GUI class that makes some system calls to my Controller class object. The controller then creates a new ListeningThread that opens a socket to a server and listens for incoming messages. This is the first time I'm using threads, and I have a problem that seem to be that the thread is never really started.

    I don't get any errors, but neither are any of my debug-messages printed out, which makes me believe that the code isn't executing the way I want it to be.

    In my GUI, there is a JOptionPane that takes the IP-address to connect to, saves it as a string and then calls the openSocket(ipAddress) method in the controller. The debug printout in line 16 of the controller is printed out to the console.

    Java Code:
    package controller;
    
    import model.ListeningThread;
    import view.SimpleGUI;
    
    public class Controller {
    	
    	SimpleGUI gui;
    
    	public Controller(SimpleGUI gui) {
    		this.gui = gui;
    	}
    
    	public void openSocket(String ipAddress) {
    		new ListeningThread(this, ipAddress);
    		System.out.println("ListeningThread created!");
    	}
    }
    The controller then goes on to create a ListeningThread which is supposed to open up the socket and start listening. Note the debug printout at line 24. This never is never executed and I do not understand why. When using
    Java Code:
    implements Runnable
    I thought the thread started when created?

    Java Code:
    package model;
    
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.Socket;
    
    import controller.Controller;
    
    public class ListeningThread implements Runnable{
    	
    	private Socket socket;
    	private ObjectInputStream ois;
    	private Controller controller;
    	private String ipAddress;
    	
    	
    	public ListeningThread(Controller controller, String ipAddress){
    		this.controller = controller;
    		this.ipAddress = ipAddress;
    	}
    	
    	@Override
    	public void run() {
    		System.out.println("Thread running");
    		try {
    			socket = new Socket(ipAddress, 1337);
    			ois = new ObjectInputStream(socket.getInputStream());
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}
    		
    		//A loop that runs forever
    		while(true){
    			try {
    				String fromServer = ois.readObject().toString();
    				System.out.println(fromServer);
    			} catch (IOException e) {
    				e.printStackTrace();
    			} catch (ClassNotFoundException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    I'm unsure if there is any need to post the code for the server. It's from Oracle and it's been modified to use more proper reader/writer classes.

    Java Code:
    package model;
    
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.Socket;
    
    import controller.Controller;
    
    public class ListeningThread implements Runnable{
    	
    	private Socket socket;
    	private ObjectInputStream ois;
    	private Controller controller;
    	private String ipAddress;
    	
    	
    	public ListeningThread(Controller controller, String ipAddress){
    		this.controller = controller;
    		this.ipAddress = ipAddress;
    	}
    	
    	@Override
    	public void run() {
    		System.out.println("Thread running");
    		try {
    			socket = new Socket(ipAddress, 1337);
    			ois = new ObjectInputStream(socket.getInputStream());
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}
    		
    		//A loop that runs forever
    		while(true){
    			try {
    				String fromServer = ois.readObject().toString();
    				System.out.println(fromServer);
    			} catch (IOException e) {
    				e.printStackTrace();
    			} catch (ClassNotFoundException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    I've tried to add breakpoints to be able to debug it, but when running both the server and the client, I can't get the debug view to follow the code properly.

    I'm grateful for any help or idea anyone can contribute with!
    Thank you,
    Z!

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Simple chat using threads and MVC pattern

    I quickly realised that there is a big problem with my code. The object of the class that implements Runnable is indeed created, though there is never a thread created of it.
    Working on a fix!

    Following up with the fix:

    Java Code:
    	public void openSocket(String ipAddress) {
    		ListeningThread lt = new ListeningThread(this, ipAddress);
    		Thread t = new Thread(lt);
    		t.start();
    		System.out.println("ListeningThread created!");
    	}
    Thread is running! No incoming messages though..? Hmm, something wrong on server side.
    Debugging tells me there is something wrong on line 25 on the server side. I added breakpoints and followed the execution, but it never goes further than line 25..

    Java Code:
    import java.net.*;
    import java.io.*;
     
    public class SimpleServer {
        public static void main(String[] args) throws IOException, ClassNotFoundException {
     
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(1337);
            } catch (IOException e) {
                System.err.println("Could not listen on port: 1337.");
                System.exit(1);
            }
     
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.exit(1);
            }
     
            System.out.println("Create reader/writer");
            ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
            System.out.println("Successfully created!");
            String inputLine, outputLine;
            outputLine = "Testmessage!";
            oos.writeObject(outputLine);
            System.out.println(outputLine + " sent!");
     
            while ((inputLine = (String) ois.readObject()) != null) {
                 outputLine = "Testmessage!";
                 System.out.println(outputLine);
                 oos.writeObject(outputLine);
                 if (outputLine.equals("Bye."))
                    break;
            }
            oos.close();
            ois.close();
            clientSocket.close();
            serverSocket.close();
        }
    }
    Last edited by Zyril; 10-06-2012 at 01:31 PM.

Similar Threads

  1. how to do multiclient private chat using socets and threads?
    By tanu.kansal in forum Threads and Synchronization
    Replies: 27
    Last Post: 08-25-2011, 05:31 PM
  2. Simple RMI chat application
    By usmangt in forum New To Java
    Replies: 3
    Last Post: 09-27-2010, 07:37 AM
  3. Simple TCP chat application not working for me
    By sundarrajan in forum New To Java
    Replies: 0
    Last Post: 08-13-2009, 08:31 AM
  4. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 04:00 PM
  5. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •