在accept()调用上抛出Socket Closed异常

时间:2012-09-21 18:14:25

标签: java sockets port

我找了一个解决方案,但我还找不到一个。我正在尝试编写一个套接字锁,因此我的程序一次只能运行一个实例,但是我得到一个套接字关闭异常。这是我的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)

2 个答案:

答案 0 :(得分:1)

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

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

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

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

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

答案 1 :(得分:0)

如果服务器套接字的目的只是为了防止一次运行两个实例,那么根本就没有接受连接。只需删除客户端套接字变量和accept()调用即可。你实现的不是锁定,而是实际上是一个死锁。