您的位置:首页 > 其它

第三部分-使用高速SPI以太网控制芯片W5200登录Telnet服务器

2012-01-13 13:11 477 查看
最近我们给大家介绍了很多Telnet服务器的知识,在您尽情享受Telnet所带给您的便捷的同时相信您已经对Telnet了解不少了,您有什么意见和学习心得吗?大家一起讨论吧。今天我将继续为大家献上我的一点学习心得,给出一些基于IAR编译器的代码例子,希望对您有用。

4执行代码

这个部分将复习加载在W5200E01-M3远程服务器的示例代码。更多有关基于用TCP实现远程信息交换的细节,请查阅“How to implement TCP for W7100”文档。这个部分将进一步解释在main()功能中的TELNETS的功能;但对与MCU初始化和网络初始化的代码不做解释。

4.1 TELNETS()功能

TELNETS()功能是执行远程服务器的基本功能,并且它是基于TCP来建立的SOCKET和等待客户机的连接。在于客户机建立连接之后,调用init_telopt()来选择远程登录方式。为了进入命令模式,需要调用tel_input()。下一小节将进一步说明init_telopt()和tel_input()的作用。远程服务使用socket可以从0到7。但端口号必须设置为23,因为根据远程服务标准,远程服务的专用端口号为23。





4.2 Init_telopt()和sendlAC()功能

Init_telop()是用来决定远程服务器与客户机之间的选择方式。由于在测试程序中是有ECHO选项,所以用户只能用WILL命令来设置ECHO选项。正如在第二部分中所解释到的那样,当发送一个控制字符时,IAC(0xFF)也必须要一起发送。SendlAC()是用于发送IAC和控制字符的。





4.3 Tel_input()的功能

Tel_input()是用来处理在远程终端命令的功能。对于每一条指令和处理方式,请参考Table 3.1。









4.4 Logic()功能

当用户登录远程服务器时,Logic()是用来检查用户ID信息和密码。如果所输入的信息和已有的登录信息相比配的话,用户将成功连接到远程服务器。但是,如果与登录信息不相同,则会要求用户重新输入ID和密码。
uint8 telnet_ID[] = {

      "wiznet"
};
uint8 telnet_PW[] = {
"0000"
};
void login(SOCKET s)

{
if(user_state == USERNAME) {           /* input the client ID and Password */
strcpy((char *)user_name, data_buf);

sprintf(buf, "Password : ");
send(s, (uint8 const *)buf, strlen(buf), FALSE);

user_state = PASSWORD;
return;
} else if(user_state == PASSWORD) {
strcpy((char *)user_password, data_buf);

/*Check the client ID and Password*/
if(!(strcmp((char const *)user_name, (char const *)telnet_ID)) && !(strcmp((char const
*)user_password, (char const *)telnet_PW))) {
sprintf(buf, "\r\n=======================");
send(s, (uint8 const *)buf, strlen(buf), FALSE);
sprintf(buf, "\r\nSuccessfully connected!\
\r\nImplemented Commands : HELP, GET LED, LED3 ON/OFF, LED4 ON/OFF, EXIT\r\n"); send(s, (uint8 const *)buf, strlen(buf), FALSE);
sprintf(buf, "=======================\r\n"); send(s, (uint8 const *)buf, strlen(buf), FALSE); user_state = LOGIN;
return;
} else {
/* If the ID or Password incorrect, print error msg */
sprintf(buf, "\r\nID or Password incorrect!\r\n"); send(s, (uint8 const *)buf, strlen(buf), FALSE); sprintf(buf, "ID : ");
send(s, (uint8 const *)buf, strlen(buf), FALSE); user_state = USERNAME;
return;
}
}
}        /* End login function */

 4.5 Proc_command()功能
Proc_command()是用来处理在tel_input()程序中的输入的命令。它详细说明了有关“HELP,GET LED,LED0 ON/OFF,LED1 ON/OFF,LED2 ON/OFF”的命令。对于没有定义的命令,则会出现“BAD COMMAND”字样的信息。
void proc_command(SOCKET s)
{
char **cmdp;

char *cp;
char *help = {"HELP: Show all available commands\ \r\nGET LED: Show all LED status\
\r\nLED3 ON/OFF: Turn ON/OFF the LED3\

\r\nLED4 ON/OFF: Turn ON/OFF the LED4\
\r\nEXIT: Exit from W5200 TELNET server\r\n"}; /* command HELP : Message */

for(cp = data_buf; *cp != '\0';    cp++){
*cp = tolower(*cp);        /* Translate big letter to small letter */
}

if(*data_buf != '\0') {
/* Find the input command in table; if it isn't there, return Syntax Error */
for(cmdp = commands; *cmdp != NULL; cmdp++) {
if(strncmp(*cmdp, data_buf, strlen(*cmdp)) == 0) break;
}

if(*cmdp == NULL) {
printf("NULL command\r\n");
sprintf(buf, "%s : BAD command\r\n", data_buf);

send(s, (uint8 const *)buf, strlen(buf), FALSE);

return;
}

switch(cmdp - commands) {

case HELP_CMD :       /* Process HELP command */
printf("HELP_CMD\r\n");
sprintf(buf, help);

send(s, (uint8 const *)buf, strlen(buf), FALSE); break;

case GET_LED_CMD :         /* Process GET LED command */
printf("GET_LED_CMD\r\n");
sprintf(buf, "LED%d is %s\r\n", 3, GPIO_ReadOutputDataBit(GPIOA, LED3) ? "OFF" : "ON"); send(s, (uint8 const *)buf, strlen(buf), FALSE);
sprintf(buf, "LED%d is %s\r\n", 4, GPIO_ReadOutputDataBit(GPIOA, LED4) ? "OFF" : "ON"); send(s, (uint8 const *)buf, strlen(buf), FALSE);
break;

case LED3_ON_CMD :         /* Process LED3 ON command */
printf("LED3_ON_CMD\r\n");
sprintf(buf, "Turn ON the LED3\r\n");
send(s, (uint8 const *)buf, strlen(buf), FALSE);

GPIO_ResetBits(GPIOA, LED3); // led3 on

break;

case LED4_ON_CMD :         /* Process LED4 ON command */
printf("LED4_ON_CMD\r\n");
sprintf(buf, "Turn ON the LED4\r\n");
send(s, (uint8 const *)buf, strlen(buf), FALSE);

GPIO_ResetBits(GPIOA, LED4); // led4 on

break;

case LED3_OFF_CMD :        /* Process LED3 OFF command */
printf("LED3_OFF_CMD\r\n");
sprintf(buf, "Turn OFF the LED3\r\n");
send(s, (uint8 const *)buf, strlen(buf), FALSE);

GPIO_SetBits(GPIOA, LED3); // led3 off

break;

case LED4_OFF_CMD :        /* Process LED4 OFF command */
printf("LED4_OFF_CMD\r\n");
sprintf(buf, "Turn OFF the LED4\r\n");
send(s, (uint8 const *)buf, strlen(buf), FALSE);

GPIO_SetBits(GPIOA, LED4); // led4 off

break;

case EXIT_CMD :       /* Process EXIT command */
printf("EXIT command\r\n");
sprintf(buf, "EXIT command\r\nGood bye!\r\nLogout from W5200 TELNET\r\n"); send(s, (uint8 const *)buf, strlen(buf), FALSE);
close(s);
user_state = LOGOUT;

break;
default :

break;
}
}
}        /* End proc_command function */

         
4.6 Willopt(),wontopt(),doopt()和dontopt()功能
Willopt(),wontopt(),doopt()还有dontopt()是用于远程服务选项协议的命令。它需要sockets和相应选项作为输入参数。更多信息可以参考Table2.1和Table2.2。
void willopt(SOCKET s, uint16 opt)

{
int ack;
printf("recv: will");
if(opt <= NOPTIONS) {
printf("%s\r\n", tel_options[opt]);

} else {
printf("%u\r\n", opt);
}

switch(opt) {
case TN_TRANSMIT_BINARY :

case TN_ECHO :
case TN_SUPPRESS_GA :
ack = DO;        /* If receive 'WILL' and it has TN_SUPPRESS_GA option, transmit 'DO' */

break;
default :
ack = DONT;        /* Refuse other commands which not defined */
}
sendIAC(s, ack, opt);
}        /* End willopt function */

void wontopt(SOCKET s, uint16 opt)

{
printf("recv: wont");

if(opt <= NOPTIONS) {
printf("%s\r\n", tel_options[opt]);

} else {
printf("%u\r\n", opt);
}

switch(opt) {
case TN_TRANSMIT_BINARY :
case TN_ECHO :

case TN_SUPPRESS_GA :
if(remote[opt] == 0) {
remote[opt] = 1;
sendIAC(s, DONT, opt);
}
break;
}

/* If receive WONT command with TN_SUPPRESS_GA option */

/* Set the TN_SUPPRESS_GA option */
/* Send DONT command with TN_SUPPRESS_GA */

}        /* End wontopt function */

void doopt(SOCKET s, uint16 opt)

{
printf("recv: do ");
if(opt <= NOPTIONS) {
printf("%s\r\n", tel_options[opt]);

} else {
printf("%u\r\n", opt);
}

switch(opt) {

case TN_SUPPRESS_GA :       /* If receive DO command with TN_SUPPRESS_GA option */
sendIAC(s, WILL, opt);         /* Send WILL command with TN_SUPPRESS_GA */
break;
case TN_ECHO :         /* If receive DO command with TN_ECHO option */
sprintf(buf, "WELCOME!\r\nID : ");
send(s, (uint8 const *)buf, strlen(buf), FALSE);
break;
default :
sendIAC(s, WONT, opt);
}
}        /* End doopt function */

void dontopt(uint16 opt)

{
printf("recv: dont ");

if(opt <= NOPTIONS) {
printf("%s\r\n", tel_options[opt]);

} else {
printf("%u\r\n", opt);
}

switch(opt) {
case TN_TRANSMIT_BINARY :
case TN_ECHO :

case TN_SUPPRESS_GA :
if(remote[opt] == 0) {
remote[opt] = 1;
}
break;
}
}      /* End dontopt function */

/* If receive DONT command with TN_SUPPRESS_GA option */

相关博文信息请参阅:http://blog.csdn.net/wiznet2012/article/details/7192604

如果您有什么疑问请留言或者来信:wiznetbj@wiznettechnology.com,
Tel: 86-10-84539974(转166),希望本篇文章可以给您带来帮助,谢谢。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息