int nLength=sizeof(struct sockaddr_in); switch(WSAGETSELECTEVENT(Message.LParam)) { case FD_READ: switch(m_Protocol) { case TCP: nLen=recv(m_Socket,buf,4096,0); if(nLen>0){ buf[nLen]='/0'; TxtEdit->Text="Received Length:"+String(nLen)+"/r/n"+StrPas(buf); } break; case UDP: nLen=recvfrom(m_Socket,buf,4096,0,(struct sockaddr*)&from,&nLength); if(nLen>0){ buf[nLen]='/0'; TxtEdit->Text="Received Length:"+String(nLen)+"/r/n"+StrPas(buf); } break; } break; case FD_CLOSE: closesocket(m_Socket); break; } }
void __fastcall TLANForm::Button1Click(TObject *Sender) { char szTmp[256],buf[4096]; int nSize=0; UINT m_Port; AnsiString addr; addr=Addr->Text.Trim(); if(addr.IsEmpty()){ ::MessageBox(0,"Please enter the server ip!","Error",MB_OK+MB_ICONERROR);
return; } unsigned long nAddr=inet_addr(addr.c_str()); if(nAddr==INADDR_NONE){ ::MessageBox(0,"Bad Internet IP!","Error",MB_OK+MB_ICONERROR); return;}
try { m_Port=Port->Text.ToInt(); } catch(Exception &e) { ::MessageBox(0,e.Message.c_str(),"Error",MB_OK+MB_ICONERROR); return; } switch(Prot->ItemIndex) { case 0: m_Protocol=TCP; break; case 1: m_Protocol=UDP; break; } if(TxtEdit->Text.IsEmpty()){ ::MessageBox(0,"Please enter the text you want to send!","Error",MB_OK+MB_ICONERROR); return;}
//Initialize Winsocket WSAData wsaData; ::ZeroMemory(&wsaData,sizeof(WSAData)); Word version=MAKEWORD(2,0); if(::WSAStartup(version,&wsaData)){ sprintf(szTmp,"Failed to initial winsock enviroment!,error no:%d",::WSAGetLastError()); return;}
//OBTain the active connection char ComputerName[255]; gethostname(ComputerName,255); struct hostent* he=gethostbyname(ComputerName);