LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   socket programming Q (https://www.linuxquestions.org/questions/programming-9/socket-programming-q-481796/)

sachitha 09-09-2006 02:14 AM

socket programming Q
 
in socket programming, (in a sort of a echo server program) how do u make the client wait in a prompt like "client>", Do you simply make it print continuously??

how do u distinguish whether you have typed the server's IP in the 127.0.0.1 format or local host format, using the command line arguments??
i have used the following coding for that, but it doesn't work..
if(strcmp("%s.%s.%s.%s", argv[1])==0)
{}
else if(strcmp("%s", argv[1])==0)
{}

paulsm4 09-09-2006 07:59 PM

Hi -

I believe the easiest way is to call something like inet_aton()

If it returns "OK", then you've got a valid IP address. Otherwise, if it returns "invalid address", then you've probably got a hostname.

'Hope that helps .. PSM

Wim Sturkenboom 09-10-2006 10:30 PM

Quote:

Originally Posted by sachitha
in socket programming, (in a sort of a echo server program) how do u make the client wait in a prompt like "client>", Do you simply make it print continuously??

If I understand the question correctly:
the server will echo the prompt to the client after avery transaction. So when the client connects, the server sends the prompt. When a command is issued from the client, the 'last' thing the server does is sending the prompt again.

A prompt always indicates that the system is done with the current command and ready to receive a new one.

sachitha 09-10-2006 11:13 PM

so that means the prompt has to be sent by the server to the client???
cant i simply do this at the client program itself with a Fputs??? i'm clueless how though..

paulsm4 09-11-2006 12:24 AM

sachitha -

In this and your other post (http://www.linuxquestions.org/questi....php?t=481798), you seem to be confusing exactly which program is performing which activity. Let's break it down, shall we?

1. "pwd" of the server directory
Here's what you want to do:
a) The server will be listening on some port on some host (for example, 9090 on "localhost").

b) The client will connect to the server and request "please tell me your working directory"
Perhaps the easiest way is if the client just sent the string "pwd".

c) The server will accept the connection

d) Using the new socket created by "accept()", the server will read and parse
the client's request ("pwd").

e) The server will respond.
If I were you, I would take "baby steps": just send the response "tbd" until
you get it working. Figure out the "real" response later (my suggestion was
to use "getcwd()").

f) The client reads the response and closes the connection.

g) The server closes its side of the connection and listens for the next connection
request.

2. How to differentiate between an IP address and a hostname
I suggested using "inet_pton()" and checking the error status.

3. Finally: "Q: Where does the prompt come from?"
A: It comes from where ever you want it to.
Yes, "printf()/fgets()" is perfectly acceptable.
"printf()/scanf()" will work, too. As will "cout/cin".
You can pop up a GUI and read it from a text edit box.
Or read it from the command line when you run your client.

The point is, your "user input" is COMPLETELY SEPARATE from your sockets
communication. You get data from "somewhere" (argv[] or fgets() or a GUI text
box - it doesn't matter) and, once you're happy with it, you open a socket and
send it.

Make sense?

Book recommendation:
Unix Network Programming, W. Richard Stevens.
<= AN ABSOLUTE "MUST HAVE"...

IMHO


All times are GMT -5. The time now is 02:44 AM.