Java Chat Program

Need help with errors in Java Chat Program

Page 1 of 1

3 Replies - 4136 Views - Last Post: 06 December 2009 - 11:12 PM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Java Chat Program

Post icon  Posted 05 December 2009 - 07:01 PM

Hello, I have a Java Chat Program but it is giving me these errors,

Remember, these are taking right from Command Prompt from the run.bat file, and I do not know what these mean.

ChatServer.java:4: cannot find symbol
symbol: class ConnectionManager
public class ChatServer extends ConnectionManager {
								^
ChatServer.java:26: cannot find symbol
symbol  : method getAddress(java.net.Socket)
location: class ChatServer
								System.out.println("Connection Accepted from: "
+ getAddress(socket));

  ^
ChatServer.java:30: cannot find symbol
symbol  : variable sockets
location: class ChatServer
										sockets[id] = socket;
										^
ChatServer.java:66: cannot find symbol
symbol  : variable sockets
location: class ChatServer
				for (int i = 0; i < sockets.length; i++) { //Happy? It's 0 now.
									^
ChatServer.java:67: cannot find symbol
symbol  : variable sockets
location: class ChatServer
						if (sockets[i] != null) {
							^
ChatServer.java:78: cannot find symbol
symbol  : method getAddress(java.net.Socket)
location: class ChatServer
						if(u != null && getAddress(u.socket) != "0.0.0.0") {
										^
UserSocket.java:96: cannot find symbol
symbol  : method getAddress(java.net.Socket)
location: class ChatServer
																		ban = ne
w File("./BannedHosts/" + u.cs.getAddress(socket) + ".dat").createNewFile();

							  ^
UserSocket.java:108: cannot find symbol
symbol  : variable sockets
location: class ChatServer
																		u.cs.soc
kets[u.userID] = null;
																			^
8 errors
Press any key to continue . . .


All right, now that you know the errors, let me post the code for the files.

ChatServer.java
import java.io.*;
import java.net.*;

public class ChatServer extends ConnectionManager {
	public boolean serverRunning = true;
	ServerSocket serverSocket = null;
	public static User[] users = new User[500];

	public ChatServer(int port) throws Exception {
		try {
			serverSocket = new ServerSocket(port);
			System.out.println("ChatServer started on port: "+port);
		}
		catch (IOException e) {
			System.out.println("Could not start server on port: " +port);
			System.exit(-1);
		}
	}

	public void run() {
		Socket socket = null;
		while (serverRunning) {
			try {
				socket = serverSocket.accept();
				socket.setTcpNoDelay(true);
				System.out.println("Connection Accepted from: " + getAddress(socket));
				int id = getFreeId();
				if (id != -1) {
					users[id] = new User(socket, id, this);
					sockets[id] = socket;
					outputAll(users[id].username + " has joined the channel!");
				}
				else {
					socket.close();
				}
				/*
				String filename = ("BannedHosts/" + getAddress(socket) + ".txt");
				File banned = new File(filename);
				if (banned.exists()) {
					try {
						users[id].rwSocket.write("\nYou have been banned from this server.\nPlease refer to the hoster for more details.");
					}
					catch (Exception e) {
					}
					try {
						socket.close();
						users[id].destruct();
					}
					catch (IOException e) {
						e.printStackTrace();
					}
					sockets[id] = null;
					users[id] = null;
				}
				*/	//The ban is messed up... sometimes it says someone is banned when the file is not even there...
				
			}
			catch (IOException e) {
				System.out.println("Client accept failed.");
				System.exit(-1);
			}
		}
	}

	public int getFreeId() {
		for (int i = 0; i < sockets.length; i++) { //Happy? It's 0 now.
			if (sockets[i] != null) {
				continue;
			}
			return i;
		}
		return -1;
	}

	public void outputAll(String message) {
		int count = 0;
		for(User u : users) {
			if(u != null && getAddress(u.socket) != "0.0.0.0") {
				try {
					u.rwSocket.write(message);
				}
				catch(Exception e) {
				}
			}
		}
	}
		
}


UserSocket.java
import java.net.*;
import java.io.*;

public class UserSocket {

	public User user;
	public Socket socket;
	public InputStream input;
	public OutputStream output;
	
	public UserSocket(User p, Socket s) {
	user = p;
		socket = s;
		try {
			input = socket.getInputStream();
			output = socket.getOutputStream();
	} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int read() throws Exception {
	if (input == null || input.available() < 1) {
			return -1;
		}
	byte[] userInputB = new byte[input.available()];
	input.read(userInputB);
	String userInput = new String(userInputB);
	String packetId = userInput.substring(0, 2);
	handleInput(userInput.substring(2), packetId);
	return 0;
}

	public void write(byte[] b) throws Exception {
		if (socket == null) {
			return;
		}
		output.write(b);
	}

	public void write(String s) throws Exception {
		if (socket == null) {
			return;
		}
	byte[] b = s.getBytes();
		output.write(b);
	}

	public void handleInput(String input, String packetId) {
		int packet = Integer.parseInt(packetId);
		switch (packet) {
			case 10: //Normal Text
				if(input != "") {
					user.cs.outputAll("10[" + user.username + "]: "+input);
				}
			break;

			case 11: //Username Change
				boolean taken = false;
				for (User u : user.cs.users) {
					if(u != null) {
						if(input.equalsIgnoreCase(u.username)) {
							taken = true;
							try {
								write("12That username is in-use!");
							}
							catch (Exception e) {
								e.printStackTrace();
							}
							return;
						}
					}
				}
				if(taken == false && !input.equalsIgnoreCase("server") && !input.contains(";") && !input.contains("\"") && !input.contains("\\") && !input.contains("}") 
									 && !input.contains("{") && !input.contains("[") && !input.contains("]") && !input.contains(")") && !input.contains("(")) {
					user.cs.outputAll("11" + user.username + " is now known as " + input);
					user.username = input;
				}
				else {
					try {
						write("12That username is invalid!");
					}	
					catch (Exception e) {
						e.printStackTrace();
					}
				}
			break;

			case 12: //Command (starts with /)
				if(input.startsWith("ban")) {
					boolean ban = false;
					for (User u : user.cs.users) {
						if(u != null) {
							if(u.username.equalsIgnoreCase(input.substring(4))) {
								try {
									ban = new File("./BannedHosts/" + u.cs.getAddress(socket) + ".dat").createNewFile();
								}
								catch (IOException e) {
									e.printStackTrace();
								}
								if(ban) {
									user.cs.outputAll("11" + input.substring(4) + " has been ip-banned!");
									try {
										u.socket.close();
									}
									catch (IOException e) {
									}
									u.cs.sockets[u.userID] = null;
									u.cs.users[u.userID] = null;
									user.destruct();
								}
								return;
							 }	
						}
					}
				}	
			break;
			
			case 14: //Quit
				System.out.println("14");
				user.cs.outputAll("11" + user.username + " has quit.");
			break;
		}
	}

}




User.java
import java.net.*;
import java.io.*;

public class User extends Thread{

public boolean reading = true;
public int userID;
public UserSocket rwSocket;
public PrintWriter print;
public Socket socket;
public String username = getUsername();
public ChatServer cs;

	public User(Socket socket, int id, ChatServer cs) {
 		   this.rwSocket = new UserSocket(this, socket);
		   this.userID = id;
		   this.print = new PrintWriter(rwSocket.output, 

false);
		   this.start();
		   this.socket = socket;
		   this.cs = cs;
	}
	public void destruct() {
		reading = false;
			try {
					rwSocket.input.close();
					rwSocket.output.close();
					rwSocket.socket.close();
			} catch (Exception e) {}
			rwSocket.input = null;
			rwSocket.output = null;
			rwSocket.socket = null;
			rwSocket = null;
	this.username = "";
		}

	public void run() {
		while (reading) {
			try {
				rwSocket.read();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			try {
				Thread.sleep(1);
			}
			catch (InterruptedException e) {
				System.out.println(e);
			}
		}
	}

	public String getUsername() { //Have fun Abraham.
		boolean found = false;
		String allUsernames = "";
		int random = (int) (Math.random() * (20000));
		if(random < 10000) {
			random += 10000;
		}
		while(!(found)) {
			for (User u : ChatServer.users) {
				if(u != null) {
					allUsernames += 

u.username;
				}
			}
			

if(!(allUsernames.contains(Integer.toString(random)))) {
				found = true;
			}
			else {
				random++;
			}
		}
		return ("User" + random);
	}

}




StartUp.java
import java.io.*;

public class StartUp {

	public static ChatServer ChatServer = null;
	
	public static void main(String[] args) throws IOException {
		try {
			ChatServer = new ChatServer(Integer.parseInt(args[0]));
			ChatServer.run();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}


Run.bat
@echo off
cd ../
title Server
java -Xmx256m -cp . StartUp 43595
pause


Compile.bat
@echo off
title Compile
"C:\Program Files\Java\jdk1.6.0_16\bin\javac.exe" *.java
pause


Thank You for any help appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Chat Program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Java Chat Program

Posted 05 December 2009 - 07:13 PM

Where is the class ConnectionManager that your ChatServer extends ?
Not a Java standard API class for sure
And the methods of these class that you call (like getAddress(socket)) do not exist
Was This Post Helpful? 0
  • +
  • -

#3 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: Java Chat Program

Posted 05 December 2009 - 08:48 PM

I fixed the problems, thank you pbl (one of the best java developers on D.I.C!)
Was This Post Helpful? 0
  • +
  • -

#4 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: Java Chat Program

Posted 06 December 2009 - 11:12 PM

i'm working on a java chat app, would you mind posting you working code? or tell what you did to fix?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1