Java MulticastSocket setSoTimeout freezes program
I am trying to use multicastsockets in a program of mine. Today, I'm not sure what changed, but my program (which hasn't changed and was working fine before) started freezing on random occasion when the setSoTimeout(int) method was called. It's not consistently, the method may be called a few times before this happens. Any clues as to what this is?
Code:
private String getPlayerInfo() {
sendMessageToPlayers("count", 500);
String temp = new String[4];
while (true) {
try {
byte buffer = new byte[1000];
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);
cSocket.receive(datagram);
String message = new String(datagram.getData());
if (message.contains("received")) {
message.substring(message.indexOf("received") - 1, 1);
int playerNum = Integer.parseInt(message.substring(message.indexOf("received") - 1, 1));
temp[playerNum] = message.substring(message.indexOf("received") + "received ".length());
}
} catch (IOException e) {
try {
cSocket.setSoTimeout(100000);
} catch (SocketException e1) {
break;
}
break;
}
}
return temp;
}
public void sendMessageToPlayers(String message, int timeout) {
byte buf = (message).getBytes();
DatagramPacket dg = new DatagramPacket(buf, buf.length, group, 6789);
try {
cSocket.send(dg);
cSocket.setSoTimeout(timeout);
} catch (IOException ex) {
System.out.println(ex);
}
}
java multicastsocket
add a comment |
I am trying to use multicastsockets in a program of mine. Today, I'm not sure what changed, but my program (which hasn't changed and was working fine before) started freezing on random occasion when the setSoTimeout(int) method was called. It's not consistently, the method may be called a few times before this happens. Any clues as to what this is?
Code:
private String getPlayerInfo() {
sendMessageToPlayers("count", 500);
String temp = new String[4];
while (true) {
try {
byte buffer = new byte[1000];
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);
cSocket.receive(datagram);
String message = new String(datagram.getData());
if (message.contains("received")) {
message.substring(message.indexOf("received") - 1, 1);
int playerNum = Integer.parseInt(message.substring(message.indexOf("received") - 1, 1));
temp[playerNum] = message.substring(message.indexOf("received") + "received ".length());
}
} catch (IOException e) {
try {
cSocket.setSoTimeout(100000);
} catch (SocketException e1) {
break;
}
break;
}
}
return temp;
}
public void sendMessageToPlayers(String message, int timeout) {
byte buf = (message).getBytes();
DatagramPacket dg = new DatagramPacket(buf, buf.length, group, 6789);
try {
cSocket.send(dg);
cSocket.setSoTimeout(timeout);
} catch (IOException ex) {
System.out.println(ex);
}
}
java multicastsocket
add a comment |
I am trying to use multicastsockets in a program of mine. Today, I'm not sure what changed, but my program (which hasn't changed and was working fine before) started freezing on random occasion when the setSoTimeout(int) method was called. It's not consistently, the method may be called a few times before this happens. Any clues as to what this is?
Code:
private String getPlayerInfo() {
sendMessageToPlayers("count", 500);
String temp = new String[4];
while (true) {
try {
byte buffer = new byte[1000];
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);
cSocket.receive(datagram);
String message = new String(datagram.getData());
if (message.contains("received")) {
message.substring(message.indexOf("received") - 1, 1);
int playerNum = Integer.parseInt(message.substring(message.indexOf("received") - 1, 1));
temp[playerNum] = message.substring(message.indexOf("received") + "received ".length());
}
} catch (IOException e) {
try {
cSocket.setSoTimeout(100000);
} catch (SocketException e1) {
break;
}
break;
}
}
return temp;
}
public void sendMessageToPlayers(String message, int timeout) {
byte buf = (message).getBytes();
DatagramPacket dg = new DatagramPacket(buf, buf.length, group, 6789);
try {
cSocket.send(dg);
cSocket.setSoTimeout(timeout);
} catch (IOException ex) {
System.out.println(ex);
}
}
java multicastsocket
I am trying to use multicastsockets in a program of mine. Today, I'm not sure what changed, but my program (which hasn't changed and was working fine before) started freezing on random occasion when the setSoTimeout(int) method was called. It's not consistently, the method may be called a few times before this happens. Any clues as to what this is?
Code:
private String getPlayerInfo() {
sendMessageToPlayers("count", 500);
String temp = new String[4];
while (true) {
try {
byte buffer = new byte[1000];
DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);
cSocket.receive(datagram);
String message = new String(datagram.getData());
if (message.contains("received")) {
message.substring(message.indexOf("received") - 1, 1);
int playerNum = Integer.parseInt(message.substring(message.indexOf("received") - 1, 1));
temp[playerNum] = message.substring(message.indexOf("received") + "received ".length());
}
} catch (IOException e) {
try {
cSocket.setSoTimeout(100000);
} catch (SocketException e1) {
break;
}
break;
}
}
return temp;
}
public void sendMessageToPlayers(String message, int timeout) {
byte buf = (message).getBytes();
DatagramPacket dg = new DatagramPacket(buf, buf.length, group, 6789);
try {
cSocket.send(dg);
cSocket.setSoTimeout(timeout);
} catch (IOException ex) {
System.out.println(ex);
}
}
java multicastsocket
java multicastsocket
asked Nov 12 at 20:13
kennemat
965
965
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The documentation of setSoTimeout is
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
With this option set to a non-zero timeout, a read() call on the
InputStream associated with this Socket will block for only this
amount of time. If the timeout expires, a
java.net.SocketTimeoutException is raised, though the Socket is still
valid. The option must be enabled prior to entering the blocking
operation to have effect. The timeout must be > 0. A timeout of zero
is interpreted as an infinite timeout.
And SO_TIMEOUT
Set a timeout on blocking Socket operations:
- ServerSocket.accept()
- SocketInputStream.read()
- DatagramSocket.receive()
The option must be set prior to entering a blocking operation to take
effect. If the timeout expires and the operation would continue to
block, java.io.InterruptedIOException is raised. The Socket is not
closed in this case.
I don't understand the usage you made of it.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269427%2fjava-multicastsocket-setsotimeout-freezes-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The documentation of setSoTimeout is
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
With this option set to a non-zero timeout, a read() call on the
InputStream associated with this Socket will block for only this
amount of time. If the timeout expires, a
java.net.SocketTimeoutException is raised, though the Socket is still
valid. The option must be enabled prior to entering the blocking
operation to have effect. The timeout must be > 0. A timeout of zero
is interpreted as an infinite timeout.
And SO_TIMEOUT
Set a timeout on blocking Socket operations:
- ServerSocket.accept()
- SocketInputStream.read()
- DatagramSocket.receive()
The option must be set prior to entering a blocking operation to take
effect. If the timeout expires and the operation would continue to
block, java.io.InterruptedIOException is raised. The Socket is not
closed in this case.
I don't understand the usage you made of it.
add a comment |
The documentation of setSoTimeout is
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
With this option set to a non-zero timeout, a read() call on the
InputStream associated with this Socket will block for only this
amount of time. If the timeout expires, a
java.net.SocketTimeoutException is raised, though the Socket is still
valid. The option must be enabled prior to entering the blocking
operation to have effect. The timeout must be > 0. A timeout of zero
is interpreted as an infinite timeout.
And SO_TIMEOUT
Set a timeout on blocking Socket operations:
- ServerSocket.accept()
- SocketInputStream.read()
- DatagramSocket.receive()
The option must be set prior to entering a blocking operation to take
effect. If the timeout expires and the operation would continue to
block, java.io.InterruptedIOException is raised. The Socket is not
closed in this case.
I don't understand the usage you made of it.
add a comment |
The documentation of setSoTimeout is
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
With this option set to a non-zero timeout, a read() call on the
InputStream associated with this Socket will block for only this
amount of time. If the timeout expires, a
java.net.SocketTimeoutException is raised, though the Socket is still
valid. The option must be enabled prior to entering the blocking
operation to have effect. The timeout must be > 0. A timeout of zero
is interpreted as an infinite timeout.
And SO_TIMEOUT
Set a timeout on blocking Socket operations:
- ServerSocket.accept()
- SocketInputStream.read()
- DatagramSocket.receive()
The option must be set prior to entering a blocking operation to take
effect. If the timeout expires and the operation would continue to
block, java.io.InterruptedIOException is raised. The Socket is not
closed in this case.
I don't understand the usage you made of it.
The documentation of setSoTimeout is
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
With this option set to a non-zero timeout, a read() call on the
InputStream associated with this Socket will block for only this
amount of time. If the timeout expires, a
java.net.SocketTimeoutException is raised, though the Socket is still
valid. The option must be enabled prior to entering the blocking
operation to have effect. The timeout must be > 0. A timeout of zero
is interpreted as an infinite timeout.
And SO_TIMEOUT
Set a timeout on blocking Socket operations:
- ServerSocket.accept()
- SocketInputStream.read()
- DatagramSocket.receive()
The option must be set prior to entering a blocking operation to take
effect. If the timeout expires and the operation would continue to
block, java.io.InterruptedIOException is raised. The Socket is not
closed in this case.
I don't understand the usage you made of it.
edited Nov 12 at 20:32
answered Nov 12 at 20:26
Aubin
11.1k64164
11.1k64164
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269427%2fjava-multicastsocket-setsotimeout-freezes-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown