Results 1 to 10 of 10
  1. #1
    ilumar589 is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default A simple TCP Chat-socket problem I think

    Hello everyone! I'm new to the forum and am hoping to get some help. As the title says I'm trying to make a client-server chat program. I'm using some code of the internet with slight modifications because it's my first time trying something like this. The "borrowed" code works but when I adapt it the client doesn't connect for some reason. For the record the GUI and serialization method are of my own doing so I took some liberties to tie the code to the interface. Anyway I've been making my eyes bleed trying to see where the problem lies. I would be really gratefull for some help!
    Without further ado i'll archive my .java files and upload them. The ClientGUI and ServerGUI have the static main procedure so that's where the client/server program launches from.
    Attached Files Attached Files

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: A simple TCP Chat-socket problem I think

    In the catch { ... } parts of your code add an e.printStackTrace(); call and see if any exceptions were thrown.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ilumar589 is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: A simple TCP Chat-socket problem I think

    Thanks for the answer JosAH. It was a silly thing, I had the default text for server address set to localhoast instead of localhost. The client connects to the server now, doesn't seem to send messages for some reason, haven't gotten around to debuggin that problem yet. I'll post the full program if I get it working anytime soon and someone is interested.

  4. #4
    ilumar589 is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: A simple TCP Chat-socket problem I think

    Well it seems I can't figure it out after all, I don't know if the messages don't reach the server or if the server doesn't send them back... It's my first time working with threads and serialization in this manner and I get a feeling I screwed it up. If anyone one here has the time and wilingness to debug my code and explain to me what I'm doing wrong I would be grateful!
    !!!Important fact for anyone willing to help!!! : In ClientGUI.java, code line 34: "serverAddressField = new TextField("localhoast");" localhoast needs to be modified to localhost so the client connects. Anyway here's hoping someone has the time! Cheers!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: A simple TCP Chat-socket problem I think

    Do you flush the output streams/writers after sending a message to the other party?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    ilumar589 is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: A simple TCP Chat-socket problem I think

    (embarrassed) I thought DataOutputStream and BufferedOutputStream autoflush(yeah new to java). I added this to my code:

    In MessageData.java:

    Java Code:
     
    @Override
        public void exportData(DataOutputStream dos) throws IOException
        {
            dos.writeUTF(message);
            dos.writeInt(type);
            dos.flush(); // flush added
            
        }
    In Client.java:

    Java Code:
    public void sendMessage(MessageData msg)
        {
            try
            {
                msg.exportData(socketOutput);
                socketOutput.flush(); //flush added
            }
            catch(IOException e)
            {
                clientInterface.generalChat.append("Scrierea pe server nu s-a efectuat");
            }
        }
    and :

    Java Code:
    //this is from the public boolean startClient() method
    ..........
    ..........
    try
            {
                socketOutput.writeUTF(nickname);
                socketOutput.flush(); //flush added
            }
            catch(IOException e)
            {
                disconnect();
                return false;
            }
    and finally in Server.java:

    Java Code:
    private boolean writeMsg(String msg)
            {
                if(!clientInstanceSocket.isConnected())
                {
                    close();
                    return false;
                }
                try
                {
                    socketOutput.writeUTF(msg);
                    socketOutput.flush(); //flush added
                }
                catch(IOException e)
                {
                    
                }
                return true;
            }

    It still doesn't work so I'm doing something wrong. Can you explain to me how the buffer works in the background for the DataOutputStream and BufferedOutputStream classes. Do I have to flush both DataOutputStream and BufferedOutputStream when wrapping like this new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()); or should I just use new DataOutputStream(socket.getOutputStream()); ? Is it the same thing ? And finally does the flush I use in my exportData(DataOutputStream) method in the MessageData class hinder things or is that the way to do it in corelation with my other flushes ? Thanks in advance for the reply!

  7. #7
    ilumar589 is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: A simple TCP Chat-socket problem I think

    Eh, I'm asking silly questions, I'll just read some information on streams from another source, the book I was learning from has some awful examples. Thx anyway JosAH!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: A simple TCP Chat-socket problem I think

    You only have to flush() the outer Stream; it calls flush() on the stream that it wraps; I noticed that you're still swallowing exceptions; put in e.printStackTrace() in the catch-clauses so you can see what is happening.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    ilumar589 is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: A simple TCP Chat-socket problem I think

    Oh, that's just because I modified the MessageData class to use the java serializable interface and tried sending messages that way but all it did was freeze my client interface. I copied my old code back and it didn't have printStackTrace() and I forgot to add it to the previous post, but I did try the printStackTrace() method in my catches and nothing happened , no exceptions were thrown. Maybe I missed one or...am I supposed to look somewhere else beside the console for the printStackTrace() log ? It's my first time using it.

  10. #10
    ilumar589 is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: A simple TCP Chat-socket problem I think

    Got it to work thanks to JosAH's printStackTrace() advice. I'd forgotten two major things, one was allocating space for my MessageData object inside the Server.java before importing the serialized data, and the other one was starting my ArrayList for client threads from and out of bounds index, I'm a dumbass aparently. Anyway thanks again JosAH for the advice! I'll post the full program as a refrence for anyone interested when I finish it!

Similar Threads

  1. TCP socket - chat server
    By Gatsu in forum Networking
    Replies: 1
    Last Post: 03-01-2013, 02:50 PM
  2. Simple Chat Server socket coding issues
    By TheGadgetCat in forum Networking
    Replies: 14
    Last Post: 12-17-2010, 12:54 PM
  3. Replies: 3
    Last Post: 09-22-2010, 07:28 PM
  4. Simple Socket Problem
    By barusk in forum Networking
    Replies: 0
    Last Post: 12-14-2009, 04:43 PM
  5. Simple Socket IOstream problem
    By Atriamax in forum Networking
    Replies: 3
    Last Post: 12-01-2009, 08:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •