如何在 Java 中打开多个 TCP 连接

How to open many TCP connections in Java

我必须打开大量到 linux 上运行的 SIP 服务器的 TCP 连接。我尝试在 Java 中使用一个简单的客户端程序,但我什至无法从另一台 linux 服务器打开 350 个连接。我要开~5万及以上做个load/performance测试

有什么办法可以克服这个问题吗?有什么限制? 对不起,如果这是一个愚蠢的问题,我是初学者。

谢谢

客户端程序

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();        
    }
}

您只能使用 1023 以上的端口。较低的数字已保留。

这是一个编程逻辑问题。在 run 方法中访问端口值并不意味着,第一个线程获取端口为 1000,下一个线程获取端口为 1001。可以成功地使用 incrementAndGet() 递增端口值以顺序绑定端口。

 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();        
    }
}

您不需要指定本地 IP 地址或本地端口。操作系统会为你做这件事。如果您 运行 端口不足,那就这样吧:这意味着您的测试不切实际。没有一个实际的客户端会耗尽本地端口 space。不要测试不属于问题的东西 space.