1
votes

if i use:

 socket_read($socket, 1024);

I don't know what type of variable it returns. I want to make it to an int, but i don't know how.
The Server respons only a number (35).
If i echo it, i get 35. If i cast it to "(int)" i get 0. intval returns 0 too.
I have tried to cast it to string, and pick the first letter (3) but it returned 0. Substr doesn't work too.
I want to have an int value, so that i can calculate things with this value. I need an int to calculate the prozentage of this value from another value.
What can i do?

EDIT:

Fixed it in JAVA SERVER SOCKET CODE whith:

PrintWriter out = new PrintWriter(server.getOutputStream());

instead of using

DataOutputStream out = new DataOutputStream(server.getOutputStream());
3
What if it echoes something like <p>35</p> or just some invisible to you symbol? Which is parsed by HTML. - sybear

3 Answers

1
votes

Why not read the manual page. It returns a string.

Try print_r to find out the details of that string. Might be in binary?!

1
votes

As you can see in the manual, socket_read always returns a string. You should try using var_dump() to check the real value of the variable.

Did you try to sum any number to that variable? In the autocast it may work.

0
votes

Seems like the string contains additional whitespace that prevents PHP from converting it to a number.

Therefore I suggest you use the trim() function to remove whitespace before converting it to a number. Or you could use a regular expression to remove all non-numeric characters $out = preg_replace("/[^0-9]/g", "", $in)

If this still doesn't work and var_dump doesn't help you either, I'd suggest to sniff the network traffic using WireShark to inspect the packet coming from the server. If you're in control of what the server sends, use a decent serialization method like XML or JSON instead of inventing some protocol yourself.