Results 1 to 1 of 1
  1. #1
    Lokin is offline Member
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Default Chat ObjectOutput/InputStreams with threading and sockets

    Hi,
    Im pretty new with Java, and I'm trying to develop a TCP chat with a server, and clients who can connect to it.
    I started out using Scanner and PrintWriter, but since its not the way I wanted it to be, I notice that using objects and implementing Serializable is much more precise in transferring data.

    EDIT
    I managed to solve half of the problem, by implementing write and read Unshared.
    But now I have some other issue.

    How to sort the incoming data to the server to send it to a proper place.

    So this is my server Thread run loop, and when the ObjectInputStream reads some data, how can I "read" it so it knows to what if statement to go to.
    I was here experimenting with the Scanner, so you can ignore that part.

    Java Code:
    public void run() {
    
            try {
                while (true) {
                    Thread.sleep(5);
    
                    System.out.println("test_while");
                    System.out.println(serverTCC_apl.UserList);
                    
                    Scanner temp= new Scanner(INPUT_DATA);
                   
                    
                    if (temp.equals(CONNECT_DATA)) {
                        System.out.println("Client: " + SOCKET.getInetAddress().getHostAddress() + " has connected.");
                        CONNECT_DATA = new ConnectData();
                        CONNECT_DATA = (ConnectData) INPUT_DATA.readUnshared();
                        incoming_connect_data();
    
                    } else if (temp.equals(DISCONNECT_DATA)) {
                        System.out.println("Client: " + SOCKET.getInetAddress().getHostAddress() + " has disconnected.");
                        DISCONNECT_DATA = new DisconnectData();
                        DISCONNECT_DATA = (DisconnectData) INPUT_DATA.readUnshared();
                        incoming_disconnect_data();
    
                    }
                    if (temp.equals(MESSAGE_DATA)) {
                        System.out.println("Client: " + SOCKET.getInetAddress().getHostAddress() + " is sending a public message.");
                        MESSAGE_DATA = new MessageData();
                        MESSAGE_DATA = (MessageData) INPUT_DATA.readUnshared();
                        incoming_message_data();
    
                    }
                    update_online_users_to_client();
    
                } //end of while true 
    
            }//End of try
            catch (IOException | ClassNotFoundException X) {
                System.out.println("ERROR RECIVING DATA" + X);
            } catch (Exception e) {
                System.out.println("ERROR RECIVING DATA" + e);
            }
    
        } //end of run
    Last edited by Lokin; 03-16-2014 at 10:49 PM. Reason: Solved half of the problem

Similar Threads

  1. TCP ObjectOutput Steam
    By alexanderschill in forum Networking
    Replies: 1
    Last Post: 12-30-2012, 11:12 PM
  2. I'm having trouble with inputstreams.
    By Zeronitic in forum New To Java
    Replies: 4
    Last Post: 02-02-2012, 11:14 AM
  3. Chat, swing & sockets
    By tnrh1 in forum New To Java
    Replies: 2
    Last Post: 01-13-2012, 05:06 AM
  4. Replies: 3
    Last Post: 09-22-2010, 07:28 PM
  5. voice chat in java with sockets
    By krati in forum Networking
    Replies: 0
    Last Post: 05-12-2010, 09: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
  •