Search in sources :

Example 66 with Socket

use of java.net.Socket in project okio by square.

the class SocketTimeoutTest method writeWithoutTimeout.

@Test
public void writeWithoutTimeout() throws Exception {
    Socket socket = socket(0, ONE_MB);
    Sink sink = Okio.buffer(Okio.sink(socket));
    sink.timeout().timeout(500, TimeUnit.MILLISECONDS);
    byte[] data = new byte[ONE_MB];
    sink.write(new Buffer().write(data), data.length);
    sink.flush();
    socket.close();
}
Also used : ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 67 with Socket

use of java.net.Socket in project okio by square.

the class SocketTimeoutTest method readWithTimeout.

@Test
public void readWithTimeout() throws Exception {
    Socket socket = socket(0, 0);
    BufferedSource source = Okio.buffer(Okio.source(socket));
    source.timeout().timeout(250, TimeUnit.MILLISECONDS);
    try {
        source.require(ONE_MB);
        fail();
    } catch (SocketTimeoutException expected) {
    }
    socket.close();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 68 with Socket

use of java.net.Socket in project zoj by licheng.

the class JudgeClientJudgeThread method process.

private void process() throws IOException, InterruptedException {
    this.status = Status.CONNECTING;
    this.socket = new Socket();
    this.socket.setKeepAlive(true);
    this.socket.setSoTimeout(JudgeClient.READ_TIMEOUT);
    this.logger.info("Connecting to " + this.address.getAddress().getCanonicalHostName() + ":" + this.address.getPort());
    this.socket.connect(this.address, JudgeClient.CONNECTION_TIMEOUT);
    this.logger.info("Connected");
    this.in = new DataInputStream(this.socket.getInputStream());
    this.out = new DataOutputStream(this.socket.getOutputStream());
    this.status = Status.RUNNING;
    try {
        while (!this.isInterrupted()) {
            try {
                this.status = Status.WAITING;
                if (this.submissionQueueReader == null) {
                    CompoundSubmissionFilter submissionFilter = new CompoundSubmissionFilter();
                    submissionFilter.add(new SimpleSubmissionFilter(new NegationTest(new LanguageTest(this.client.getSupportedLanguages())), Priority.DENY));
                    submissionFilter.add(this.submissionFilter);
                    submissionFilter.add(this.client.getSubmissionFilter());
                    submissionFilter.add(this.client.getService().getSubmissionFilter());
                    this.submissionQueueReader = this.client.getService().getSubmissionQueue().getReader(submissionFilter);
                }
                // IMPORTANT: set to null here to avoid rejudging this one when queue.poll throws a
                // PersistenceException
                this.submission = null;
                this.submission = this.submissionQueueReader.poll(this);
                this.client.getService().judgeStart(this.submission);
                this.status = Status.RUNNING;
                try {
                    this.judge(this.submission);
                } catch (JudgeServerErrorException e) {
                    this.logger.error(e);
                    this.submission.setJudgeReply(JudgeReply.JUDGE_INTERNAL_ERROR);
                } catch (JudgeClientErrorException e) {
                    this.logger.error(e);
                    this.submission.setJudgeReply(JudgeReply.JUDGE_INTERNAL_ERROR);
                } catch (PersistenceException e) {
                    this.logger.error(e);
                    this.submission.setJudgeReply(JudgeReply.JUDGE_INTERNAL_ERROR);
                }
                this.submissionDAO.updateSubmission(this.submission, 1);
                this.submission.setContent(null);
            } catch (PersistenceException e) {
                this.client.getService().judge(this.submission, Priority.HIGH);
                Thread.sleep(60000);
            } finally {
                this.client.getService().judgeDone(this.submission);
            }
        }
    } finally {
        Utility.closeSocket(this.socket);
    }
}
Also used : SimpleSubmissionFilter(cn.edu.zju.acm.onlinejudge.judgeservice.submissionfilter.SimpleSubmissionFilter) DataOutputStream(java.io.DataOutputStream) LanguageTest(cn.edu.zju.acm.onlinejudge.judgeservice.submissiontest.LanguageTest) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) DataInputStream(java.io.DataInputStream) CompoundSubmissionFilter(cn.edu.zju.acm.onlinejudge.judgeservice.submissionfilter.CompoundSubmissionFilter) NegationTest(cn.edu.zju.acm.onlinejudge.judgeservice.submissiontest.NegationTest) Socket(java.net.Socket)

Example 69 with Socket

use of java.net.Socket in project bigbluebutton by bigbluebutton.

the class NetworkSocketStreamSender method connect.

public void connect(String host, int port, boolean useTLS) throws ConnectionException {
    //We use this value to devie how to create the socket
    this.useTLS = useTLS;
    System.out.println("NetworkSocketStreamSender: connecting to " + host + ":" + port);
    try {
        //Handling if TLS is enabled or not
        if (useTLS) {
            System.out.println("Connecting over TLS");
            sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
            outstream = new DataOutputStream(sslSocket.getOutputStream());
        } else {
            //If not use regular socket
            socket = new Socket(host, port);
            outstream = new DataOutputStream(socket.getOutputStream());
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
        throw new ConnectionException("UnknownHostException: " + host);
    } catch (IOException e) {
        e.printStackTrace();
        throw new ConnectionException("IOException: " + host + ":" + port);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 70 with Socket

use of java.net.Socket in project bigbluebutton by bigbluebutton.

the class NetworkStreamSender method trySocketConnection.

private boolean trySocketConnection(String host, int port) {
    try {
        Socket socket = new Socket();
        InetSocketAddress endpoint = new InetSocketAddress(host, port);
        socket.connect(endpoint, 5000);
        socket.close();
        return true;
    } catch (UnknownHostException e) {
        System.out.println("Unknown host [" + host + "]");
    } catch (IOException e) {
        System.out.println("Cannot connect to [" + host + ":" + port + "]");
    }
    return false;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)1633 IOException (java.io.IOException)705 ServerSocket (java.net.ServerSocket)578 OutputStream (java.io.OutputStream)377 InetSocketAddress (java.net.InetSocketAddress)367 Test (org.junit.Test)362 InputStream (java.io.InputStream)259 InputStreamReader (java.io.InputStreamReader)179 BufferedReader (java.io.BufferedReader)160 SocketException (java.net.SocketException)137 SSLSocket (javax.net.ssl.SSLSocket)111 SocketTimeoutException (java.net.SocketTimeoutException)97 UnknownHostException (java.net.UnknownHostException)86 ConnectException (java.net.ConnectException)84 InetAddress (java.net.InetAddress)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 OutputStreamWriter (java.io.OutputStreamWriter)70 DataOutputStream (java.io.DataOutputStream)68 ServletOutputStream (javax.servlet.ServletOutputStream)68 CountDownLatch (java.util.concurrent.CountDownLatch)65