I have a server-side socket running, with the aim for the socket to constantly accept new inputs from multiple different clients and the client can send an infinite number of requests. However, when I run the server and client, the server only seems to print only a single input from the client, even though the client is sending a new input every second.
What do I need to modify in order for the server to:
- serve multiple clients at once
- Receive and print out an infinite number of inputs from each client
Code:
The server socket thread:
public void run() {
try {
System.out.println("Binding socket..");
ServerSocket serverSocket = new ServerSocket(portNumber);
// THE MAIN READING LOOP:
while(true){
Socket socket = serverSocket.accept();
System.out.println("Socket is listening..");
DataInputStream dis = new DataInputStream(socket.getInputStream());
//here, the code is meant to read the inputs made in the last 2 seconds as a single block (I'm making a game)
if(tickHappened){
DebugLogger.print("Tick happened.");
String undividedInputs = dis.readUTF();
String[] inputs = undividedInputs.split("/");
DebugLogger.print("Messages:");
for(int i =0; i < inputs.length;i++){
System.out.println(inputs[i]);
}
tickHappened = false;
}
}
} catch (IOException e) {
System.err.println(e);
throw new RuntimeException(e);
}
}
The client side, which sends inputs every second. The sendMessageToServer method is called:
package Graphics;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ClientToServerCommunications {
private static Socket s;
static {
try {
s = new Socket("localhost",6666);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
;
private static DataOutputStream dout;
static {
try {
dout = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
;
public static void sendMessageToServer(String message) throws InterruptedException {
while (true) {
Thread.sleep(1000);
try {
dout.writeUTF(message);
} catch (Exception e) {
System.out.println(e);
}
}
}
}