急[求助]:VC++中的SOCKET,需要在DELPHI中使用,有几个问题不明白!(100分)

  • 主题发起人 主题发起人 ydzi_
  • 开始时间 开始时间
Y

ydzi_

Unregistered / Unconfirmed
GUEST, unregistred user!
typedef struct tagSocketItem
{
SOCKET SocketHandle;
void* SocketData; // 这里在DELPHI中怎么写?
}tSocketItem, * ptSocketItem;
const SIZEOF_SOCKETITEM = sizeof(tSocketItem);

typedef struct tagFragment
{
int FragmentSize; // Size of fragmentBuf
void* FragmentBuf;
}tFragment, * ptFragment;
const SIZEOF_FRAGMENT = sizeof(tFragment);

UINT SocketReceiveThread(LPVOID pParam) // 这里在DELPHI中怎么写?
{
char SocketReceiveBuf[1024];
int ReceivedLen, iSocketHandle;
POSITION SocketIndex;
ptSocketItem pSocketItem;
ptFragment pFragment;
CString sInfo;
BOOL AppendResult, ParseResult;
// 这里在DELPHI中是什么类型?
SOCKET m_SocketSocket;
sockaddr_in m_SockAddr;
int m_LenOfSockAddr;
CPtrList m_SocketList;
CCriticalSection m_SocketListLock;
CCriticalSection m_AppendFragmentLock, m_ParseMsgLock, m_ClearFragmentLock;
CEvent m_evtExitSocketReceiveThread;

CLogicCardManagerDlg* pDlg = (CLogicCardManagerDlg*)AfxGetApp()->GetMainWnd();



// 接受连接
iSocketHandle = accept(pDlg->m_ServerSocket, (sockaddr*)&(pDlg->m_SockAddr),
&(pDlg->m_LenOfSockAddr));
if(iSocketHandle == INVALID_SOCKET){ // accept错误
AfxEndThread(0);
return 0;
}

pSocketItem = (ptSocketItem)malloc(SIZEOF_SOCKETITEM);
if(pSocketItem == NULL){ // 无可用内存
AfxEndThread(0);
return 0;
}
ZeroMemory(pSocketItem, SIZEOF_SOCKETITEM);
pSocketItem->SocketHandle = iSocketHandle;

// 加入SocketList
pDlg->m_SocketListLock.Lock();
try{
pDlg->m_SocketList.AddTail(pSocketItem);
}
catch(...){
}
pDlg->m_SocketListLock.Unlock();

// 启动新的Socket接收线程
AfxBeginThread(SocketReceiveThread, 0);

// 接收Socket数据
while(WaitForSingleObject(pDlg->m_evtExitSocketReceiveThread, 1) != WAIT_OBJECT_0){
ReceivedLen = recv(pSocketItem->SocketHandle, SocketReceiveBuf, 1024, 0);
if(ReceivedLen <= 0){
break;
}

// 判断Socket缓冲区是否已有数据
if(pSocketItem->SocketData){
pFragment = (ptFragment)(pSocketItem->SocketData);
}
else{ // 分配新的Fragment
pFragment = (ptFragment)malloc(SIZEOF_FRAGMENT);
if(pFragment == NULL){
break;
}
ZeroMemory(pFragment, SIZEOF_FRAGMENT);

// 将SocketData与Fragment绑定
pSocketItem->SocketData = pFragment;
}

// 判断Fragment中是否已有数据
if(pFragment->FragmentBuf == NULL){
pFragment->FragmentSize = ReceivedLen;
pFragment->FragmentBuf = SocketReceiveBuf;
}
else{
// 拼接前后收到的数据, 注意线程安全
pDlg->m_AppendFragmentLock.Lock();
try{
AppendResult = pDlg->AppendFragment(pFragment, SocketReceiveBuf, ReceivedLen);
}
catch(...){
}
pDlg->m_AppendFragmentLock.Unlock();

// 拼接数据失败
if(!AppendResult){
pDlg->m_ClearFragmentLock.Lock();
try{
pDlg->ClearFragment(pFragment);
}
catch(...){
}
pDlg->m_ClearFragmentLock.Unlock();
free(pFragment);
pSocketItem->SocketData = NULL;
break;
}
}

// 解析完整的Socket消息, 注意线程安全
pDlg->m_ParseMsgLock.Lock();
try{
ParseResult = pDlg->ParseWholeMsgFromFragment(pFragment, pSocketItem->SocketHandle);
}
catch(...){
}
pDlg->m_ParseMsgLock.Unlock();

// 解析失败
if(!ParseResult){
pDlg->m_ClearFragmentLock.Lock();
try{
pDlg->ClearFragment(pFragment);
}
catch(...){
}
pDlg->m_ClearFragmentLock.Unlock();
free(pFragment);
pSocketItem->SocketData = NULL;
}
else{
if(pFragment->FragmentSize == 0){ // 所有消息处理完毕
free(pFragment);
pSocketItem->SocketData = NULL;
}
}
} // end of while...

// 关闭连接
closesocket(pSocketItem->SocketHandle);

// 释放资源
SocketIndex = pDlg->m_SocketList.Find(pSocketItem);
if(SocketIndex != NULL){
pDlg->m_SocketListLock.Lock();
try{
pDlg->m_SocketList.RemoveAt(SocketIndex);
}
catch(...){
}
pDlg->m_SocketListLock.Unlock();
free(pSocketItem);
}
AfxEndThread(0);
return 0;
}

请大侠帮忙解决一下!问题解决马上结帐,谢谢。。。
 
void* SocketData; // 这里在DELPHI中怎么写?
}tSocketItem, * ptSocketItem;
const SIZEOF_SOCKETITEM = sizeof(tSocketItem);

typedef struct tagFragment
{
int FragmentSize; // Size of fragmentBuf
void* FragmentBuf;
}tFragment, * ptFragment;
const SIZEOF_FRAGMENT = sizeof(tFragment);

UINT SocketReceiveThread(LPVOID pParam) // 这里在DELPHI中怎么写?
UNIT就是DELPHI的WORD类型,
LPVOID,*SocketData你需要使用VC++的 Go to Definition一点一点的跟踪,而且参数需要查看MSDN,找才知道。
void*应该就是Tpointer指针。指向函数结构的指针。
 
void* SocketData;
这个是无类型指针,在Delphi中的写法: SocketData: Pointer;

UINT SocketReceiveThread(LPVOID pParam) // 这里在DELPHI中怎么写?
请参考Classes单元的ThreadProc函数:
function ThreadProc(Thread: TThread): Integer;
 
SOCKET, sockaddr_in, CPtrList, CCriticalSection, CEvent这些在delphi中都是什么类型的???

accept(pDlg->m_ServerSocket, (sockaddr*)&(pDlg->m_SockAddr),
&(pDlg->m_LenOfSockAddr));??这个在delphi中怎么写?


pSocketItem = (ptSocketItem)malloc(SIZEOF_SOCKETITEM); 还有这个?


不好意思,最近没什么时间上网!

不过问题解决马上结贴,谢谢[:)]
 
高人呢?大侠呢?都到哪里去了???!!
 
用Indy吧,比这些用起来更方便

去网上随便搜下就出来了!
 
郁闷……

没有解决!

唉。。。。。。
 
后退
顶部