Results 1 to 20 of 24
Thread: Java TCP help.
- 08-28-2011, 12:44 AM #1
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 08-28-2011, 12:59 AM #2
You need to allow multiple connects. The server should save the current connection some where and loop back to wait for another connect.but only the first person can connect, and the other person cannot.
- 08-28-2011, 01:41 AM #3
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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?
- 08-28-2011, 01:44 AM #4
By server I meant the code that uses the ServerSocket class.what do you mean by server?
- 08-28-2011, 01:53 AM #5
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
Ok! which would be in my try block that begins the connection.
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?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){ }
- 08-28-2011, 02:31 AM #6
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?
- 08-28-2011, 02:37 AM #7
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 08-28-2011, 02:44 AM #8
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?
- 08-28-2011, 02:52 AM #9
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 08-28-2011, 02:56 AM #10
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.
- 08-28-2011, 03:02 AM #11
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 08-28-2011, 03:10 AM #12
"save the connection"Java Code:socket = hostServer.accept()
The socket variable represents the connection to a client.
- 08-28-2011, 03:13 AM #13
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 08-28-2011, 03:14 AM #14
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
Should i use and array, and add the client to that array every time one is detected?
- 08-28-2011, 03:15 AM #15
Yes, you get a new socket value returned by the accept method.that socket variable specific to that connection?
- 08-28-2011, 03:16 AM #16
An array or an arraylist should work to hold the connection data.
- 08-28-2011, 03:26 AM #17
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 08-28-2011, 03:30 AM #18
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
- 08-28-2011, 03:34 AM #19
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
I want it to act as an open chat room, so client 1 to client 2 & 3.
- 08-28-2011, 03:40 AM #20
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks