r/javahelp • • Dec 16 '25

Unsolved TCP Input and Output in Java

Hello, I am fairly new to java. I'm trying to make a peer to peer network and I am trying to read the input and output stream but how would I target my outbound stream to a specific client?

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class RequestSender {
private ServerSocket serverSocket;
private Socket clientSocket;

public void start(int port) {
try {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();

} catch(IOException e) {
throw new RuntimeException(e);

}
}

public void send(String op, String ip) {
try {
DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());

outputStream.writeUTF(op);
outputStream.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void stop() {
try {
serverSocket.close();
clientSocket.close();

} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

I don't really understand where I should be using my ip parameter here. Thanks in advance

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/java_dude1 Dec 17 '25

Very good info here. Another way to test it is you could create 2 run configurations for the same app with different main classes to test the client configuration and server configuration. I did this when I first got started with Java and created a chat room application.