Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Java TCP help.

  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default Java TCP help.

    Hello! It has been a long time since i have been here last. Recently i made a chat/IM program, to chat across a local network between 2 people. Until now, it has been working ok, until i wanted to add another person. I though i could just set one person as the host, and the other 2 to connect to it, but only the first person can connect, and the other person cannot. Any user enters an ip (in my testing localhost) a port (1234) and the program writes to a socket, and reads from it as well, then takes the read data and prints it to a JTextArea.

    So, basically, i want to make the program more of a chat room, then a 2 way connection.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    but only the first person can connect, and the other person cannot.
    You need to allow multiple connects. The server should save the current connection some where and loop back to wait for another connect.

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Hmm, perhaps, use another thread to wait for, and make connections, then the current thread to read from and write to the socket? Maybe i don't quite understand what REALLY happens when we read from/write to a socket. And what do you mean by server? the host Machine is the server, correct? or is the socket the server?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    what do you mean by server?
    By server I meant the code that uses the ServerSocket class.

  5. #5
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok! which would be in my try block that begins the connection.
    Java Code:
     try{
                  if(Host == true){
                      hostServer = new ServerSocket(port);
                      socket = hostServer.accept();
                  }else{
                      socket = new Socket(hostIP, port); 
                  }
                   in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                   out = new PrintWriter(socket.getOutputStream(), 
    
     }catch(IOException e){
    
                }
    So, this is what creates the connection. If i understand what you mean, the implementation would apply to the host, where the guest part of the code is still ok? basically, i need to use a thread to keep running the try block, and another one that sends/receives the data?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    How do you decide which PC is to be the server/Host? Do all of the clients have the IP addresses of all the other clients?

  7. #7
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    How do you decide which PC is to be the server/Host? Do all of the clients have the IP addresses of all the other clients?
    In the program, you have a choice to be the host, or the guest. And No, even though i am using localhost, normally, there would be an IP adress specified, so for example, person 1 is host, with ip 192.168.1.1 and port 1234, person 2 would select guest, then, type in IP 192.168.1.1, and port 1234. If person 3 wanted to join, he would type in 192.168.1.1, and port 1234, but would have no idea oh person 2's IP.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    This is not clear to me yet.
    How do you decide which PC is to be the server/Host? Do you call up on the phone and ask all possible client's if they want to be the host?
    How does a client know which PC (IP address) is the server/Host?

  9. #9
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    The flow of the the program is a little choppy. Yes, everyone can be a host, its made to be on a local network at my school, there we can communicate across the access point in each room. Where the client, knows the host via a quick shout across the room.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    So everyone knows the host/server's IP address and can connect to it.
    The server code needs to have a loop where it waits for connections and saves the connection so it can communicate with each of the clients and loops back to await the next connection.
    You will need threads so the "client" on the same PC as the server code can join the conversation while the server code waits for the next connection.

  11. #11
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok, so now the logic is laid out. I have set up a new thread that loops, that much was easy. Idk what you mean by "save the connection". I guess i dont fully understand the meaning of some of the net code. ill pay a visit to java docs.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Java Code:
    socket = hostServer.accept()
    "save the connection"
    The socket variable represents the connection to a client.

  13. #13
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    But is that socket variable specific to that connection? i understood it from this book as a connection to the port, and requests attempting be accepted to that port.

  14. #14
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Should i use and array, and add the client to that array every time one is detected?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    that socket variable specific to that connection?
    Yes, you get a new socket value returned by the accept method.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    An array or an arraylist should work to hold the connection data.

  17. #17
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    ok! so in a separate thread, i run a while loop when you are host, then in that while loop, simply add and element to an array and set it equal to hostServer.accept(); ... How would i wait? wouldn't i get an error if i tried to do that and there was not a connection to be made? or would it simple wait until there is a connection to fill the variable? If you are curious, i am testing theses, but it takes a min to write the code so im asking questions i may have found the answer too by the time you respond.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    the accept method returns a socket. You need to save that to use to communicate with the clients.
    The accept method does the waiting for the next connection.

    Does the server broadcast client messages to all clients and/or can messages be private: client 1 to client 3 only

  19. #19
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    I want it to act as an open chat room, so client 1 to client 2 & 3.

  20. #20
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

Page 1 of 2 12 LastLast

Posting Permissions

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