SupremeBeing
asked on
server client communication
Hi,
i'm relatively new with java so don't be suprised if i ask stupid questions ;)
I'm working on a multi-user chat application that i'm making for a school project.
I already succeeded in making a connection with the server and exchange messages between that server and that client.
However when trying to write some code so that a client can receive messages from other clients too (not only from server or himself) i got stuck :(
the code i have written isn't complete to do this job but should be step 1
It all compiles very well, but when running i get some exception errors.... :(
I would be very greatfull if someone would take a quick look at my code and give me a solution or correction for the error
For experienced programmers this should only take 5 minutes i think.
And i wouldn't be asking this if i wasn't searching for this over a period of more then 2 weeks, i just can't find the answer myself, I need help :)
You can find the code on this hotmail-adres: chat_probs@hotmail.com
password:
expert
In the email you will also find compiling instructions
Btw you should adjust hostname (look in dos for your hostname =>ipconfig /all) in code (not by typing it in)
I used port: 4444
If you have any questions email me : levawa@vt4.net
NO SPAM PLEASE
Thanks in advance dudes,
SupremeBeing
i'm relatively new with java so don't be suprised if i ask stupid questions ;)
I'm working on a multi-user chat application that i'm making for a school project.
I already succeeded in making a connection with the server and exchange messages between that server and that client.
However when trying to write some code so that a client can receive messages from other clients too (not only from server or himself) i got stuck :(
the code i have written isn't complete to do this job but should be step 1
It all compiles very well, but when running i get some exception errors.... :(
I would be very greatfull if someone would take a quick look at my code and give me a solution or correction for the error
For experienced programmers this should only take 5 minutes i think.
And i wouldn't be asking this if i wasn't searching for this over a period of more then 2 weeks, i just can't find the answer myself, I need help :)
You can find the code on this hotmail-adres: chat_probs@hotmail.com
password:
expert
In the email you will also find compiling instructions
Btw you should adjust hostname (look in dos for your hostname =>ipconfig /all) in code (not by typing it in)
I used port: 4444
If you have any questions email me : levawa@vt4.net
NO SPAM PLEASE
Thanks in advance dudes,
SupremeBeing
Post your exception errors here. By doing this other people with similar errors might be helped as well.
ASKER
in client window
----------------
Exception in thread "main" java.lang.NullPointerExcep tion: null string
at sun.awt.windows.WTextAreaP eer.insert Text(Nativ e Method)
at java.awt.TextArea.insertTe xt(TextAre a.java:306 )
at java.awt.TextArea.appendTe xt(TextAre a.java:332 )
at java.awt.TextArea.append(T extArea.ja va:323)
at Chat.<init>(Chat.java:75)
at Client.connect(Client.java :20)
at Client.main(Client.java:40 )
in server window
----------------
Exception in thread "main" java.lang.NullPointerExcep tion
at UserManagement.addClient(U serManagem ent.java:5 0)
at UserManagement.ConnectieGe gevens(Use rManagemen t.java:44)
at MultiServerThread.zendGege vens(Multi ServerThre ad.java:29 )
at MultiServerThread.<init>(M ultiServer Thread.jav a:16)
at MultiServer.main(MultiServ er.java:18 )
----------------
Exception in thread "main" java.lang.NullPointerExcep
at sun.awt.windows.WTextAreaP
at java.awt.TextArea.insertTe
at java.awt.TextArea.appendTe
at java.awt.TextArea.append(T
at Chat.<init>(Chat.java:75)
at Client.connect(Client.java
at Client.main(Client.java:40
in server window
----------------
Exception in thread "main" java.lang.NullPointerExcep
at UserManagement.addClient(U
at UserManagement.ConnectieGe
at MultiServerThread.zendGege
at MultiServerThread.<init>(M
at MultiServer.main(MultiServ
ASKER
in client window
----------------
Exception in thread "main" java.lang.NullPointerExcep tion: null string
at sun.awt.windows.WTextAreaP eer.insert Text(Nativ e Method)
at java.awt.TextArea.insertTe xt(TextAre a.java:306 )
at java.awt.TextArea.appendTe xt(TextAre a.java:332 )
at java.awt.TextArea.append(T extArea.ja va:323)
at Chat.<init>(Chat.java:75)
at Client.connect(Client.java :20)
at Client.main(Client.java:40 )
in server window
----------------
Exception in thread "main" java.lang.NullPointerExcep tion
at UserManagement.addClient(U serManagem ent.java:5 0)
at UserManagement.ConnectieGe gevens(Use rManagemen t.java:44)
at MultiServerThread.zendGege vens(Multi ServerThre ad.java:29 )
at MultiServerThread.<init>(M ultiServer Thread.jav a:16)
at MultiServer.main(MultiServ er.java:18 )
----------------
Exception in thread "main" java.lang.NullPointerExcep
at sun.awt.windows.WTextAreaP
at java.awt.TextArea.insertTe
at java.awt.TextArea.appendTe
at java.awt.TextArea.append(T
at Chat.<init>(Chat.java:75)
at Client.connect(Client.java
at Client.main(Client.java:40
in server window
----------------
Exception in thread "main" java.lang.NullPointerExcep
at UserManagement.addClient(U
at UserManagement.ConnectieGe
at MultiServerThread.zendGege
at MultiServerThread.<init>(M
at MultiServer.main(MultiServ
The object you are trying to pass is null. Are you sure you have initialized it and assigned a non-null value?
ASKER
I think it has something to do with the following
i send a String where i must send an Object
is there a way around without rewriting all my streams
public void sendinfo(){
String gegevens = nick+" "+"test"+" "+"gegevens";
sendData(gegevens);
}
public boolean sendData(String data){
try{
out = new ObjectOutputStream(socket. getOutputS tream());
out.writeObject(data);
}catch(IOException e ){System.out.println("test 2");
return false; }
return true;
}
i send a String where i must send an Object
is there a way around without rewriting all my streams
public void sendinfo(){
String gegevens = nick+" "+"test"+" "+"gegevens";
sendData(gegevens);
}
public boolean sendData(String data){
try{
out = new ObjectOutputStream(socket.
out.writeObject(data);
}catch(IOException e ){System.out.println("test
return false; }
return true;
}
> I think it has something to do with the following i send a String where i must send an Object
It shouldn't have, no. "Object" is a superclass of all classes so you can freely pass a "String" type where there is an "Object" type expected.
It is not a type problem but a pointer problem. The object you are pointing at is null. You probably forgot to initialize it somewhere.
Can you post some sample code before the following lines:
Chat.java:75 (post from 70-75 lines)
Client.java:20 (post from 15-20)
MultiServerThread.java:29 (from 10-30)
It shouldn't have, no. "Object" is a superclass of all classes so you can freely pass a "String" type where there is an "Object" type expected.
It is not a type problem but a pointer problem. The object you are pointing at is null. You probably forgot to initialize it somewhere.
Can you post some sample code before the following lines:
Chat.java:75 (post from 70-75 lines)
Client.java:20 (post from 15-20)
MultiServerThread.java:29 (from 10-30)
ASKER
Think this would be easier
=>
You can find the code on this hotmail-adres: chat_probs@hotmail.com
password:
expert
no?
=>
You can find the code on this hotmail-adres: chat_probs@hotmail.com
password:
expert
no?
I mentioned it earlier. Other people might have similar problems. Better post the code here.
ASKER
Oh ,i see ! sorry :)
In any case if the prob get solved i plan to post the solution here
Here is the code =>
Chat.java:75 (post from 67-78 lines)
-------------------------- ----------
aFrame = new JFrame("Simple Internet Talker");
aFrame.setContentPane(aDes ktopPane);
aFrame.setSize(750,500);
aFrame.setVisible(true);
aFrame.setDefaultCloseOper ation(JFra me.EXIT_ON _CLOSE);
zendGegevens();
String message = " ";
while (true) {
message = leesData();
aTextArea.append(message);
System.out.println(message );
}
Client.java:20 (post from 14-21)
-------------------------- ------
public void connect(){
Chat c;
try{
socket = new Socket("server",4444);
}catch (Exception e) {System.err.println("test9 9");System .out.print ln(e);}
c = new Chat(nick,socket);
}
MultiServerThread.java:29 (from 10-30)
-------------------------- ---------- --
private String str=null;
public MultiServerThread(Socket socket) {//constructor
super("MultiServerThread") ;// zet titel in venster
this.socket = socket;
zendGegevens();
}
//public voegClientBij(Socket s,ObjectInputStream in,ObjectOutputStream out,String naam)
//{
// Client
// ((Server) servers.elementAt(i)).addC hatter(con nection,in put,output ,name,info );
//}
public void zendGegevens(){
try{
out = new ObjectOutputStream(socket. getOutputS tream());
in = new ObjectInputStream(socket.g etInputStr eam());
UserManagement um = new UserManagement();
um.ConnectieGegevens(socke t,in,out);
}catch(IOException e ){System.err.println("test 4"); System.out.println("test4" );}
}
In any case if the prob get solved i plan to post the solution here
Here is the code =>
Chat.java:75 (post from 67-78 lines)
--------------------------
aFrame = new JFrame("Simple Internet Talker");
aFrame.setContentPane(aDes
aFrame.setSize(750,500);
aFrame.setVisible(true);
aFrame.setDefaultCloseOper
zendGegevens();
String message = " ";
while (true) {
message = leesData();
aTextArea.append(message);
System.out.println(message
}
Client.java:20 (post from 14-21)
--------------------------
public void connect(){
Chat c;
try{
socket = new Socket("server",4444);
}catch (Exception e) {System.err.println("test9
c = new Chat(nick,socket);
}
MultiServerThread.java:29 (from 10-30)
--------------------------
private String str=null;
public MultiServerThread(Socket socket) {//constructor
super("MultiServerThread")
this.socket = socket;
zendGegevens();
}
//public voegClientBij(Socket s,ObjectInputStream in,ObjectOutputStream out,String naam)
//{
// Client
// ((Server) servers.elementAt(i)).addC
//}
public void zendGegevens(){
try{
out = new ObjectOutputStream(socket.
in = new ObjectInputStream(socket.g
UserManagement um = new UserManagement();
um.ConnectieGegevens(socke
}catch(IOException e ){System.err.println("test
}
Can you please add a:
System.out.println("messag e is: " + message);
statement right after the following line:
message = leesData();
It should look like:
while (true) {
message = leesData();
System.out.println("messag e is: " + message);
aTextArea.append(message);
System.out.println(message );
}
Can you also post the code for this method: leesData();
System.out.println("messag
statement right after the following line:
message = leesData();
It should look like:
while (true) {
message = leesData();
System.out.println("messag
aTextArea.append(message);
System.out.println(message
}
Can you also post the code for this method: leesData();
And please let us know of the message you get after the: System.out.println("messag e is: " + message); is executed.
ASKER
leesData();
-----------
private String leesData(){
String s=null;
try{
in = new ObjectInputStream(socket.g etInputStr eam());
s = (String)in.readObject();
System.out.print(s);
}catch(Exception e){System.out.println("tes t3");
//removeSelf();
return null; }
return s;
}//einde leesData()
-------------------------- ---------- ---------- ---------- -
message i get after the: System.out.println("messag e is: " + message); is executed.
=>message is: null
-----------
private String leesData(){
String s=null;
try{
in = new ObjectInputStream(socket.g
s = (String)in.readObject();
System.out.print(s);
}catch(Exception e){System.out.println("tes
//removeSelf();
return null; }
return s;
}//einde leesData()
--------------------------
message i get after the: System.out.println("messag
=>message is: null
ASKER
hmmmm Strange
before i tried to make a communication between different clients this method worked without a problem...
before i tried to make a communication between different clients this method worked without a problem...
Ok we got it leaking somewhere... Can you also do a:
System.out.println("socket : " + socket + ", in: " + in + ", out: " + out);
just before the:
um.ConnectieGegevens(socke t,in,out);
line and let us know the result?
Also in the leesData() method I assume you are not getting the "test3" error message and therefore the exception is not thrown.
Before the:
in = new ObjectInputStream(socket.g etInputStr eam());
statement can you do a
System.out.println("socket : " + socket);
and also before the:
return s;
System.out.println("s is: " + s)
and let us know the result?
System.out.println("socket
just before the:
um.ConnectieGegevens(socke
line and let us know the result?
Also in the leesData() method I assume you are not getting the "test3" error message and therefore the exception is not thrown.
Before the:
in = new ObjectInputStream(socket.g
statement can you do a
System.out.println("socket
and also before the:
return s;
System.out.println("s is: " + s)
and let us know the result?
ASKER
Actually now you mention it ,i do get the "test3" error message ....
soo i assume i only should do the first thing
in server window
-----------------
socket: Socket[addr=/192.168.0.100 ,port=2802 ,localport =4444], in: java.io.Object
InputStream@119298d, out: java.io.ObjectOutputStream @f72617
Exception in thread "main" java.lang.NullPointerExcep tion
at UserManagement.addClient(U serManagem ent.java:5 0)
at UserManagement.ConnectieGe gevens(Use rManagemen t.java:44)
at MultiServerThread.zendGege vens(Multi ServerThre ad.java:30 )
at MultiServerThread.<init>(M ultiServer Thread.jav a:16)
at MultiServer.main(MultiServ er.java:18 )
Press any key to continue...
in client window
-----------------
Please type in your nick: lennert
Please type in the host address: server
Please type in the host port number: 4444
test3
message is: null
Exception in thread "main" java.lang.NullPointerExcep tion: null string
at sun.awt.windows.WTextAreaP eer.insert Text(Nativ e Method)
at java.awt.TextArea.insertTe xt(TextAre a.java:306 )
at java.awt.TextArea.appendTe xt(TextAre a.java:332 )
at java.awt.TextArea.append(T extArea.ja va:323)
at Chat.<init>(Chat.java:77)
at Client.connect(Client.java :20)
at Client.main(Client.java:40 )
soo i assume i only should do the first thing
in server window
-----------------
socket: Socket[addr=/192.168.0.100
InputStream@119298d, out: java.io.ObjectOutputStream
Exception in thread "main" java.lang.NullPointerExcep
at UserManagement.addClient(U
at UserManagement.ConnectieGe
at MultiServerThread.zendGege
at MultiServerThread.<init>(M
at MultiServer.main(MultiServ
Press any key to continue...
in client window
-----------------
Please type in your nick: lennert
Please type in the host address: server
Please type in the host port number: 4444
test3
message is: null
Exception in thread "main" java.lang.NullPointerExcep
at sun.awt.windows.WTextAreaP
at java.awt.TextArea.insertTe
at java.awt.TextArea.appendTe
at java.awt.TextArea.append(T
at Chat.<init>(Chat.java:77)
at Client.connect(Client.java
at Client.main(Client.java:40
ASKER
when i do it any way i get
Please type in your nick: test
Please type in the host address: server
Please type in the host port number: 4444
socket: Socket[addr=server/192.168 .0.100,por t=4444,loc alport=281 0]
test3
message is: null
Exception in thread "main" java.lang.NullPointerExcep tion: null string
at sun.awt.windows.WTextAreaP eer.insert Text(Nativ e Method)
at java.awt.TextArea.insertTe xt(TextAre a.java:306 )
at java.awt.TextArea.appendTe xt(TextAre a.java:332 )
at java.awt.TextArea.append(T extArea.ja va:323)
at Chat.<init>(Chat.java:77)
at Client.connect(Client.java :20)
at Client.main(Client.java:40 )
Please type in your nick: test
Please type in the host address: server
Please type in the host port number: 4444
socket: Socket[addr=server/192.168
test3
message is: null
Exception in thread "main" java.lang.NullPointerExcep
at sun.awt.windows.WTextAreaP
at java.awt.TextArea.insertTe
at java.awt.TextArea.appendTe
at java.awt.TextArea.append(T
at Chat.<init>(Chat.java:77)
at Client.connect(Client.java
at Client.main(Client.java:40
> Actually now you mention it ,i do get the "test3" error message ....
If you had told us this from the beginning now we would probably have solved your problem. :-)
Anyhow... Do a:
System.out.println("Except ion: " + e);
e.printStackTrace();
inside your:
catch(Exception e){System.out.println("tes t3");
//removeSelf();
return null;}
It shoudl look like this:
catch(Exception e)
{
System.out.println("test3" );
System.out.println("Except ion: " + e);
e.printStackTrace();
//removeSelf();
return null;
}
and tell me the exact error message you are getting.
If you had told us this from the beginning now we would probably have solved your problem. :-)
Anyhow... Do a:
System.out.println("Except
e.printStackTrace();
inside your:
catch(Exception e){System.out.println("tes
//removeSelf();
return null;}
It shoudl look like this:
catch(Exception e)
{
System.out.println("test3"
System.out.println("Except
e.printStackTrace();
//removeSelf();
return null;
}
and tell me the exact error message you are getting.
ASKER
Oops, i'm soo sorry man, you may call me stupid :)
client side
-----------
Please type in your nick: test
Please type in the host address: server
Please type in the host port number: 4444
socket: Socket[addr=server/192.168 .0.100,por t=4444,loc alport=283 1]
test3
Exception: java.net.SocketException: Connection reset
java.net.SocketException: Connection reset
at java.net.SocketInputStream .read(Sock etInputStr eam.java:1 68)
at java.net.SocketInputStream .read(Sock etInputStr eam.java:1 82)
at java.io.ObjectInputStream$ PeekInputS tream.peek (ObjectInp utStream.j ava
:2133)
at java.io.ObjectInputStream$ BlockDataI nputStream .peek(Obje ctInputStr eam
.java:2426)
at java.io.ObjectInputStream$ BlockDataI nputStream .peekByte( ObjectInpu tSt
ream.java:2436)
at java.io.ObjectInputStream. readObject 0(ObjectIn putStream. java:1245)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:324)
at Chat.leesData(Chat.java:10 7)
at Chat.<init>(Chat.java:75)
at Client.connect(Client.java :20)
at Client.main(Client.java:40 )
message is: null
Exception in thread "main" java.lang.NullPointerExcep tion: null string
at sun.awt.windows.WTextAreaP eer.insert Text(Nativ e Method)
at java.awt.TextArea.insertTe xt(TextAre a.java:306 )
at java.awt.TextArea.appendTe xt(TextAre a.java:332 )
at java.awt.TextArea.append(T extArea.ja va:323)
at Chat.<init>(Chat.java:77)
at Client.connect(Client.java :20)
at Client.main(Client.java:40 )
server side
-----------
socket: Socket[addr=/192.168.0.100 ,port=2831 ,localport =4444], in: java.io.Object
InputStream@119298d, out: java.io.ObjectOutputStream @f72617
Exception in thread "main" java.lang.NullPointerExcep tion
at UserManagement.addClient(U serManagem ent.java:5 0)
at UserManagement.ConnectieGe gevens(Use rManagemen t.java:44)
at MultiServerThread.zendGege vens(Multi ServerThre ad.java:30 )
at MultiServerThread.<init>(M ultiServer Thread.jav a:16)
at MultiServer.main(MultiServ er.java:18 )
Press any key to continue...
client side
-----------
Please type in your nick: test
Please type in the host address: server
Please type in the host port number: 4444
socket: Socket[addr=server/192.168
test3
Exception: java.net.SocketException: Connection reset
java.net.SocketException: Connection reset
at java.net.SocketInputStream
at java.net.SocketInputStream
at java.io.ObjectInputStream$
:2133)
at java.io.ObjectInputStream$
.java:2426)
at java.io.ObjectInputStream$
ream.java:2436)
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at Chat.leesData(Chat.java:10
at Chat.<init>(Chat.java:75)
at Client.connect(Client.java
at Client.main(Client.java:40
message is: null
Exception in thread "main" java.lang.NullPointerExcep
at sun.awt.windows.WTextAreaP
at java.awt.TextArea.insertTe
at java.awt.TextArea.appendTe
at java.awt.TextArea.append(T
at Chat.<init>(Chat.java:77)
at Client.connect(Client.java
at Client.main(Client.java:40
server side
-----------
socket: Socket[addr=/192.168.0.100
InputStream@119298d, out: java.io.ObjectOutputStream
Exception in thread "main" java.lang.NullPointerExcep
at UserManagement.addClient(U
at UserManagement.ConnectieGe
at MultiServerThread.zendGege
at MultiServerThread.<init>(M
at MultiServer.main(MultiServ
Press any key to continue...
> Chat.leesData(Chat.java:10 7)
Which exactly is the above line (107)?
Is it this:
n = new ObjectInputStream(socket.g etInputStr eam());
or this:
s = (String)in.readObject();
Which exactly is the above line (107)?
Is it this:
n = new ObjectInputStream(socket.g
or this:
s = (String)in.readObject();
ASKER
s = (String)in.readObject();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Meanwhile i totally rewrote my server on this example
=> http://www.acm.org/crossroads/xrds6-1/ovp61.html
and it works great now!!!
Girionis,Thank you very very very much for your help and quick responses :)
i learnt alot concerning error-messages thxs too you
I can't thank you enough m8
hopefully not until soon ;)
cu
=> http://www.acm.org/crossroads/xrds6-1/ovp61.html
and it works great now!!!
Girionis,Thank you very very very much for your help and quick responses :)
i learnt alot concerning error-messages thxs too you
I can't thank you enough m8
hopefully not until soon ;)
cu
ASKER
Meanwhile i totally rewrote my server on this example
=> http://www.acm.org/crossroads/xrds6-1/ovp61.html
and it works great now!!!
Girionis,Thank you very very very much for your help and quick responses :)
i learnt alot concerning error-messages thxs too you
I can't thank you enough m8
hopefully not until soon ;)
cu
=> http://www.acm.org/crossroads/xrds6-1/ovp61.html
and it works great now!!!
Girionis,Thank you very very very much for your help and quick responses :)
i learnt alot concerning error-messages thxs too you
I can't thank you enough m8
hopefully not until soon ;)
cu
ASKER
he really earned those points :)
Finally, I am glad it's solved :-)
Please if you feel I have helped you accept an answer otherwise ask a moderator (https://www.experts-exchange.com/Community_Support/) to delete the question and refund the points back to you.
Please if you feel I have helped you accept an answer otherwise ask a moderator (https://www.experts-exchange.com/Community_Support/) to delete the question and refund the points back to you.
Thank you for the points :-)