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
Cbas
  • 105

2 Answers2

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!

Dave C
  • 732
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.