简体   繁体   中英

Blocking a java socket that is opening/in use

Good Day,

I have a sendMessage function in java that looks like this. It exists currently as a static method that can be called by any thread operation (such as a HTTP request). However, if I was to send two requests at the same time..one of the sendMessage requests will throw a "Connection reset".

I need someway to know that my current port and ip is in use! 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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