0

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?

helen
  • 587
  • 1
  • 10
  • 26
  • 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. https://stackoverflow.com/questions/8113782/split-string-on-whitespace-in-python – Peter Lawrey Nov 22 '18 at 10:31
  • Both fields are separated by a space. Just split the string. – jhamon Nov 22 '18 at 10:32

2 Answers2

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]
b-fg
  • 3,959
  • 2
  • 28
  • 44
1

You can simply do :

address, num = data.split();
Gautam
  • 1,862
  • 9
  • 16