希望有心人可以帮我改一下这个程序,谢谢
#include "AfxWin.h"
#include <winsock2.h >
#include <stdio.h >
#include <string.h >
#include "afxmt.h"
#define HTTP "http://"
#define FTP "ftp://"
#define PROXYPORT 5060 //端口
#define BUFSIZE 100*1024 //缓冲区
UINT ProxyToServer(LPVOID pParam);
UINT UserToProxyThread(void *pParam);
struct SocketPair
{
SOCKET user_proxy;
SOCKET proxy_server;
BOOL IsUser_ProxyClosed;
BOOL IsProxy_ServerClosed;
};
struct ProxyParam
{
char Address[256];
HANDLE User_SvrOK;
SocketPair *pPair;
int Port;
};
SOCKET gListen_Socket; //用来侦听的SOCKET。
int StartServer() //启动服务
{
WSADATA wsaData;
sockaddr_in local;
SOCKET listen_socket;
if(::WSAStartup(0x202,&wsaData)!=0)
{
printf("\nError in Startup session.\n");
WSACleanup();
return -1;
}
//准备帧听套节字的参数
local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons(PROXYPORT);
//创建帧听套节字
listen_socket=socket(AF_INET,SOCK_STREAM,0);
if(listen_socket==INVALID_SOCKET)
{
printf("\nError in New a Socket.");
WSACleanup();
return -2;
}
//绑定帧听套节字
if(::bind(listen_socket,(sockaddr *)&local,sizeof(local))!=0)
{
printf("\n Error in Binding socket.");
WSACleanup();
return -3;
}
//建立侦听
if(::listen(listen_socket,5)!=0)
{
printf("\n Error in Listen.");
WSACleanup();
return -4;
}
gListen_Socket=listen_socket;
AfxBeginThread(UserToProxyThread,NULL); //启动侦听
return 1;
}
int CloseServer() //关闭服务
{
closesocket(gListen_Socket);
WSACleanup();
return 1;
}
//服务器IP地址
int GetAddressAndPort( char * str, char *address, int * port)
{
char buf[BUFSIZE], command[512], proto[128], *p;
sscanf(str,"%s%s%s",command,buf,proto);
p=strstr(buf,HTTP);
//HTTP
if(p)
{
p+=strlen(HTTP);
for(UINT i=0;i<strlen(p);i++)
if(*(p+i)=='/') break;
*(p+i)=0;
strcpy(address,p);
p=strstr(str,HTTP);
for(UINT j=0;j<i+strlen(HTTP);j++)
*(p+j)=' ';
*port=80;
}
else
{//FTP
p=strstr(buf,FTP);
if(!p) return 0;
p+=strlen(FTP);
for(UINT i=0;i<strlen(p);i++)
if( *(p+i)=='/') break;
*(p+i)=0;
for(UINT j=0;j<strlen(p);j++)
if(*(p+j)==':')
{
*port=atoi(p+j+1);
*(p+j)=0;
}
else *port=21;
strcpy(address,p);
p=strstr(str,FTP);
for(j=0;j<i+strlen(FTP);j++)
*(p+j)=' ';
}
return 1;
}
CEvent evt_Exit; // 申明为全局变量(线程)
// 取客户端数据发送到服务器
UINT UserToProxyThread(void *pParam)
{
char Buffer[BUFSIZE];
int Len;
sockaddr_in from;
SOCKET msg_socket;
int fromlen,retval;
SocketPair SPair;
ProxyParam ProxyP;
CWinThread *pChildThread;
fromlen=sizeof(from);
//Accept Brower 的 连 接 申 请
msg_socket=accept(gListen_Socket,(struct sockaddr*)&from,&fromlen);
// AfxBeginThread(B_P_S) 重 新 启 动 一 个B_P_S 线 程
CWinThread *pThread;
pThread=AfxBeginThread(UserToProxyThread,pParam); //启动另一侦听.
pThread->m_bAutoDelete=FALSE;//线程为手动删除
if( msg_socket==INVALID_SOCKET)
{
printf( "\nError in accept ");
return -5;
}
// 收到客户端发送的数据
SPair.IsUser_ProxyClosed=FALSE;
SPair.IsProxy_ServerClosed=TRUE;
SPair.user_proxy=msg_socket;
retval=0;
retval=recv(SPair.user_proxy,Buffer,sizeof(Buffer),0);
if(retval==SOCKET_ERROR)
{
printf("\nError Recv\n");
//fprintf(stderr,"recv() failed: error %d\n",WSAGetLastError());
printf("\nrecv() failed: error %d\n",WSAGetLastError());
if(SPair.IsUser_ProxyClosed==FALSE)
{
closesocket(SPair.user_proxy);
SPair.IsUser_ProxyClosed=TRUE;
//return 0;
}
}
if(retval==0)
{
printf("Client Close connection\n");
if(SPair.IsUser_ProxyClosed==FALSE)
{
closesocket(SPair.user_proxy);
SPair.IsUser_ProxyClosed=TRUE;
//return 0;
}
}
Len=retval;
Buffer[Len]=0;
printf("\n Received %d bytes,data[%s]from client\n",retval,Buffer);
SPair.IsUser_ProxyClosed=FALSE;
SPair.IsProxy_ServerClosed=TRUE;
SPair.user_proxy=msg_socket;
ProxyP.pPair=&SPair;
//创建事件
ProxyP.User_SvrOK=CreateEvent(NULL,TRUE,FALSE,NULL);
//分析用户链接请求 得到服务器IP地址
GetAddressAndPort( Buffer,ProxyP.Address,&ProxyP.Port);
// 启动S_P_B线 程创建线程,连接目标地址
pChildThread=AfxBeginThread(ProxyToServer,(LPVOID)&ProxyP);
// 等待或S_P_B线程连接上服务器
::WaitForSingleObject(ProxyP.User_SvrOK,60000); //等待联结
::CloseHandle(ProxyP.User_SvrOK);
// 客户端把数据传到代理,再由代理传到服务器,直到数据传输为止
while(SPair.IsProxy_ServerClosed==FALSE && SPair.IsUser_ProxyClosed==FALSE)
{
//发送到服务器
retval=0;
retval=send(SPair.proxy_server,Buffer,Len,0);
if(retval==SOCKET_ERROR)
{
printf("\n send() failed:error%d\n",WSAGetLastError());
if(SPair.IsProxy_ServerClosed==FALSE)
{
closesocket(SPair.proxy_server);
SPair.IsProxy_ServerClosed=TRUE;
}
//return 0;
continue;
}
//接受请求
retval=0;
retval=recv(SPair.user_proxy,Buffer,sizeof(Buffer),0);
if(retval==SOCKET_ERROR)
{
printf("\nError Recv\n");
//fprintf(stderr,"recv() failed: error %d\n",WSAGetLastError());
printf("\nrecv() failed: error %d\n",WSAGetLastError());
if(SPair.IsUser_ProxyClosed==FALSE)
{
closesocket(SPair.user_proxy);
SPair.IsUser_ProxyClosed=TRUE;
}
continue;
}
if(retval==0)
{
printf("Client Close connection\n");
if(SPair.IsUser_ProxyClosed==FALSE)
{
closesocket(SPair.user_proxy);
SPair.IsUser_ProxyClosed=TRUE;
}
break;
}
Len=retval;
Buffer[Len]=0;
printf("\n Received %d bytes,data[%s]from client\n",retval,Buffer);
} //End While
//关闭ProxyToServSockets套节字
if(SPair.IsProxy_ServerClosed==FALSE)
{
closesocket(SPair.proxy_server);
SPair.IsProxy_ServerClosed=TRUE;
}
//关闭ProxyToUserSockets套节字
if(SPair.IsUser_ProxyClosed==FALSE)
{
closesocket(SPair.user_proxy);
SPair.IsUser_ProxyClosed=TRUE;
}
//::WaitForSingleObject(pChildThread->m_hThread,INFINITE);
//::CloseHandle(pChildThread->m_hThread);
DWORD dwSuccess = WaitForSingleObject(evt_Exit,100000);
if (dwSuccess == WAIT_OBJECT_0) return 0 ;
return 0;
}
// 读取服务器数据,并发往客户端
UINT ProxyToServer(LPVOID pParam)
{
ProxyParam * pPar=(ProxyParam*)pParam;
char Buffer[BUFSIZE];
char *server_name= "localhost";
unsigned short port ;
int retval,Len;
unsigned int addr;
int socket_type ;
struct sockaddr_in server;
struct hostent *hp;
SOCKET conn_socket;
socket_type = SOCK_STREAM;
server_name = pPar->Address;
port = pPar->Port;
// 解析服务器域名
if (isalpha(server_name[0]))
{
hp = gethostbyname(server_name);
}
else
{
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr,4,AF_INET);
}
if (hp == NULL )
{
//fprintf(stderr,"Client: Cannot resolve address [%s]: Error %d\n",server_name,WSAGetLastError());
::SetEvent(pPar->User_SvrOK);
return 0;
}
// 新建socket并连接到服务器
memset(&server,0,sizeof(server));
memcpy(&(server.sin_addr),hp->h_addr,hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);
conn_socket = socket(AF_INET,socket_type,0);
if (conn_socket <0 )
{
//fprintf(stderr,"Client: Error Opening socket: Error %d\n",WSAGetLastError());
pPar->pPair->IsProxy_ServerClosed=TRUE;
::SetEvent(pPar->User_SvrOK);
return -1;
}
printf("Client connecting to: %s\n",hp->h_name);
if (connect(conn_socket,(struct sockaddr*)&server,sizeof(server))== SOCKET_ERROR)
{
//fprintf(stderr,"connect() failed: %d\n",WSAGetLastError());
pPar->pPair->IsProxy_ServerClosed=TRUE;
::SetEvent(pPar->User_SvrOK);
return -1;
}
pPar->pPair->proxy_server=conn_socket;
pPar->pPair->IsProxy_ServerClosed=FALSE;
::SetEvent(pPar->User_SvrOK);
//服务器把数据传到代理,再由代理传到客户端直到数据传输完毕
while(pPar->pPair->IsProxy_ServerClosed==FALSE&&pPar->pPair->IsUser_ProxyClosed==FALSE)
{
//接受服务器数据
retval=0;
retval = recv(conn_socket,Buffer,sizeof (Buffer),0 );
if (retval == SOCKET_ERROR )
{
//fprintf(stderr,"recv() failed: error %d\n",WSAGetLastError());
printf("\nrecv() failed: error %d\n",WSAGetLastError());
closesocket(conn_socket);
pPar->pPair->IsProxy_ServerClosed=TRUE;
break;
}
Len=retval;
if(retval == 0)
{
printf("Server closed connection\n");
closesocket(conn_socket);
freopen("1.txt","w",stdout);
pPar->pPair->IsProxy_ServerClosed=TRUE;
printf("%d\n",pPar->pPair->IsProxy_ServerClosed);
fclose(stdout);
break;
}
//发送到客户端
retval=0;
retval = send(pPar->pPair->user_proxy,Buffer,Len,0);
if (retval == SOCKET_ERROR)
{
//fprintf(stderr,"send() failed: error %d\n",WSAGetLastError());
printf("send() failed: error %d\n",WSAGetLastError());
closesocket(pPar->pPair->user_proxy);
pPar->pPair->IsUser_ProxyClosed=TRUE;
break;
}
#ifdef _DEBUG
Buffer[Len]=0;
printf("Received %d bytes, data [%s] from server\n",retval,Buffer);
#endif
}
//关闭套节字
if(pPar->pPair->IsProxy_ServerClosed==FALSE)
{
closesocket(pPar->pPair->proxy_server);
pPar->pPair->IsProxy_ServerClosed=TRUE;
}
if(pPar->pPair->IsUser_ProxyClosed==FALSE)
{
closesocket(pPar->pPair->user_proxy);
pPar->pPair->IsUser_ProxyClosed=TRUE;
}
//线程退出
SetEvent(evt_Exit);
return 1;
}
void main()
{
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
printf("ERROR");
else
{
StartServer();
while(1)
if(getchar()=='q') break;
CloseServer();
}
}
[此贴子已经被作者于2007-4-23 16:06:15编辑过]