I’m working through Beej’s socket tutorial. Using the sample code I’ve created a simple server iOS app and a simple client running on my desktop.
When I run the server on the iOS simulator everything works fine. From the OS X Terminal I make a request to the server and receive the expected Hello, world! response:
$ g++ client.cpp
./a.out localhost
client: connecting to 127.0.0.1
client: received numbytes 13
client: received 'Hello, world!'
However when I run the server app on the device I receive a strange response:
$ ./a.out 192.168.1.2
client: connecting to 192.168.1.2
client: received numbytes 0
client: received ''
I’ve modified the sample code to print all errors to the console. Unfortunately I don’t see any errors when running on the simulator or the device.
I’m fairly new to network programming. Perhaps my wireless router is preventing the bytes from being sent? Any debugging tips are greatly appreciated.
I also tried using telnet to talk to the device and the simulator:
$ telnet 192.168.1.2 3490
Trying 192.168.1.2...
Connected to 192.168.1.2.
Escape character is '^]'.
Connection closed by foreign host.
$ telnet localhost 3490
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello, world!Connection closed by foreign host.
Seemingly similar results (running on the simulator returns the expected results while running on the device returns nothing).
printing the number of bytes sent helped. It highlighted the fact that
send()wasn’t called. Commenting theif (!fork()) {check causes the bytes to be sent and received as expected. Now to understand the purpose of the above check and why it returns different results on the simulator and device.