View Single Post
Old 09-16-2011, 12:27 PM   PM User | #1
LSn145
New to the CF scene

 
Join Date: Sep 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
LSn145 is an unknown quantity at this point
Help...? Java code multi chat room app

I need some help with this application that i "Made" in java. I went through some source code on the internet found some useful code... But when i edited it to fit my needs... well it doesn't work Please help...

So the error is that when i connect the client and the server, the text that is being sent doesn't show up???

Any help would be much appreciative...
BTW i'm using Ubuntu(linux)

Heres the source:
Client.Main.java
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Main {

	static JFrame frame = new JFrame(".:Chat:.");
	static JButton send = new JButton("Send");
	static JTextArea chatBox = new JTextArea();
	static JTextField chatText = new JTextField();
	static OutputStreamWriter osw;
	static String name;
	static String IP;
	
	public static void main(String[] args) throws Exception {		
		
		name = JOptionPane.showInputDialog("Type in your name:");
		IP = JOptionPane.showInputDialog("Type in the ip:");
		drawGUI();
		serverThread.start();
		final Socket s = new Socket(InetAddress.getByName(IP), 1024);
		BufferedOutputStream wr = new BufferedOutputStream(s.getOutputStream());
		osw = new OutputStreamWriter(wr);
	}
	
	public static void drawGUI() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 505);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setLayout(null);
		chatBox.setBounds(0, 0, 500, 445);
		chatBox.setEditable(false);
		chatText.setBounds(5, 450, 400, 20);
		send.setBounds(410, 450, 75, 20);
		send.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				Click();
			}});
		frame.add(chatBox);
		frame.add(chatText);
		frame.add(send);
	}
	
	protected static void Click() {
		try {
			osw.write(name + " said: " + chatText.getText() + "\n");
			chatText.setText("");
			osw.flush();
		} catch (Exception e1) {}
	}

	public static Thread serverThread = new Thread(){
		ServerSocket serverSocket;
		Socket s;
		BufferedInputStream bis;
		InputStreamReader read;
		String storage;
		public void run(){
			storage = "Welcome To The Chat\n";
			try{
				serverSocket = new ServerSocket(1024);
				s = serverSocket.accept();
				bis = new BufferedInputStream(s.getInputStream());
				read = new InputStreamReader(bis);
				while(true){
					int c;
				      while ( (c = read.read()) != 13)
				        storage += ( (char) c);
					chatBox.setText(storage);
				}
			}catch(Exception e){}
		}
	};
	
}
Server.ConnectionHandler.java
Code:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;


public class ConnectionHandler implements Runnable {
	Socket s;
	InputStreamReader inS;
	char[] cbuf;
	String current;
	DataOutputStream outS;
	BufferedOutputStream os;
    OutputStreamWriter osw;
	
	public ConnectionHandler(Socket s){
		try{
			this.s = s;
			BufferedInputStream ins = new BufferedInputStream(s.getInputStream());
			inS = new InputStreamReader(ins);
			os = new BufferedOutputStream(s.getOutputStream());
			osw = new OutputStreamWriter(os);
		}catch(Exception e){}
	}	
	
	public void run(){
		try {
			while(true){
				int character;
			      StringBuffer process = new StringBuffer();
			      while((character = inS.read()) != 13) {
			        process.append((char)character);
			      }
			      osw.write(process.toString());
			      osw.flush();
			}
		} catch (Exception e) {}
	}
	
}
Server.Main.java
Code:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;


public class ConnectionHandler implements Runnable {
	Socket s;
	InputStreamReader inS;
	char[] cbuf;
	String current;
	DataOutputStream outS;
	BufferedOutputStream os;
    OutputStreamWriter osw;
	
	public ConnectionHandler(Socket s){
		try{
			this.s = s;
			BufferedInputStream ins = new BufferedInputStream(s.getInputStream());
			inS = new InputStreamReader(ins);
			os = new BufferedOutputStream(s.getOutputStream());
			osw = new OutputStreamWriter(os);
		}catch(Exception e){}
	}	
	
	public void run(){
		try {
			while(true){
				int character;
			      StringBuffer process = new StringBuffer();
			      while((character = inS.read()) != 13) {
			        process.append((char)character);
			      }
			      osw.write(process.toString());
			      osw.flush();
			}
		} catch (Exception e) {}
	}
	
}
And also i give you the full right to use this in other apps, kinda did it myself...
LSn145 is offline   Reply With Quote