Loop the client for sending multiple requests to the server until reaching the max

Viewed 20

My client can send a single request to the server. I want to create some loads by looping the client 20 times, and having 5 clients running. I tried something based on this post, but it didn't work.

My Server

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Random;

public class MyServer {
    public static void main(String[] args) throws IOException {
        String jokes[] = {"j1", "j2", "j3"};
        ServerSocket socket = new ServerSocket(9000);
        while(true){
            Socket s = socket.accept();
            PrintWriter print = new PrintWriter(s.getOutputStream(), true);
            String ip = (InetAddress.getLocalHost().getHostAddress());
            print.println(ip+jokes[(int)(Math.random()*(jokes.length-1))]);
            s.close();
            print.close();
        }
    }
}

My Client

import java.io.*;
import java.net.*;
public class MyClient{
    public static void main(String[]args) throws IOException{
     System.out.println(args[0]);
     System.out.println(args[1]);
     Socket socket = new Socket(args[0], Integer.parseInt(args[1]));

     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     String message;

     while((message = in.readLine()) != null)
     {
         System.out.println(message);
         System.out.println(cnt);
     }
     socket.close();
     }
}

I'm not familiar with java networking programming, any suggestions would be great.

1 Answers

I looped the client 20 times and opened 5 terminals.

import java.io.*;
import java.net.*;
public class MyClient{
    public static void main(String[]args) throws IOException{
     System.out.println(args[0]);
     System.out.println(args[1]);
     int cnt = 0;
     while(cnt<20) {
     Socket socket = new Socket(args[0], Integer.parseInt(args[1]));

     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     String message;

     while((message = in.readLine()) != null)
     {
         System.out.println(message);
         System.out.println(cnt);
     }
     socket.close();
     }
   }
}

Related