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();
}
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();
}
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);
}
}
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);
}
}
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;
}
Aggregations