Command line and waiting for a connection (Socket)

Hello everyone,

I am designing a server program in C++ and now that everything works for the clients, I would like to implement a command line. The code waits for a connection with the predefined function "new_socket = accept(serversocket,(struct sockaddr *)&client,&addrsize);". I already made a function for the command line but I can't find a way (in my brain or on the web) to accept commands (getline) whilst no connection is accepted yet. (I mean wait for a connection. If no connection, then be ready to receive some arguments from the command line). Here is part of the code:

cout << "Waiting for connection/command...";
command_line_function(); //getline(cin,string)
while(true){
new_socket = accept(serversocket,(struct sockaddr *)&client,&addrsize);
if(new_socket == INVALID_SOCKET){ //INVALID_SOCKET is an error somehow.
error_function();}
cout << "ACCEPTED\n";
//etc.
}


If anyone has a simple idea, this would really save me a lot of time.

Thank you in advance!
Last edited on
I'm not sure I understand you. You do know that accept() is a call the server makes, don't you? A client needs to call connect().
Yes I agree. But I want to 'getline' when no client call connect(). I mean the code following this fragment works fine, when the client 'connect()'. But I want that until the client does not connect(), the program accepts my command line. So that it is somehow doing both in parallel.

Thank you for your quick answer!
Please try again, I am not following you. I'll be happy to try to help you then!
A fast solution I found was to create a "monitoring" client. This one would send requests to the server and the server would return the desired information, according to the command line function I did in the server code. Now, what I wanted was to be able to input commands with the keyboard when the server 'waits for a connection' (accept). Because I can't do both except by using threads which seem complicated for this simple task.

Thank you again for the help

EDIT: I changed the code fragment and sorry for its bad explanation.
Last edited on
The server itself never* exposes itself to the command-line like that.

The command line program should itself be a client of the server -- a special one, mind you, but a client non-the-less.


* You could do it, but then you would have to implement either threads or pseudo-multi-tasking (which would be less difficult than you think, since all I/O would dispatch after a call to WaitForMultipleObjects(), just so long as each dispatch doesn't take too long the app would still be fairly peppy. You would have to make sure all input is raw, though, and implement readline()-like stuff yourself, much like the ever-popular "password with stars" kind of homework problem).

Good luck!
Topic archived. No new replies allowed.