简体   繁体   English

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

[英]How to open many TCP connections in Java

I have to open plenty of TCP connections to a SIP server which is runnning on linux.我必须打开大量 TCP 连接到在 linux 上运行的 SIP 服务器。 I tried with one simple client program in Java, but I could not open even 350 connections from another linux server.我尝试用 Java 编写一个简单的客户端程序,但我什至无法打开来自另一台 linux 服务器的 350 个连接。 I want to open ~ 50 thousand and above for a load/performance test.我想打开 ~ 50,000 及以上进行负载/性能测试。

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

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

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

You don't need to specify either the local IP address or the local port.您不需要指定本地 IP 地址或本地端口。 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.不要测试不属于问题空间的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM