Simple Telnet Server code under the VS platform

Source: Internet
Author: User
Tags message queue strcmp

This is the only way to give me the same confusion when I was doing this task.

1. Request to write a small server that can be linked with Telnet in WinDOS

Because of the limited network knowledge, plus 1 years of no use of C + +, vs under the development of less experience, so the beginning of the start, nonsense less to enter the theme

1) First I also Baidu Telnet Server source code, see a lot of is not too understand, and then no way to decide directly with Baidu Library recruit to the following is link http://wenku.baidu.com/link?url= Qrblqqslqxevrklvmkhanjvfgs3o36zy09p5nz9cy8iwbhjb9usgfnz2l1ytkrktj9l5yomjlybx9zoguer9rencq2_1gzcxxaejkjgctna

But I run the unsuccessful, so there is no way, one line to see.

2) debugging process encountered strcat is not available, no way, with strcat_s instead, eliminate all warnings and errors, found still have a problem, the original is acceptmessages is not in the Telnet link, Here is my modified acceptmessages function, plus the new Get_input_cmd function

The code is as follows:

void Telnetserver::acceptmessages ()//check to see if each user has input and put their input into the message queue
{
int nmsg, LCV, Lcv2;
U_long lmsg;

for (LCV = 0; LCV < max_connects; lcv++)//LCV represents the user's label, looping over each user's information
{
Nmsg = ioctlsocket (User[lcv].suser, Fionread, &lmsg);//the ioctlsocket function controls the I/O mode of a socket.
if ((nmsg = = 0) && (lmsg > 0))
{
memcpy (User[lcv].cnote, User[lcv].cinput, use_size);

Get_input_cmd (User[lcv].suser, user[lcv].cinput);
Lcv2 = 0;
while (mmsg[lcv2].cmsg[0]! = 0) {lcv2++;} Find the first available message slot in the message queue
Mmsg[lcv2].nuser = LCV;
memset (mmsg[lcv2].cmsg, 0, use_size);
memcpy (Mmsg[lcv2].cmsg, User[lcv].cinput, use_size);

}
}
}

void Telnetserver::get_input_cmd (SOCKET sclient,char *rec)
{
Char *recvbuf = (char *) malloc (use_size);
memset (recvbuf, 0, use_size);
while (1)
{
Char Fx[buff_size]; memset (FX, 0, Buff_size);
Char *p;
p = recvbuf;
Recv (sclient, FX, sizeof (FX), 0);
Recvbuf[0] = fx[0];
p++;
/* Receive commands from the client */
while (1)
{
Recv (sclient, p, sizeof (p), 0);
if (*p = = ' \ r ') break;
else if (*p = = ' \b ')
{
Char bs[3];
Bs[0] = 46;
BS[1] = 8;
BS[2] = ' + ';
Send (Sclient, BS, sizeof (BS), 0);
p--;
}
else p++;
int STRLEN = STRLEN (RECVBUF);
printf ("Array buf:%s; Array length:%d.\n", Recvbuf, STRLEN);
}
*p = ' + '; /* At the end of the "+", the command to accept the completion! */
Break

}
memset (REC, 0, use_size);
memcpy (REC, recvbuf, use_size);
}

3) Basically the largest changes such as the above 2 functions, of course, the other does not change can be run, the code is a lot of direct numbers written, I changed to define

4) Then modify the main function of the while (1) inside the things, here according to individual needs to modify can

I can give you a reference.

while (1)
{
Accept Connection
Tserv.acceptconnects ();
Accept the incoming message
Tserv.acceptmessages ();
Get the first message
Mmsg = Tserv.getmessage ();

The message is not empty
if (mmsg.cmsg[0]! = 0)
{
cout << "Come on Baby" << mmsg.cmsg << Endl;

Get Message from client feedback
Uinfo = Tserv.getuserinfo (Mmsg.nuser);

Compare whether the input command is new
if (strcmp (Uinfo.cnote, "new") = = 0)
{
Tserv.senduser (Mmsg.nuser, "Create a successful, Huan drink came to Yesgames, please enter South, north, east, west>>");
}
else if (strcmp (Uinfo.cinput, "new") = = 0)
{
Send the message to the corresponding user
Tserv.senduser (Mmsg.nuser, "Please enter create account");
}
else if (uinfo.cnote[0]!= ' && strcmp (Uinfo.cinput, "south") = = 0)
{
Tserv.senduser (Mmsg.nuser, "You are now in the South");
}

Else if (uinfo.cnote[0]! = ' \ "&& strcmp (Uinfo.cinput," east ") = = 0)
{
Tserv.senduser (Mmsg.nuser," You are now located in the East ");
}
Else if (uinfo.cnote[0]! = ' &&strcmp ' (Uinfo.cinput, "west") = = 0)
{
Tserv.senduser ( Mmsg.nuser, "You are now in the West");
}
Else if (uinfo.cnote[0]! = ' \ "&& strcmp (Uinfo.cinput," north ") = = 0)
{
Tserv.senduser ( Mmsg.nuser, "You are now in the North");
}
Else if (strcmp (Uinfo.cinput, "North") && strcmp (Uinfo.cinput, "south") = = 0)
{
Tserv.senduser ( Mmsg.nuser, "Congratulations on completing the Novice Guide");
}

else if (strcmp (Uinfo.cinput, "West") && strcmp (Uinfo.cinput, "east") = = = 0)
{
Tserv.senduser (Mmsg.nuser, "Congratulations on completing the Beginner Guide");
}
else if (strcmp (Uinfo.cinput, "East") && strcmp (Uinfo.cinput, "west") = = 0)
{
Tserv.senduser (Mmsg.nuser, "Congratulations on completing the Beginner Guide");
}
else if (strcmp (Uinfo.cinput, "South") && strcmp (Uinfo.cinput, "north") = = 0)
{
Tserv.senduser (Mmsg.nuser, "Congratulations on completing the Beginner Guide");
}
else Tserv.senduser (mmsg.nuser, "Input error:");

}

Release the socket for the connection to the client that has been disconnected
Tserv.closeemptysockets ();
}

Hope for the needs of friends to help, but also I hope that the same as my friends in the Learning Network to Exchange learning QQ125871289, hope to add QQ when the blog park, thank you!!!!

Simple Telnet Server code under the VS platform

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.