简体   繁体   English

阻止正在打开/正在使用的Java套接字

[英]Blocking a java socket that is opening/in use

Good Day, 美好的一天,

I have a sendMessage function in java that looks like this. 我在java中有一个sendMessage函数,看起来像这样。 It exists currently as a static method that can be called by any thread operation (such as a HTTP request). 它当前作为静态方法存在,可以由任何线程操作(例如HTTP请求)调用。 However, if I was to send two requests at the same time..one of the sendMessage requests will throw a "Connection reset". 但是,如果我要同时发送两个请求,则其中一个sendMessage请求将引发“连接重置”。

I need someway to know that my current port and ip is in use! 我需要知道我当前的端口和IP正在使用中! So that I do not reset the connection, and instead, wait for it to become available again. 这样我就不会重置连接,而是等待它再次可用。 Is there a way? 有办法吗?

private static String sendMessage(int command,String data,int port,String ip){
    try{
        String sendString=Integer.toString(command)+":"+data+'$'+'\n';
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        Socket clientSocket = new Socket();
        InetAddress addr = InetAddress.getByName(ip);
        SocketAddress sockaddr = new InetSocketAddress(addr, port);
        clientSocket.connect(sockaddr, 20000);

        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        outToServer.writeBytes(sendString);
        String rcvString = inFromServer.readLine();
        System.out.println("FROM DEVICE: " + rcvString);
        clientSocket.close();
        return rcvString;
    }catch(Exception e){
        System.err.println(e.getMessage());
    }

    return NULL;
}

Okay I just thought of an ingenious solution but it's ugly. 好吧,我只是想到了一个巧妙的解决方案,但这很丑陋。

Have a blockChecker class 有一个blockChecker类

public class BlockerCheck {

List<String> blockerList;

public BlockerCheck(){
    blockerList=new ArrayList<String>();
}

public synchronized void addBlock(String ip,int port){
    String finalString=ip+":"+Integer.toString(port);
    blockerList.add(finalString);
}

public synchronized void removeBlock(String ip,int port){
    String finalString=ip+":"+Integer.toString(port);
    blockerList.remove(finalString);
}

public synchronized boolean isAvailable(String ip,int port){
    String finalString=ip+":"+Integer.toString(port);
    System.out.println("SIZE:" +blockerList.size());
    return !(blockerList.contains(finalString));
}
}

Then use it as such: 然后按如下方式使用它:

private static String sendMessage(int command,String data,int port,String ip){
    if(blockerCheck.isAvailable(ip, port)) blockerCheck.addBlock(ip, port);
    else{
        long startTime=TimeUnit.SECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
        while(!(blockerCheck.isAvailable(ip, port))){
            long currTime=TimeUnit.SECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
            if((currTime-startTime)>10) return null; //ensure null handled
        }
    }
      //SENDING CODE HERE
      blockerCheck.removeBlock(ip, port);
}

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

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