设为首页 加入收藏

TOP

Linux下的lcx(一)
2014-11-24 02:45:47 来源: 作者: 【 】 浏览:5
Tags:Linux lcx

以下是代码片段:


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define VERSION "2.2"
#define TIMEOUT 300
#define max(a,b) (a)>(b) (a):(b)
#define MAXSIZE 10240
#define HOSTLEN 40
#define CONNECT_NUMBER 5



void usage(char *s);
void transdata(int fd1,int fd2);
void closeallfd();
void makelog(char *buffer,int length);
int testifisvalue(char *str);
int bind2conn(int port1,char *host,int port2);
int bind2bind(int port1,int port2);
int conn2conn(char *host1,int port1,char *host2,int port2);
int create_socket();
int create_serv(int sockfd,int port);
int client_connect(int sockfd,char* server,int port);



extern int errno;
FILE *fp;


main(int argc,char **argv)
{
char **p;
char host1[HOSTLEN],host2[HOSTLEN];
int port1=0,port2=0,method=0;
int length;
char *logfile=NULL;


p=argv;
memset(host1,0,HOSTLEN);
memset(host2,0,HOSTLEN);
while(*p)
{
if(strcmp(*p,"-v")==0)
{
printf("Socket data transport tool.\r\nVersion:%s\r\n",VERSION);
p++;
continue;
}
if(strcmp(*p,"-h1")==0)
{
if(testifisvalue(*(p+1))==1)
{
length=(strlen(*(p+1))>HOSTLEN-1) HOSTLEN-1:strlen(*(p+1));
strncpy(host1,*(++p),length);
}
p++;
continue;
}
if(strcmp(*p,"-h2")==0)
{
if(testifisvalue(*(p+1))==1)
{
length=(strlen(*(p+1))>HOSTLEN-1) HOSTLEN-1:strlen(*(p+1));
strncpy(host2,*(++p),length);
}
p++;
continue;
}
if(strcmp(*p,"-p1")==0)
{
if(testifisvalue(*(p+1))==1)
port1=atoi(*(++p));
p++;
continue;
}
if(strcmp(*p,"-p2")==0)
{
if(testifisvalue(*(p+1))==1)
port2=atoi(*(++p));
p++;
continue;
}
if(strcmp(*p,"-m")==0)
{
if(testifisvalue(*(p+1))==1)
method=atoi(*(++p));
p++;
continue;
}
if(strcmp(*p,"-log")==0)
{
if(testifisvalue(*(p+1))==1)
logfile=*(++p);
else
{
printf("[ERROR]:must supply logfile name\r\n");
exit(0);
}
p++;
continue;
}
p++;
}
signal(SIGCLD,SIG_IGN);
signal(SIGINT,&closeallfd);
if(logfile !=NULL)
{
fp=fopen(logfile,"a");
if(fp == NULL )
{
perror("open logfile");
exit(0);
}
}
makelog("######################## start ################\r\n",49);
switch(method)
{
case 0:
usage(argv[0]);
break;
case 1:
if((port1==0) || (port2==0))
{
printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
break;
}
if(strlen(host2)==0)
{
printf("[ERROR]:must supply HOST2.\r\n");
break;
}
bind2conn(port1,host2,port2);
break;
case 2:
if((port1==0) || (port2==0))
{
printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
break;
}
bind2bind(port1,port2);
break;
case 3:
if((port1==0) || (port2==0))
{
printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
break;
}
if(strlen(host1)==0)
{
printf("[ERROR]:must supply HOST1.\r\n");
break;
}
if(strlen(host2)==0)
{
printf("[ERROR]:must supply HOST2.\r\n");
break;
}
conn2conn(host1,port1,host2,port2);
break;
default:
usage(argv[0]);
}
closeallfd();
}


int testifisvalue(char *str)
{
if(str == NULL ) return(0);
if(str[0]=='-') return(0);
return(1);
}


void usage(char *s)
{
printf("Socket data transport tool\r\n");
printf("by bkbll(bkbll@cnhonker.net)\r\n\r\n");
printf("Usage:%s -m method [-h1 host1] -p1 port1 [-h2 host2] -p2 port2 [-v] [-log filename]\r\n",s);
printf(" -v: version\r\n");
printf(" -h1: host1\r\n");
printf(" -h2: host2\r\n");
printf(" -p1: port1\r\n");
printf(" -p2: port2\r\n");
printf(" -log: log the data\r\n");
printf(" -m: the action method for this tool\r\n");
printf(" 1: listen on PORT1 and connect to HOST2:PORT2\r\n");
printf(" 2: listen on PORT1 and P

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java运行时异常 下一篇Java堆和栈的区别

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: