TCP socket communication
- by raven
hello,
I am creating a Chat in java.
I have a method (onMouseRelease) inside an object that creates a tcp server and waits for a socket like this:
ServerSocket server = new ServerSocket(port);
Socket channel = server.accept();
Now I want to make a thread that will loop and read data from the socket, so that once the user on the other side sends me a string, I will extract the data from the socket (or is it called packet? Sorry, I am new to this) and update a textbox to add the additional string from the socket (or packet?).
I have no idea how to READ (extract) the information from the socket(/packet) and then update it into a JTextArea which is called userOutput. And how to send a string to the other client, so that it will also could read the new data and update its JTextArea.
From what I know, for a 2 sided TCP communication you need one computer to host a server
and the other to connect (as a client) and once the connection is set the client can also receive new information from the socket. Is that true? and please tell me how.
Any help is appreciated! I know this is a bit long but I have searched a lot and didn't understand it (I saw something like PrintWriter but failed to understand).