?? kshmain.c
字號:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <signal.h> #ifdef WIN32#include <winsock.h>#define WINSOCK_VERSION MAKEWORD(2,0) #else#include <linux/types.h>#include <sys/select.h>#include <sys/socket.h>#include <linux/in.h>#endif#include "kshmain.h"int inet_pton(int af, const char *src, void *dst);static int inet_pton4(const char *src, u_char *dst);#define EAFNOSUPPORT 47#define INADDRSZ 4#define KSH_SVR_PORT 7915int still_loop = 1;extern int errno;int ksh_connect(char * ipaddr, int port){ int socket_fd; struct sockaddr_in sin; int error = 0;#ifdef WIN32 WSADATA wsaData; if(WSAStartup(WINSOCK_VERSION,&wsaData)) { printf("Winsock init failed!"); return -1; } #endif sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr(ipaddr); sin.sin_port = htons(KSH_SVR_PORT); /* Create the socket. */ socket_fd = socket (AF_INET, SOCK_STREAM, 0); if(socket_fd <= 0){ printf("create socket failed, socket_fd = %d\n", socket_fd); return -1; } /* Connect the socket. */ error = connect (socket_fd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)); if(error){ printf("connect failed, error = %d\n", error); return -1; }else{ printf("connect ok\n"); } return socket_fd; }int main(int argc, char *argv[]){ char usr_input[MAX_CMD_LEN] = {0}; fd_set fds, stdin_fd; int socket_fd = -1, max_fd; struct timeval timeout; int res, ipaddr; char buffer[4050]; if(argc != 2) { printf("usage : ksh IP\n"); printf("example: ksh 192.168.1.1\n"); return -1; } if(inet_pton(AF_INET, argv[1], &ipaddr) <= 0 ){ printf("Invalid IP address!\n"); return -1; } FD_ZERO(&fds); FD_ZERO(&stdin_fd); socket_fd = ksh_connect(argv[1], KSH_SVR_PORT); if(socket_fd < 0){ return -1; } printf("%s", CLI_PROMPT); while(still_loop) { FD_ZERO(&fds); FD_SET(socket_fd, &fds); FD_SET(0, &stdin_fd); timeout.tv_sec = 0; timeout.tv_usec = 0; /*linux和win下的select函數(shù)區(qū)別很大 *win下的select只能用于socket fd *linux下的select能用于各種fd */ res = select(max_fd+1, &fds, NULL, NULL, &timeout); if(res > 0){ if(FD_ISSET(socket_fd, &fds)){ /*read socket*/ int len = recv(socket_fd, buffer, sizeof(buffer), 0); if(len > 0 && len < 4050){ buffer[len] = 0; printf("%s\n", buffer); }else{ printf("something error happened, exit ksh\n"); break; } printf("%s", CLI_PROMPT); } } /*判斷鍵盤是否有輸入, 串口終端怎么辦?*/ if(kbhit()){ int len = 0; usr_input[0] = '\0'; fgets(usr_input, MAX_CMD_LEN-1, stdin); //printf("usr input:%s\n", usr_input); len = strlen(usr_input); usr_input[len-1] = 0; if( (len-1) > 0){ send(socket_fd, usr_input, len, 0); }else{ printf("%s", CLI_PROMPT); } } } close(socket_fd);#ifdef WIN32 WSACleanup();#endif return 0;}/*********************************copy from inet_pton.c*********************************************//* int * inet_pton(af, src, dst) * convert from presentation format (which usually means ASCII printable) * to network format (which is usually some kind of binary format). * return: * 1 if the address was valid for the specified address family * 0 if the address wasn't valid (`dst' is untouched in this case) * -1 if some other error occurred (`dst' is untouched in this case, too) * author: * Paul Vixie, 1996. */intinet_pton(af, src, dst) int af; const char *src; void *dst;{ switch (af) { case AF_INET: return (inet_pton4(src, dst)); /* I'm lazy, comment this case AF_INET6: return (inet_pton6(src, dst)); */ default: errno = EAFNOSUPPORT; return (-1); } /* NOTREACHED */}/* int * inet_pton4(src, dst) * like inet_aton() but without all the hexadecimal and shorthand. * return: * 1 if `src' is a valid dotted quad, else 0. * notice: * does not touch `dst' unless it's returning 1. * author: * Paul Vixie, 1996. */static intinet_pton4(src, dst) const char *src; u_char *dst;{ static const char digits[] = "0123456789"; int saw_digit, octets, ch; u_char tmp[INADDRSZ], *tp; saw_digit = 0; octets = 0; *(tp = tmp) = 0; while ((ch = *src++) != '\0') { const char *pch; if ((pch = strchr(digits, ch)) != NULL) { u_int new = *tp * 10 + (pch - digits); if (new > 255) return (0); *tp = new; if (! saw_digit) { if (++octets > 4) return (0); saw_digit = 1; } } else if (ch == '.' && saw_digit) { if (octets == 4) return (0); *++tp = 0; saw_digit = 0; } else return (0); } if (octets < 4) return (0); memcpy(dst, tmp, INADDRSZ); return (1);}/*******************************copy end*****************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -