Separating a received message to two parts
I have a server in java which is supposed to transfer an IP address and a number to a client in a single message. Here is the transfer code:
int indexMin;
int num;
.
.
.
String found = String.format("10.0.0.%1$d %2$d", indexMin, num);
byte foundByte = found.getBytes();
the length of indexMin and num is variable (and they may have 1 or 2 or ... digits).
I'm using Python on the client side. I want my client to receive this string and differentiate between the address (for example 10.0.0.5) and the second value which is separated by space. Here is the code that I'm using on the client:
def read_udp(s):
data,addr = s.recvfrom(1024)
print("received message:", data)
.
.
.
address = #adress part
num = #2nd number part
Since the length of the received string is variable, how can I extract the address and the num? Should I check the characters of the received message one by one to find the space or is there a better way?
java python string
add a comment |
I have a server in java which is supposed to transfer an IP address and a number to a client in a single message. Here is the transfer code:
int indexMin;
int num;
.
.
.
String found = String.format("10.0.0.%1$d %2$d", indexMin, num);
byte foundByte = found.getBytes();
the length of indexMin and num is variable (and they may have 1 or 2 or ... digits).
I'm using Python on the client side. I want my client to receive this string and differentiate between the address (for example 10.0.0.5) and the second value which is separated by space. Here is the code that I'm using on the client:
def read_udp(s):
data,addr = s.recvfrom(1024)
print("received message:", data)
.
.
.
address = #adress part
num = #2nd number part
Since the length of the received string is variable, how can I extract the address and the num? Should I check the characters of the received message one by one to find the space or is there a better way?
java python string
I don't know python, but I expect there is an easy way to separate the two value using a space as a separator. e.g. stackoverflow.com/questions/8113782/…
– Peter Lawrey
Nov 22 at 10:31
Both fields are separated by a space. Just split the string.
– jhamon
Nov 22 at 10:32
add a comment |
I have a server in java which is supposed to transfer an IP address and a number to a client in a single message. Here is the transfer code:
int indexMin;
int num;
.
.
.
String found = String.format("10.0.0.%1$d %2$d", indexMin, num);
byte foundByte = found.getBytes();
the length of indexMin and num is variable (and they may have 1 or 2 or ... digits).
I'm using Python on the client side. I want my client to receive this string and differentiate between the address (for example 10.0.0.5) and the second value which is separated by space. Here is the code that I'm using on the client:
def read_udp(s):
data,addr = s.recvfrom(1024)
print("received message:", data)
.
.
.
address = #adress part
num = #2nd number part
Since the length of the received string is variable, how can I extract the address and the num? Should I check the characters of the received message one by one to find the space or is there a better way?
java python string
I have a server in java which is supposed to transfer an IP address and a number to a client in a single message. Here is the transfer code:
int indexMin;
int num;
.
.
.
String found = String.format("10.0.0.%1$d %2$d", indexMin, num);
byte foundByte = found.getBytes();
the length of indexMin and num is variable (and they may have 1 or 2 or ... digits).
I'm using Python on the client side. I want my client to receive this string and differentiate between the address (for example 10.0.0.5) and the second value which is separated by space. Here is the code that I'm using on the client:
def read_udp(s):
data,addr = s.recvfrom(1024)
print("received message:", data)
.
.
.
address = #adress part
num = #2nd number part
Since the length of the received string is variable, how can I extract the address and the num? Should I check the characters of the received message one by one to find the space or is there a better way?
java python string
java python string
asked Nov 22 at 10:27
helen
327216
327216
I don't know python, but I expect there is an easy way to separate the two value using a space as a separator. e.g. stackoverflow.com/questions/8113782/…
– Peter Lawrey
Nov 22 at 10:31
Both fields are separated by a space. Just split the string.
– jhamon
Nov 22 at 10:32
add a comment |
I don't know python, but I expect there is an easy way to separate the two value using a space as a separator. e.g. stackoverflow.com/questions/8113782/…
– Peter Lawrey
Nov 22 at 10:31
Both fields are separated by a space. Just split the string.
– jhamon
Nov 22 at 10:32
I don't know python, but I expect there is an easy way to separate the two value using a space as a separator. e.g. stackoverflow.com/questions/8113782/…
– Peter Lawrey
Nov 22 at 10:31
I don't know python, but I expect there is an easy way to separate the two value using a space as a separator. e.g. stackoverflow.com/questions/8113782/…
– Peter Lawrey
Nov 22 at 10:31
Both fields are separated by a space. Just split the string.
– jhamon
Nov 22 at 10:32
Both fields are separated by a space. Just split the string.
– jhamon
Nov 22 at 10:32
add a comment |
2 Answers
2
active
oldest
votes
Assuming your string is something like data = "10.10.122.1 5678" you can just do
data_split = data.split(" ")
address, num = data_split[0], data_split[1]
add a comment |
You can simply do :
address, num = data.split();
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%2f53428840%2fseparating-a-received-message-to-two-parts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming your string is something like data = "10.10.122.1 5678" you can just do
data_split = data.split(" ")
address, num = data_split[0], data_split[1]
add a comment |
Assuming your string is something like data = "10.10.122.1 5678" you can just do
data_split = data.split(" ")
address, num = data_split[0], data_split[1]
add a comment |
Assuming your string is something like data = "10.10.122.1 5678" you can just do
data_split = data.split(" ")
address, num = data_split[0], data_split[1]
Assuming your string is something like data = "10.10.122.1 5678" you can just do
data_split = data.split(" ")
address, num = data_split[0], data_split[1]
answered Nov 22 at 10:34
b-fg
1,79111422
1,79111422
add a comment |
add a comment |
You can simply do :
address, num = data.split();
add a comment |
You can simply do :
address, num = data.split();
add a comment |
You can simply do :
address, num = data.split();
You can simply do :
address, num = data.split();
answered Nov 22 at 10:39
Gautam
1,10819
1,10819
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%2f53428840%2fseparating-a-received-message-to-two-parts%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
I don't know python, but I expect there is an easy way to separate the two value using a space as a separator. e.g. stackoverflow.com/questions/8113782/…
– Peter Lawrey
Nov 22 at 10:31
Both fields are separated by a space. Just split the string.
– jhamon
Nov 22 at 10:32