Solved

C++ telneting

Posted on 1998-08-12
6
285 Views
Last Modified: 2010-04-02
I have RedHat linux. I want to know how to make a telnet server that will accept MULTIPLE REQUESTS at one time. I know how to already make a single user telnet server, where one user can telnet in, and when they give the quit command, such as returning 'q' to the server, they will be disconnected and the server will die.

I think I know just a tad on making multiple user Telnet servers. File descriptors. A VERY SIMPLE chat server would be appreciated,
thanx
ty
0
Comment
Question by:TRM
  • 4
  • 2
6 Comments
 

Author Comment

by:TRM
ID: 1170106
Lemme rephrase that last part: I know little on multiple user telnet servers: I know File descriptors are involved. I have no CLUE to what they are.
0
 
LVL 3

Accepted Solution

by:
sailwind earned 200 total points
ID: 1170107
File descriptors is a integer that represent the I/O location to read/write information.
In our case, we would be using socket descriptors (a type of descriptor) to define our
connection to the socket.

The general idea is:

-setup the socket to listen to
-wait for a connection on that socket
-when a connection is established on a new socket
-fork off a child process to do handle the communication
     through that new socket. Make sure you close off the
     original socket.

Here's a psedocode:

program multiConnection {

int sockfd;         // socket to listen connection requests from
int newsockfd;   // new socket to to with when connection is establshed
int childpid;       // child's process id

      Create and bind(sockfd);      // generate our socket based on protocol and port
      Listen(sockfd);                    // wait for connection on this socket

      for(infinity) {
           newsockfd = accept(sockfd); // connection request received, generate new socket
                                                      // to talk to client
           childpid=fork();                     // create a child to handle the communication
           if(childpid == 0) {                  // if it is child          
                 close(sockfd);                // close the parent's socket
                 doaction();                     // do the communication activities
                 exit(0);                          // when communication is done, kill the child
            } // if

            close(newsockfd);               // close the newly opened socket
     }
}
     



0
 

Author Comment

by:TRM
ID: 1170108
It helps, but i would like to have some more actual code. I, currently, am using(in my program), socket(), bind(), listen(), and accept(). Remember : i know how a single user can connect w/out using any file descripters or anything. i want to know how to let several different users connect, then have my program send inputed data from one to the output of another. thanx.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:TRM
ID: 1170109
It helps, but i would like to have some more actual code. I, currently, am using(in my program), socket(), bind(), listen(), and accept(). Remember : i know how a single user can connect w/out using any file descripters or anything. i want to know how to let several different users connect, then have my program send inputed data from one to the output of another. thanx.
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1170110
Here's an example of multi-node server. It can handle mutliple connections but there
is no way for each connection to display the information to another. The only way
I can think of is to have each child process alert the parent of the message to update
and send to other connections.

The code is taken from UNIX Network Programming by W. Richard Stevens.
A great book, one that every network engineer should have. I'm including the
multiple connection part. Remember, the file descriptor is just the socket descriptor.
So you're already using it but didn't know.

listen(sockfd, 5);

for(;;) {
  clilen = sizeof(cli_addr);
  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  if(newsockfd < 0)
    err_dump("server: accept error");
  if((childpid = fork()) < 0)
    err_dump("server: fork error");
  else if(childpid==0) {
    close(sockfd);
    str_echo(newsockfd);   // the actual running function
    exit(0);
  }

  close(newsockfd);
}

There, reads just like my pseudocode :)

0
 

Author Comment

by:TRM
ID: 1170111
OK. Thanks.  I'll give it a try

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question