?
Solved

server client communication

Posted on 2003-02-26
26
Medium Priority
?
268 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:SupremeBeing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 12
26 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8024450
 Post your exception errors here. By doing this other people with similar errors might be helped as well.
0
 

Author Comment

by:SupremeBeing
ID: 8024474
in client window
----------------
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java: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.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:29)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.java:18)

0
 

Author Comment

by:SupremeBeing
ID: 8024477
in client window
----------------
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java: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.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:29)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.java:18)

0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 35

Expert Comment

by:girionis
ID: 8024537
 The object you are trying to pass is null. Are you sure you have initialized it and assigned a non-null value?
0
 

Author Comment

by:SupremeBeing
ID: 8024688
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.getOutputStream());
                   out.writeObject(data);
              }catch(IOException e ){System.out.println("test2");
                   return false;     }

             return true;
        }
0
 
LVL 35

Expert Comment

by:girionis
ID: 8024762
> 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)
0
 

Author Comment

by:SupremeBeing
ID: 8025005
Think this would be easier
=>
You can find the code on this hotmail-adres: chat_probs@hotmail.com
password:
expert

no?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8025256
 I mentioned it earlier. Other people might have similar problems. Better post the code here.
0
 

Author Comment

by:SupremeBeing
ID: 8025307
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(aDesktopPane);
          aFrame.setSize(750,500);
          aFrame.setVisible(true);
          aFrame.setDefaultCloseOperation(JFrame.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("test99");System.out.println(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)).addChatter(connection,input,output,name,info);
     //}    
     public void zendGegevens(){
          try{
          out = new ObjectOutputStream(socket.getOutputStream());
          in = new ObjectInputStream(socket.getInputStream());
          UserManagement um = new UserManagement();
          um.ConnectieGegevens(socket,in,out);
          }catch(IOException e ){System.err.println("test4"); System.out.println("test4");}          
     }    
0
 
LVL 35

Expert Comment

by:girionis
ID: 8025356
 Can you please add a:

System.out.println("message is: " + message);

  statement right after the following line:

  message = leesData();

  It should look like:

 while (true) {
               message = leesData();
               System.out.println("message is: " + message);
               aTextArea.append(message);
               System.out.println(message);
               }

  Can you also post the code for this method: leesData();
0
 
LVL 35

Expert Comment

by:girionis
ID: 8025365
 And please let us know of the message you get after the: System.out.println("message is: " + message); is executed.
0
 

Author Comment

by:SupremeBeing
ID: 8025427
leesData();
-----------

private String leesData(){
           String s=null;
        try{
                  in = new ObjectInputStream(socket.getInputStream());
                     s = (String)in.readObject();
                  System.out.print(s);
             }catch(Exception e){System.out.println("test3");
                 //removeSelf();
                return null;     }

        return s;
        }//einde leesData()
---------------------------------------------------------
message i get after the: System.out.println("message is: " + message); is executed.

=>message is: null
0
 

Author Comment

by:SupremeBeing
ID: 8025444
hmmmm Strange

before i tried to make a communication between different clients this method worked without a problem...
0
 
LVL 35

Expert Comment

by:girionis
ID: 8025482
 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(socket,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.getInputStream());

  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?
0
 

Author Comment

by:SupremeBeing
ID: 8025745
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.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:30)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.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.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java:323)
        at Chat.<init>(Chat.java:77)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)
0
 

Author Comment

by:SupremeBeing
ID: 8025779
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,port=4444,localport=2810]
test3
message is: null
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java:323)
        at Chat.<init>(Chat.java:77)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)

0
 
LVL 35

Expert Comment

by:girionis
ID: 8025788
> 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("Exception: " + e);
  e.printStackTrace();

  inside your:

catch(Exception e){System.out.println("test3");
                //removeSelf();
               return null;}

  It shoudl look like this:

catch(Exception e)
{
  System.out.println("test3");
  System.out.println("Exception: " + e);
  e.printStackTrace();
  //removeSelf();
  return null;
}

  and tell me the exact error message you are getting.
0
 

Author Comment

by:SupremeBeing
ID: 8025880
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,port=4444,localport=2831]
test3
Exception: java.net.SocketException: Connection reset
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at java.net.SocketInputStream.read(SocketInputStream.java:182)
        at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java
:2133)
        at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream
.java:2426)
        at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputSt
ream.java:2436)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1245)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
        at Chat.leesData(Chat.java:107)
        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.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java: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.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:30)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.java:18)
Press any key to continue...
0
 
LVL 35

Expert Comment

by:girionis
ID: 8026028
> Chat.leesData(Chat.java:107)

  Which exactly is the above line (107)?

  Is it this:

  n = new ObjectInputStream(socket.getInputStream());

  or this:

  s = (String)in.readObject();
0
 

Author Comment

by:SupremeBeing
ID: 8026074
s = (String)in.readObject();
0
 
LVL 35

Accepted Solution

by:
girionis earned 340 total points
ID: 8026210
 The TCP/IP layer resets the connection for some reason, while you are trying to read the data. This is a problem on your server, since it's the server that closes the connection. Check the files: UserManagement.java (line 50) and MultiServerThread.java (line 30). See if something unexpected is going on since you migth be closing conenctions too early.
0
 

Author Comment

by:SupremeBeing
ID: 8026264
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
0
 

Author Comment

by:SupremeBeing
ID: 8026270
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
0
 

Author Comment

by:SupremeBeing
ID: 8026284
he really earned those points :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 8026304
 Finally, I am glad it's solved :-)

  Please if you feel I have helped you accept an answer otherwise ask a moderator (http://www.experts-exchange.com/Community_Support/) to delete the question and refund the points back to you.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8026312
 Thank you for the points :-)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question