Receiving ints over TCP


I’m trying to do what I think should be very simple: read the data sent over TCP as an integer. It seems, though, that TCP only reads strings. Is there a way around this? Or a way to convert the string to an int? I’ve tried atoi, but it doesn’t seem to do the trick.

Thanks in advance


Yes you have to convert what you get (string) to int. try :

int number;    
number = ofToInt(your_string);

Here is my problem with more code.

the function that returns a count:

def count():

oops… sorry, never mind. wrong message… your answer was correct. Thanks!

It is not true that TCP/IP only sends strings. While what Gallo said is true and is a very easy for a beginner, at some point you will need to send real binary data.

When you do, take a look at htonl() and ntohl() for example. These functions change the byte ordering of longs from what your host is using to the byte ordering of your destination. Of course in your case these probably are the same, since you are testing on the same box. But your data will be safely transmitted by using these functions when sending and receiving on sockets. Good Luck!