1

我一直在寻找解决方案,但我还没有找到。我正在尝试对套接字锁进行编程,以便一次只能运行我的程序的一个实例,但是我遇到了套接字关闭异常。这是我的 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)
4

2 回答 2

1

您的测试超时失败。你打电话时

 @Test(timeout = 10000)
    public void lockProducer() {
        assertTrue(producerLocker.getLock());
    }

在指定端口上打开服务器套接字,然后等待连接进入会发生什么情况。

serverSocket = new ServerSocket(PORT,1);
clientSocket = serverSocket.accept();

因此,您的代码将等待接受直到超时,因为您没有发起连接尝试。

于 2012-09-21T18:55:24.653 回答
0

如果服务器套接字的目的只是防止两个实例同时运行,那么接受它上的连接是没有意义的。只需删除客户端套接字变量和 accept() 调用。您实现的不是锁,而是实际上是死锁。

于 2012-09-21T20:42:36.557 回答