我一直在寻找解决方案,但我还没有找到。我正在尝试对套接字锁进行编程,以便一次只能运行我的程序的一个实例,但是我遇到了套接字关闭异常。这是我的 SocketCode 代码
package utilities;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import utilities.enums.ClientType;
public class SocketLocker {
private final int PORT;
private ServerSocket serverSocket;
private Socket clientSocket;
public SocketLocker(ClientType cType) {
serverSocket = null;
clientSocket = null;
switch (cType)
{
case PRODUCER:
PORT = 7856;
break;
case CONSUMER:
PORT = 7857;
break;
default:
PORT = 7859;
}
}
public boolean getLock() {
try {
serverSocket = new ServerSocket(PORT,1);
clientSocket = serverSocket.accept();
return true;
} catch (IOException e) {
// TODO: log event
e.printStackTrace();
}
return false;
}
public void releaseLock() {
try {
if (clientSocket != null)
clientSocket.close();
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
// TODO: Log event
}
}
}
这是我正在运行以获取异常的单元测试代码:
package utilities;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import utilities.enums.ClientType;
public class SocketLockerTest {
private SocketLocker producerLocker;
private SocketLocker consumerLocker;
@Before
public void start() {
producerLocker = new SocketLocker(ClientType.PRODUCER);
consumerLocker = new SocketLocker(ClientType.CONSUMER);
}
@Test(timeout = 10000)
public void lockProducer() {
assertTrue(producerLocker.getLock());
}
@Test(timeout = 10000)
public void lockConsumer() {
assertTrue(consumerLocker.getLock());
}
@Test(timeout = 10000)
public void lockProducerTwice() {
producerLocker.getLock();
assertFalse(producerLocker.getLock());
}
@Test(timeout = 10000)
public void lockSeparately() {
producerLocker.getLock();
assertTrue(consumerLocker.getLock());
}
@Test(timeout = 10000)
public void lockProdcuerSeparateObject(){
producerLocker.getLock();
assertFalse(new SocketLocker(ClientType.PRODUCER).getLock());
}
@After
public void finish() {
producerLocker.releaseLock();
consumerLocker.releaseLock();
}
}
这是一个例外:
java.net.SocketException: socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(Unknown Source)
at java.net.ServerSocket.implAccept(Unknown Source)
at java.net.ServerSocket.accept(Unknown Source)
at utilities.SocketLocker.getLock(SocketLocker.java:34)
at utilities.SocketLockerTest.lockProducer(SocketLockerTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.FailOnTimeout$1.run(FailOnTimeout.java:28)