|
发表于 2003-8-22 09:55:56
|
显示全部楼层
这是我修改后的代码,可以访问linuxsir。
[php]
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define LISTENIEPORT 3333
#define SERVERPORT 80
#define TRANSPORT 3334
#define BACKLOG 10
#define SERVERADDR "211.93.98.22"//www.linuxsir.cn
struct sockaddr_in server_addr;
void proxy(int usersock);
main()
{
int listeniesock,transock;//listenie and transinfo socket
struct sockaddr_in local_addr;//local IP information
struct sockaddr_in remote_addr;//remote client IE IP information
// struct sockaddr_in server_addr;//remote server IP information
//fill the remote server address
bzero((char *)&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(SERVERPORT);
//server_addr.sin_addr.s_addr=htonl(SERVERADDR);
inet_aton(SERVERADDR, &server_addr.sin_addr);
//create listenie socket
if((listeniesock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket create error!");
exit(1);
}
printf("create socket OK!\n");
bzero((char *)&local_addr,sizeof(local_addr));
printf("bzero OK!\n");
local_addr.sin_family=AF_INET;
printf("local family AF_INET OK!\n");
local_addr.sin_port=htons(LISTENIEPORT);
printf("local port OK!\n");
//local_addr.sin_addr.s_addr=inet_addr("182.4.1.26");
local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
printf("local address is %s\n",inet_ntoa(local_addr.sin_addr));
//bzero(&(local_addr.sin_zero),8);
//memset(&(local_addr.sin_zero),0,sizeof(local_addr.sin_zero));
//bind listenie socket
//printf("bzero OK!");
if((bind(listeniesock,(struct sockaddr *)&local_addr,sizeof(struct sockaddr_in)))<0)
{
printf("error!\n");
perror("bind error!\n");
exit(1);
}
printf("bind socket OK!\n");
//listen IE request
if(listen(listeniesock,BACKLOG)==-1)
{
perror("listen error!\n");
exit(1);
}
printf("listening......\n");
while(1)
{
int sin_size=sizeof(remote_addr);
if((transock=accept(listeniesock,(struct sockaddr *)&remote_addr,&sin_size))==-1)
{
perror("accept error!\n");
continue;
}
// printf("accept one connet!\n");
printf("recieved a connection from %s\n",inet_ntoa(remote_addr.sin_addr));
if(!fork())
{
//child process code segment
close(listeniesock);
proxy(transock);
printf("child process success!\n");
exit(0);
}
close(transock);
}
}
void proxy(int usersock)
{
int connectsock;//connect server socket
int connstat;//the connection status
int iolen;//the length of write or read data
char buf[2048];//the buffer
fd_set rdfdset;
if((connectsock=socket(AF_INET,SOCK_STREAM,0))==-1)perror("connect socket create error!\n");
connstat=connect(connectsock,(struct sockaddr *)&server_addr,sizeof(server_addr));
switch(connstat)
{
case 0:break;
case ETIMEDOUT:
case ECONNREFUSED:
case ENETUNREACH:
close(usersock);
perror("connect error!\n");
exit(1);
break;
//default<img src="images/smilies/tongue.gif" border="0" alt="">error("failed to connect to host!\n");
}
while(1)
{
FD_ZERO(&rdfdset);
FD_SET(usersock,&rdfdset);
FD_SET(connectsock,&rdfdset);
if(select(FD_SETSIZE,&rdfdset,NULL,NULL,NULL)<0)perror("select failed!\n");
if(FD_ISSET(usersock,&rdfdset))
{
if((iolen=read(usersock,buf,sizeof(buf)))<=0)break;
write(connectsock,buf,iolen);
}
if(FD_ISSET(connectsock,&rdfdset))
{
if((iolen=read(connectsock,buf,sizeof(buf)))<=0)break;
write(usersock,buf,iolen);
}
}
close(connectsock);
close(usersock);
}
[/php] |
|