【问题标题】:How to open many TCP connections in Java如何在 Java 中打开多个 TCP 连接
【发布时间】:2017-05-22 03:37:27
【问题描述】:

我必须打开大量 TCP 连接到在 linux 上运行的 SIP 服务器。我尝试使用 Java 中的一个简单客户端程序,但我什至无法从另一台 linux 服务器打开 350 个连接。我想开~50,000及以上进行负载/性能测试。

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

谢谢

客户端程序

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

【问题讨论】:

  • 350 连接尝试是否给您任何错误?
  • 异常是“exceptionjava.lang.IllegalArgumentException:端口超出范围”,服务器端口范围是15000到61000。为什么不能像1000,1001,1002一样一一分配源端口...... 1000,2002,4005,......,我不认为所有端口都忙,因为我在 linux 上用 ss -s 命令检查,它只显示 33 tcp 连接
  • 请提供源代码。你在使用线程吗?您的 jvm 参数是什么等。另外,请为您的问题付出一点努力。否则你将得不到任何帮助。
  • 我在问题上添加了源代码
  • 请阅读tour,然后阅读stackoverflow.com/help/how-to-askstackoverflow.com/help/dont-ask,尤其是stackoverflow.com/help/mcve,然后再在这里发布更多问题。祝你好运。

标签: java linux tcp sip


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您无需指定本地 IP 地址或本地端口。操作系统会为您做到这一点。如果你的端口用完了,那就这样吧:这意味着你的测试是不现实的。没有单个实际客户端会耗尽本地端口空间。不要测试不属于问题空间的东西。

    【讨论】:

      【解决方案3】:

      这是一个编程逻辑问题。访问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();        
          }
      }
      

      【讨论】:

      • 不,这不是唯一的方法。您可以将其声明为volatile,或者通过synchronized 方法或块获取和设置它。
      • 是的,你是对的。我必须非常小心地使用每一个字。抱歉
      • 根据@JonathanRosenne,您只能使用端口高于 1023,保留较低的端口!还要确保您没有其他服务使用您打开的端口,无论出于何种原因(我真的不明白为什么)。一开始我会限制在 100 个端口上。
      • 好的,谢谢,实际上我有一个程序逻辑问题。我通过使用 AtomicInteger 类的 incrementAndGet() 解决了它
      猜你喜欢
      • 2014-07-09
      • 2013-03-15
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 2011-01-09
      相关资源
      最近更新 更多