【发布时间】: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-ask、stackoverflow.com/help/dont-ask,尤其是stackoverflow.com/help/mcve,然后再在这里发布更多问题。祝你好运。