So I have began with doing a turn based rpg game, which going to have multiplayer, but I have run into a problem. I don’t know how to send data to a socket without closing it, sure I could make the clients re-connect to the ServerSocket to receive new data, but that seems wrong to me, or is it?
Some code I tested with
public void sendMessage(Socket socket, String message){
try{
OutputStream out = socket.getOutputStream();
out.write(message.getBytes());
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void sendMessage(String message){
for(Socket s : players)
sendMessage(s, message);
}
public class Test {
public static void main(String[] args) throws Exception{
Server server = new Server(1233);;
Client client = new Client("127.0.0.1", 1233);
server.sendMessage("Data 1");
server.sendMessage("Data 2");
Thread.sleep(1000);
System.exit(0);
}
}