Skip to main content

Network communicator (HashMap) problem

4 replies [Last post]
yoss
Offline
Joined: 2010-01-25
Points: 0

Hello. I'm writing a network communicator. At the moment infinity number of clients can connect and write text to server. When server receive any message it sends it to all connected at this moment clients. Now, I want to make a comunication between two clients. I'm using name of user for identification (for now). I heard, that to make such communication, first of all I have to put into HashMap key - some ID (here it is user ID) and value - instance of class, which is responsible for communication). User ID is placed in map correctly, for every new connected to server user, but I can't put class correctly (it is always null). I have tried many times and I don't know how to solve it. I don't know how to add instance of the class corrrectly . Could someone help me, explain me this?

<br />
Method from ServerCommunicationClass responsible for listening connections. It run a new Thread for every new connection:</p>
<p>public static void lauch_server(int port,JTextArea receive_field){</p>
<p>try{<br />
	server_socket = new ServerSocket(port);<br />
	receive_field.append("Serwer jest uruchomiony...");<br />
	}catch (IOException e) {<br />
	e.printStackTrace();<br />
	}<br />
ServerClientManagerClass client_manager_class = new ServerClientManagerClass();<br />
	while(true){<br />
	ServerReadingStreamsThreadClass serv_thr_class;<br />
	try{<br />
		serv_thr_class = new ServerReadingStreamsThreadClass(server_socket.accept(), receive_field, client_manager_class);<br />
		Thread server_thread = new Thread(serv_thr_class);<br />
		server_thread.start();<br />
	}catch (IOException e) {<br />
		e.printStackTrace();<br />
	}<br />
	}<br />
}</p>
<p>Now, the class which is responsible for sending data by in/out streams:</p>
<p>class ServerReadingStreamsThreadClass implements Runnable{</p>
<p>	private Socket new_socket;<br />
	private DataInputStream in_data;<br />
	private DataOutputStream out_data;<br />
	private JTextArea receive_field;<br />
	ServerClientManagerClass scmc = new ServerClientManagerClass();</p>
<p>ServerReadingStreamsThreadClass(Socket new_socket, JTextArea receive_field, ServerClientManagerClass scmc){<br />
		this.new_socket = new_socket;<br />
			this.receive_field = receive_field;<br />
	this.scmc = scmc;<br />
	}</p>
<p>	public void run() {<br />
		String message;<br />
		String output;<br />
		try{<br />
		in_data = new DataInputStream(new_socket.getInputStream());<br />
		out_data = new DataOutputStream(new_socket.getOutputStream());<br />
			scmc.add_client_streams_2_list(out_data);<br />
		}catch (IOException e) {<br />
			e.printStackTrace();<br />
		}<br />
		while(true){<br />
	try{<br />
	while((message = in_data.readUTF())!=null){<br />
	output = scmc.process_message(message);<br />
	receive_field.append("\n"+output);<br />
	scmc.broadcast_message(output);<br />
	}<br />
	}catch (IOException e) {<br />
		e.printStackTrace();<br />
	}</p>
<p>		}<br />
		}<br />
	}</p>
<p>And the last part 2 methods from ServerClientManagerClass responsible forprocessing the input, retrieving a user name, etc...:</p>
<p>...</p>
<p>public String process_message(String message){<br />
String result = null;<br />
if(message.startsWith("#connect")){<br />
     map.put(user_nick(message), ser_rea_thread);//putting user name and thread in map - here is the problem<br />
     result = "\nConnected is user: "+user_nick(message);<br />
     System.out.print("\nMapa: "+map.toString());<br />
return result;<br />
}<br />
...<br />
public String user_nick(String message){<br />
String user_nick = null;<br />
user_nick = split_message_method(message)[1];<br />
return user_nick;<br />
}<br />

edit by yoss

Message was edited by: yoss

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
walterln
Offline
Joined: 2007-04-17
Points: 0

Where do you set ser_rea_thread to something not null? Set a breakpoint there and figure out why it isn't called.

BTW, use [ code ] // code here [ /code ] without spaces to post code.
[code] // code here [/code]

yoss
Offline
Joined: 2010-01-25
Points: 0

I create ser_rea_thread in the same class which adds values to the HashMap.
It goes like this:

[code]
public class ServerClientManagerClass{
...
ServerReadingStreamsThreadClass ser_rea_thread;
....
//and the method putting the data into HashMap:
public String process_message(String message){
String result = null;
if(message.startsWith("#connect")){
map.put(user_nick(message), ser_rea_thread);//putting user name and thread in map
result = "\nConnected is user: "+user_nick(message);
System.out.print("\nMapa: "+map.toString());
}else if(...)
}
return result;
}
[/code]

walterln
Offline
Joined: 2007-04-17
Points: 0

That still doesn't show where you do initialize ser_rea_thread. You should have something like this somewhere:

[code] ser_rea_thread = new ServerReadingStreamsThreadClass(...); [/code]

yoss
Offline
Joined: 2010-01-25
Points: 0

I have solved the problem (today morning) by moving method responsible for creating instance of class to class which contains the HashMap and every new client is added to HashMap. Now, I want to make communication between two clients. I there are some problems, I will let You know :). Thanks for interesting.
Regards