I'm pretty new to Unix coding and would like to work on a few simple networking projects. I found a few tutorials that are exactly what I am looking for - this one in particular: http://www.linuxhowtos.org/C_C++/socket.htm.

I have figured out how to compile the server and client c files into executables, and I can run them in the terminal like this:

open server
open client

since I'm using Mac OS X 10.7.5. My problem is that I need to pass arguments to these programs when I initialize them. The tutorial uses this code:

but that throws a bunch of errors. I tried many variations but couldn't get any to run. I also couldn't get the Chrome example working from that post (I don't have FF installed).

How do I pass a simple integer port number as an argument to the program like in the tutorial? Is this easier to do on a Linux machine? I have no problem switching OS's at this point, and it would be ideal if i could use the 'server 51717' syntax that the tutorial uses instead of having to create a separate run script for each program. Looking for an answer and advice. Thanks.

EDIT:

I have gotten it to run correctly, but somethings still not working right. I start the server like this:

Do you have to use open? Can't you just execute the binary directly e.g. cd to the directory with it in and then ./server 51717 ?
–
Dave CApr 5 '13 at 9:11

Don't have to use open, I didn't know I could call exes directly (I tried '$ server' but that didn't work so I assumed that I needed a run function).. I'm still having trouble running the client though - it isn't accepting strings as hostnames
–
CbasApr 5 '13 at 20:42

1

So mClient is your hostname? Are you doing a DNS lookup on it (I'm assuming yes because the error is no such host) in which case does it exist and work for your client? What happens if you ping mClient ?
–
Dave CApr 6 '13 at 14:24

that was the issue, I didn't know I had to define the alias in hosts before using it. Thanks
–
CbasApr 6 '13 at 16:05

using an IP address as the hostname is the only thing that seems to work - see my edit for an example
–
CbasApr 5 '13 at 20:37

You have to set up a real DNS server or aliases /etc/hosts for custom names to work. I bet using localhost will also work because it is aliased to 127.0.0.1, your local IP address will do too (192.168.0.13 or some such).
–
Ярослав РахматуллинApr 5 '13 at 21:44

Ohh, thanks it makes sense now. I added an alias in the hosts file and got it to work.
–
CbasApr 6 '13 at 16:04

Just execute the binary directly from the console - using open on my MacBook (10.8.4) "arbitrary" binaries can't be opened from open (open is meant to use a file descriptor to open a file in the appropriate application).

So for example if your server and client binaries are in Desktop (~/Desktop) then they can be executed from the console with:

~/Desktop/server 51717
~/Desktop/client clientName 51717

Or locally in the Desktop directory:

cd ~/Desktop
./server 51717
./client clientName 51717

This is exactly the same behavior as you would see on Linux (or any other *ix system).

Note the files would need to be marked as executable but the compiler will normally do that for you.