문제

I am using Swing and now i want to Allocate one Special port for the Java class which is running in my swing. so how do i bind that port so every time when user run that so there wont be creating new object it will show message that process is already running!! I need to Bind that port to the Process how to do it?

Socket s = new Socket();
s.bind(new InetSocketAddress(9000));

i have bind this address but how to bind it in current class object.?

도움이 되었습니까?

해결책

try to use static variable - this object will be created only once

private static Socket s = new Socket();

static {
    try {
        s.bind(new InetSocketAddress(9000));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top