KillerZ123 0 Newbie Poster

I am new to sockets and trying to make a simple two person chat program and I have everything working except for the receiving and printing of the messages to the jtextarea. I am not sure why it isn't working here is my code:

Driver:

import java.awt.*;
import javax.swing.*;

public class Client
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Client");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.getContentPane().add(new ClientPanel());
		
		frame.pack();
		frame.setVisible(true);
	}
}

Client code:

import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ClientPanel extends JPanel implements Runnable
{
	private PrintWriter outbound;
	private final int PORT = 5000;
	private JTextField tf = new JTextField();
	private JTextArea ta = new JTextArea();
	private Socket socket;
	private ServerSocket server;
	private Scanner inbound;	
	
	//---------------------------------------------------
	// Sets up the panel and asks the user if they want
	// to host or join a chat.
	//---------------------------------------------------
	public ClientPanel()
	{
		setPreferredSize(new Dimension(400, 400));
		
		int serverPort = 0;
		String serverHost = "", answer;		
		Scanner scan = new Scanner(System.in);
		
		System.out.print("Do you want to host the chat(y/n)? ");
		answer = scan.nextLine();
		
		if(answer.toLowerCase().equals("y"))
		{
			try
			{
				server = new ServerSocket(PORT);
				System.out.println("Waiting for other user.");
				socket = server.accept();
				inbound = new Scanner(socket.getInputStream());
				outbound = new PrintWriter(socket.getOutputStream(), true);
				new Thread(this).start();
			}
			catch(IOException e)
			{
				System.out.println(e);
			}
		}
		else
		{			
			System.out.print("Enter Other users host: ");
			serverHost = scan.nextLine();
			System.out.print("Enter Other users port: ");
			serverPort = scan.nextInt();		
			try
			{
				socket = new Socket(serverHost, serverPort);
				inbound = new Scanner(socket.getInputStream());
				outbound = new PrintWriter(socket.getOutputStream(), true);
				new Thread(this).start();
			}
			catch(UnknownHostException e)
			{
				System.out.println(e);
			}
			catch(IOException e)
			{
				System.out.println(e);
			}		
		}
		
		setLayout(new BorderLayout());
		add(tf, BorderLayout.NORTH);
		add(ta, BorderLayout.CENTER);
		tf.addActionListener(new MessageListener());		
	}	
	
	//------------------------------------------------------
	// Send a message received from the textfield listener
	// to other user and clears textfield.
	//------------------------------------------------------
	public void processMessage(String message)
	{
		outbound.print(message);
		
		tf.setText("");
	}
	
	//-----------------------------------------------------------
	// Thread to listen for incoming messages and prints them to
	// the textarea.
	//-----------------------------------------------------------
	public void run()
	{
		while(true)
		{
			String message = inbound.nextLine();
			//System.out.print(message);
			ta.append(message + "\n");
		}		
	}
	
	//---------------------------------------------------------------
	// Listens for the user to type a message into the textfield and
	// passes the text to the processMessage method.
	//---------------------------------------------------------------
	private class MessageListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			String text = tf.getText();
			//System.out.println(text);
			processMessage(text);
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.