duh ikal
duh ikal

Reputation: 162

Why does server process multiple clients at the same time when it's not multithreaded?

So in my code below i have a singlethreaded server and a multithreaded client. The reason for this is because i want the client to send packets and receive packets simultaneously. However when i start the server and run multiple clients the server can process multiple clients simultaneously even tough the server is not multithreaded? Can you explain this?

Server:

public class server {

public static void main(String[] args) throws IOException{
    new server();
}


//declare the UDP server socket
DatagramSocket datagramSocket;
int port = 3741;

public server() throws IOException {
    
    //create UDP server with a specific port number
    datagramSocket = new DatagramSocket(port);
    System.out.println("[UDP server] Datagram socket started on port " + port);
    
    //prepare the packet structure for received packets
    int dataLength = 100; //must be large enough so some part of packet doesn't get lost
    byte[] receiveData = new byte[dataLength];
    DatagramPacket packet = new DatagramPacket(receiveData, receiveData.length);
    
while(true) {
        datagramSocket.receive(packet);
        System.out.println("client connected");
        
        InetAddress inetAddress = packet.getAddress();
        int clientPort = packet.getPort();
        byte[] data = packet.getData();

        
        
        //send response back to the client host
        byte[] sendData = new byte[1024];
        
        DatagramPacket datagramPacket = new DatagramPacket(data, data.length, inetAddress, clientPort);
        datagramSocket.send(datagramPacket); //sending data from server to client
        
}
}

}

Client:

public class client {

public static void main(String[] args) throws Exception {
    new client();
}


public client() throws Exception{
    
    DatagramSocket socket = new DatagramSocket();
    
    InetAddress ip = InetAddress.getByName("127.0.0.1");
    String message = "hello from client"; 
    
    DatagramPacket packetSend = new DatagramPacket(message.getBytes(), message.getBytes().length, ip, 3741);
    
    Thread th = new Thread(new Runnable() {
        
        @Override
        public void run() {
            for(int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                try {
                    socket.send(packetSend);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
    });
    th.start();
    
    byte[] buffer = new byte[1024];
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    
    //this part can't be in a thread because the loop above us will finish first before this starts
    //we can put this code before the loop and start a thread this would also work
    while(true) {
        try {
            socket.receive(packet);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        String data = new String(packet.getData()).trim();
        
        System.out.println(data);
    }
}

}

Upvotes: 1

Views: 113

Answers (1)

tyChen
tyChen

Reputation: 1494

You think "the server can process multiple clients simultaneously even tough the server is not multithreaded" cause your brain cheat you. Acutually they are processing one by one in a reliatively fast speed. It's easy to see if you use wireshark or tcpdump to capture the server side packets. You will find the interesing truth.

Upvotes: 2

Related Questions