Jump to content

Chatroom help! [java]


Reid

Recommended Posts

So pretty much my client is not receiving the text that that was sent to the server, been stuck at this for a few hours now if you can help that would be awesome thanks! 

 

 

Client.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

/**
 * 
 * @author reid dacosta
 *
 */
public class Client implements Runnable {

	private JPanel contentPane;
	private boolean running = false;
	private JFrame frame;
	private JTextArea messageArea;
	private JTextArea sendMessageArea;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JScrollPane scrollPane_2;
	private static String userName;

	private static DatagramSocket socket;
	private static InetAddress ip;

	private Thread send,listen;

	public static void main(String[] args) throws IOException {
		userName = JOptionPane.showInputDialog("Please enter your name!");
		new Client();
	}
	
	public void run() {
		listen();
	}

	public Client() {
		frame = new JFrame();
		frame.setTitle("rChat - [1.01 BETA]");
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(100, 100, 478, 340);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		frame.setContentPane(contentPane);
		contentPane.setLayout(null);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(6, 6, 322, 223);
		contentPane.add(scrollPane);

		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(334, 6, 138, 306);
		contentPane.add(scrollPane_1);

		scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(6, 235, 322, 78);
		contentPane.add(scrollPane_2);

		messageArea = new JTextArea();
		scrollPane.setViewportView(messageArea);
		messageArea.setEditable(false);
		messageArea.setLineWrap(true);

		JTextArea onlineArea = new JTextArea();
		scrollPane_1.setViewportView(onlineArea);
		onlineArea.setEditable(false);

		sendMessageArea = new JTextArea();
		scrollPane_2.setViewportView(sendMessageArea);
		sendMessageArea.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					e.consume();
					send(sendMessageArea.getText());
				}
			}

			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
			}
		});
		frame.setVisible(true);
		sendMessageArea.requestFocusInWindow();
		openConnection();
		running = true;
	}
	
	private void openConnection() {
		try {
			socket = new DatagramSocket();
			ip = InetAddress.getByName("localhost");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}

	private void send(String message) {
		//log(message);
		send(message.getBytes());
		sendMessageArea.setText("");
	}

	private void send(final byte[] data) {
		send = new Thread("Send") {
			public void run() {
				DatagramPacket packet = new DatagramPacket(data, data.length,ip, 1996);
				try {
					socket.send(packet);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		send.start();
	}
	
	public String receive() {
		byte[] data = new byte[1024];
		DatagramPacket packet = new DatagramPacket(data, data.length);
		try {
			socket.receive(packet);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String message = new String(packet.getData());
		return message;
	}

	
	public void listen() {
		listen = new Thread("Listen") {
			public void run() {
				while (running) {
					String message = receive();
					System.out.println(message);
					log(message);
				}
			}
		};
		listen.start();
	}

	private void log(String str) {
		Date today = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
		String date = dateFormat.format(today);
		messageArea.append(String.format("(" + date + ") " + userName + ": " + str + "\n"));
	}
}

Server.java

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;

/**
 * 
 * @author reid dacosta
 *
 */
public class Server extends JFrame implements Runnable {

	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextArea textArea;
	
	private DatagramSocket socket;
	private static final int PORT = 1996;
	private boolean running = false;
	private Thread run, recive;

	public Server() {
		setTitle("rChat Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 200);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(6, 6, 438, 166);
		contentPane.add(scrollPane);

		textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		scrollPane.setViewportView(textArea);
		DefaultCaret caret = (DefaultCaret)textArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		setVisible(true);
		
		startServer();
	}

	public static void main(String[] args) {
		new Server();
	}

	@Override
	public void run() {
		running = true;
		recive();
	}

	private void startServer() {
		try {
			socket = new DatagramSocket(PORT);
		} catch (SocketException e) {
			e.printStackTrace();
		}
		run = new Thread(this, "Server");
		run.start();
	}

	private void recive() {
		recive = new Thread("Receive") {
			public void run() {
				while (running) {
					byte[] data = new byte[1024];
					DatagramPacket packet = new DatagramPacket(data,data.length);
					try {
						socket.receive(packet);
					} catch (IOException e) {
						e.printStackTrace();
					}
					String str = new String(packet.getData());
					log(str);
				}
			}
		};
		recive.start();
	}
	
	private void log(String str) {
		textArea.append("Someone said : " + str + "\n");
	}
	
}
Edited by Reid
Link to comment
Share on other sites

Java recently added "New IO" which allow you to create easily scalable TCP based client/server applications using a selector. A selector allows you to handle many Sockets (SocketChannel in NIO) using a single thread. I don't recommend it, but with a NIO selector you can manage UDP channels as well.

I recommend looking into it

Edited by FrostBug
  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...