0

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:

server 51717
client clientName 51717

but I haven't found a way to replicate that in my Mac terminal.

open server --args 51717 //doesn't work

I found this post and created an applescript with this code

do shell script "open -a /Desktop/server --args 51717"

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:

$ ./server 3456

then open a new terminal and start the client with limited success:

$ ./client mClient 3456
ERROR, no such host
$ ./client 127.0.0.1 3456
Please enter the message: //running correctly
//... this also works
$ ./client 127.1 3456
Please enter the message: //running correctly

I can start the client with some number.number combinations but it doesn't work for all of them and I can't use any letters. Why does only one of these solutions work?

SOLUTION:

$ sudo nano /private/etc/hosts

add 127.0.0.1 mClient to the bottom then press Ctrl^o to save

Now this works:

$ ./client mClient 3456
4
  • 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 C
    Apr 5, 2013 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
    – Cbas
    Apr 5, 2013 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 C
    Apr 6, 2013 at 14:24
  • that was the issue, I didn't know I had to define the alias in hosts before using it. Thanks
    – Cbas
    Apr 6, 2013 at 16:05

2 Answers 2

1

Compile the code:

$ gcc client.c -o client
$ gcc server.c -o server

Run the server without arguments:

$ ./server
ERROR, no port provided

Run the server with one argument:

$ ./server 3456

In a different terminal:

$ ./client 3456
usage ./client hostname port  

a usage message indicates how to run a binary, that is which arguments to give it

$ ./client eee.lan 3456
Please enter the message:

since the server is listening on all interfaces, you can also use

$ ./client 127.0.0.1 3456
Please enter the message: Hello World!
I got your message

so arguments are strings that follow the command. In the gcc example there are 3 arguments: server.c -o server

the relevant part of the code is (server):

portno = atoi(argv[1]);

argv is the vector (array) which holds all the arguments given to a command on the command prompt. atoi converts a string to an integer.

3
  • using an IP address as the hostname is the only thing that seems to work - see my edit for an example
    – Cbas
    Apr 5, 2013 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, 2013 at 21:44
  • Ohh, thanks it makes sense now. I added an alias in the hosts file and got it to work.
    – Cbas
    Apr 6, 2013 at 16:04
1

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.

Good luck!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .