1
votes

I have to open plenty of TCP connections to a SIP server which is runnning on linux. I tried with one simple client program in Java, but I could not open even 350 connections from another linux server. I want to open ~ 50 thousand and above for a load/performance test.

Is there any way to overcome this? What are the limitations? Sorry if this is a silly question,I am a Beginner.

Thanks

client program

public class ConnectionTcp
{
static int noOfconnected;
Socket socket;
static int port=1000;
static Object obj=new Object();
static AtomicInteger atomicInteger;
public static void main(String[]args)
{
try{
ConnectionTcp con=new ConnectionTcp();
atomicInteger = new AtomicInteger();
Date date = new Date();
for(int i=0;i<50000;i++)
{
port+=i;    

con.sendmsg();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public  synchronized  void sendmsg(){
        try{
        Thread.sleep(100);  
        }
        catch(Exception e){
        System.out.println(e);
        }    
        Runnable r=new Runnable(){
            public void run(){
                try{
                    boolean check=true;
                    InetAddress ip=InetAddress.getByName("131.10.20.16");  
                    Socket socket=new Socket("131.10.20.17",5060,ip,port);              
                    System.out.println("conected is "+socket.isConnected()+"<----------with port----------->"+socket.getLocalPort());


                    OutputStream out =socket.getOutputStream();
                    InputStream in =socket.getInputStream();
                        String str = "keep alive";
                        byte[] array = str.getBytes();          
                    System.out.println("no of user connected with server is "+atomicInteger.incrementAndGet());
                    while(true){                    
                        try{
                            int i = in.read();
                            out.write(array);                           
                        }catch(Exception e){
                            System.out.println("exception"+e);
                            atomicInteger.decrementAndGet();
                            socket.close();
                            Date date = new Date();    
                             System.out.println("Ented Time  is "+date.toString());
                            break;
                        }
                    }
                }catch(Exception e1){   
                System.out.println("main exception"+e1);
                atomicInteger.decrementAndGet();
                }
            }
        };
        (new Thread(r,"tcp")).start();        
    }
}
3
Is 350 connection attempt gave you any error? - RamPrakash
Exception was "exceptionjava.lang.IllegalArgumentException: port out of range",server port range is 15000 to 61000.why cant assigned source port one by one like 1000,1001,1002.... instead of 1000,2002,4005,......, i dont think all the port are busy, cuz i checked with ss -s command on linux,it shows only 33 tcp connection - jagadeesh k
Please supply sourcecode. are you using threads? what are your jvm arguments, etc. Also please give a little efford into your question. Else you won't get any help. - Gewure
i added source code on question - jagadeesh k
Please take the tour , then read stackoverflow.com/help/how-to-ask , stackoverflow.com/help/dont-ask , and especially stackoverflow.com/help/mcve before posting more Qs here. Good luck. - shellter

3 Answers

1
votes

You can only use ports above 1023. The lower numbers are reserved.

0
votes

You don't need to specify either the local IP address or the local port. The operating system will do that for you. If you run out of ports, so be it: all that means is that your test isn't realistic. No single actual client is going to exhaust the local port space. Don't test things that aren't part of the problem space.

0
votes

It is a programming logic problem. Accessing the port value inside the run method is not meant, first thread gets port as 1000 and the next one as 1001. It can be succeeded incrementing the port value with incrementAndGet() to bind the port sequentially.

 public class ConnectionTcp
{
static AtomicInteger atomicInteger;
public static void main(String[]args)
{
ConnectionTcp con=new ConnectionTcp();
atomicInteger_=new AtomicInteger(1000);
for(int i=0;i<5000;i++)
{
con.sendmsg();
}
}
public  synchronized void sendmsg(){    
        Runnable r=new Runnable(){
            public void run(){
                try{                    
                    InetAddress ip=InetAddress.getByName("192.168.1.22");                   
                    int port = atomicInteger.incrementAndGet();                 
                    Socket socket=new Socket("131.10.20.17",5060,ip,port);                  
                    OutputStream out =socket.getOutputStream();
                    InputStream in =socket.getInputStream();
                        String str = "keep alive";
                        byte[] array = str.getBytes();                      
                    while(true){                    
                        try{
                            int i = in.read();
                            out.write(array);                           
                        }catch(Exception e){
                            System.out.println("exception"+e);                              
                            socket.close();                     
                            break;
                        }
                    }
                }catch(Exception e1){   
                System.out.println("main exception"+e1);                    
                }
            }
        };
        (new Thread(r,"tcp")).start();        
    }
}