| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 663 人关注过本帖
标题:[求助]这些错误这么改啊,急啊,谢谢
只看楼主 加入收藏
gerrard_lu
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-4-23
收藏
 问题点数:0 回复次数:1 
[求助]这些错误这么改啊,急啊,谢谢

The thread 0x2B80 has exited with code 0 (0x0).
The thread 0x165C has exited with code 1 (0x1).

下面的程序调试时,总是出现这两个类似的提示,有人可以帮我改吗?谢谢
#include "AfxWin.h"
#include <winsock2.h >
#include <stdio.h >
#include <string.h >
#include "afxmt.h"

#define WM_THREADFINISHED WM_USER+5

#define HTTP "http://"
#define FTP "ftp://"
#define PROXYPORT 8888 //端口
#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;
struct 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; // 申明为全局变量(线程)
CEvent evt_UserToProxy;

// 取客户端数据发送到服务器
UINT UserToProxyThread(void *pParam)
{
char Buffer[BUFSIZE];
int Len;
sockaddr_in from;
SOCKET msg_socket;
int fromlen,retval;
struct SocketPair SPair;
struct 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 线 程
//AfxBeginThread(UserToProxyThread,pParam);
CWinThread *pThread;
pThread=AfxBeginThread(UserToProxyThread,pParam); //启动另一侦听.
//pThread->m_bAutoDelete=FALSE;//线程为手动删除
evt_UserToProxy.SetEvent();
::WaitForSingleObject(evt_UserToProxy,INFINITE);

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);
//pChildThread->m_bAutoDelete=FALSE;
evt_Exit.SetEvent();

// 等待或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);
::PostMessage((HWND)pParam,WM_THREADFINISHED,0,0);

return 0;
}

// 读取服务器数据,并发往客户端
UINT ProxyToServer(LPVOID pParam)
{
struct ProxyParam * pPar=(struct 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;

::WaitForSingleObject(evt_Exit,INFINITE);

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
}

::PostMessage((HWND)pParam,WM_THREADFINISHED,0,0);

//关闭套节字
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;
}
//线程退出
return 1;
}


void main()
{
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
printf("ERROR");
else
{
StartServer();
while(1)
if(getchar()=='q') break;
CloseServer();
}
}


搜索更多相关主题的帖子: 谢谢 include thread 缓冲区 
2007-04-25 15:50
sunspirit
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-10
收藏
得分:0 
2007-04-25 16:53
快速回复:[求助]这些错误这么改啊,急啊,谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.075439 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved