Board index » cppbuilder » TCP TServer- and TClientSocket
Kresten Tolstrup
![]() CBuilder Developer |
Kresten Tolstrup
![]() CBuilder Developer |
TCP TServer- and TClientSocket2006-11-17 02:58:29 PM cppbuilder89 Hello I'm using the TServer- and TClientSocket classes to make en Ethernet connection. I'm using the SendText and ReceiveText metodes, and there is communication. But if I sent e.g 200 packets, as fast as possible, then they are not all received, I have to put in a large delay about 100-150msec between each transmission, if all the packet shall be received, can that be true, or what shall I do? Best Regards Kresten Tolstrup |
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2006-11-17 04:22:17 PM
Re:TCP TServer- and TClientSocket
"Kresten Tolstrup" < XXXX@XXXXX.COM >wrote in message
QuoteI'm using the SendText and ReceiveText metodes Quoteif I sent e.g 200 packets, as fast as possible, then they are not all reading the packets properly in the first place. Please show your actual code. Are you taking into account that packets may be split and/or merged together? I guess would be no. QuoteI have to put in a large delay about 100-150msec between each transmission Gambit |
Kresten Tolstrup
![]() CBuilder Developer |
2006-11-18 03:57:12 AM
Re:TCP TServer- and TClientSocket
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote
string strMail = Socket->ReceiveText().c_str(); if (strMail.size()>0) { m_pRxMsgSynchronizer->BeginWrite(); conStoredRxMsg.push(strMail); m_pRxMsgSynchronizer->EndWrite(); } Best Regards Kresten Tolstrup {smallsort} |
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2006-11-18 05:43:57 AM
Re:TCP TServer- and TClientSocket
"Kresten Tolstrup" < XXXX@XXXXX.COM >wrote in message
QuoteHere is the code, it is in the ClientSocketRead metode. The received make changes to your protocol, then I STRONGLY encourage you to send the string length along with the string data. That way, the receive can determine ahead of time exactly how many bytes to read. Please go to www.deja.com and search through the newsgroup archives. This topic has been discussed in detail MANY MANY times before, with full code samples provided. Gambit |
Kresten Tolstrup
![]() CBuilder Developer |
2006-11-19 06:46:10 PM
Re:TCP TServer- and TClientSocketQuotePlease show ALL of the reading/writing code. Again, I STRONGLY discourage //--------------------------------------------------------------------------- #pragma hdrstop #include <assert.h> #include "dpRadioTCPServer.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" const int ciNoIndexFound = 10000; using namespace std; //--------------------------------------------------------------------------- __fastcall TfmRadioTCPServer::TfmRadioTCPServer(int iIPPortNo, TComponent* Owner) : TForm(Owner), m_ulLastSendTime(0) { m_pClientSynchronizer = new TMultiReadExclusiveWriteSynchronizer; m_pRxMsgSynchronizer = new TMultiReadExclusiveWriteSynchronizer; m_IPSynchronizer = new TMultiReadExclusiveWriteSynchronizer; m_clIP.strHost = ""; m_clIP.stripAddr = ""; m_clIP.iIPPortNo = iIPPortNo; ServerSocket->Port = iIPPortNo; ServerSocket->Open(); } //--------------------------------------------------------------------------- __fastcall TfmRadioTCPServer::~TfmRadioTCPServer() { ServerSocket->Close(); delete m_pClientSynchronizer; delete m_pRxMsgSynchronizer; delete m_IPSynchronizer; delete ServerSocket; } //--------------------------------------------------------------------------- //A client is connected to the server void __fastcall TfmRadioTCPServer::ServerSocketClientConnect( TObject *Sender, TCustomWinSocket *Socket) { TdpIP clClient(Socket->RemoteAddress.c_str(), Socket->RemotePort); clClient.strHost = Socket->RemoteHost.c_str(); m_pClientSynchronizer->BeginWrite(); m_conClientConnected.push_back(clClient); m_pClientSynchronizer->EndWrite(); m_IPSynchronizer->BeginWrite(); m_clIP.strHost = Socket->LocalHost.c_str(); m_clIP.stripAddr = Socket->LocalAddress.c_str(); m_clIP.iIPPortNo = Socket->LocalPort; m_IPSynchronizer->EndWrite(); } //--------------------------------------------------------------------------- TdpIP TfmRadioTCPServer::GetThisIP() { TdpIP clIP; m_IPSynchronizer->BeginRead(); clIP = m_clIP; m_IPSynchronizer->EndRead(); return clIP; } //--------------------------------------------------------------------------- bool TfmRadioTCPServer::GetConnected(tconClientConnected& conClientConnected) { bool bReturn = false; m_pClientSynchronizer->BeginRead(); if (m_conClientConnected.size()>0) { conClientConnected = m_conClientConnected; bReturn = true; } m_pClientSynchronizer->EndRead(); return bReturn; } //--------------------------------------------------------------------------- //A message sent from the client, is received here void __fastcall TfmRadioTCPServer::ServerSocketClientRead(TObject *Sender, TCustomWinSocket *Socket) { char *pcData = NULL; try { int iByte = Socket->ReceiveLength(); pcData = new char[iByte]; if (iByte>0) { iByte = Socket->ReceiveBuf(pcData, iByte); string strMsg(pcData, iByte); if (strMsg.size()>0) { m_pRxMsgSynchronizer->BeginWrite(); m_conStoredRxMsg.push(strMsg); m_pRxMsgSynchronizer->EndWrite(); } } } catch(...) { assert(0); } if (pcData != NULL) { delete pcData; } } //------------------------------------------------------------------------------ bool TfmRadioTCPServer::GetMail(std::string &strMail) { bool bReturn = false; m_pRxMsgSynchronizer->BeginWrite(); if (m_conStoredRxMsg.size()>0) { strMail = m_conStoredRxMsg.front(); m_conStoredRxMsg.pop(); bReturn = true; } m_pRxMsgSynchronizer->EndWrite(); return bReturn; } //--------------------------------------------------------------------------- void __fastcall TfmRadioTCPServer::ServerSocketClientDisconnect( TObject *Sender, TCustomWinSocket *Socket) { DisConnectSocket(Socket); } //--------------------------------------------------------------------------- void __fastcall TfmRadioTCPServer::ServerSocketClientError( TObject *Sender, TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode) { ErrorCode = 0; DisConnectSocket(Socket); } //--------------------------------------------------------------------------- void __fastcall TfmRadioTCPServer::DisConnectSocket(TCustomWinSocket *Socket) { TdpIP Client(Socket->RemoteAddress.c_str(), Socket->RemotePort); TdpIP ReadClient; m_pClientSynchronizer->BeginWrite(); std::vector<TdpIP>::iterator itClient; for (itClient = m_conClientConnected.begin(); itClient != m_conClientConnected.end(); itClient++) { ReadClient = (*itClient); if (Client.stripAddr == ReadClient.stripAddr && Client.iIPPortNo == ReadClient.iIPPortNo) { m_conClientConnected.erase(itClient); break; } } m_pClientSynchronizer->EndWrite(); } //--------------------------------------------------------------------------- int TfmRadioTCPServer::SendMail(TdpIP clRX, std::string strTxMsg) { unsigned long ulMinTimeBetweenTx = 100; int iByte=0; try { int iIndex = GetConnectIndex(&clRX); if (iIndex != ciNoIndexFound) { unsigned long ulDiffTime = GetTickCount() - m_ulLastSendTime; if (ulDiffTime < ulMinTimeBetweenTx) { Sleep(ulMinTimeBetweenTx - ulDiffTime); } for (int i=0; i<3; i++) { char *pcData; pcData = new char[strTxMsg.size()]; strTxMsg.copy(pcData, strTxMsg.size()); iByte = ServerSocket->Socket->Connections[iIndex]->SendBuf(pcData, strTxMsg.size()); m_ulLastSendTime = GetTickCount(); if (iByte != 0) { break; } Sleep(200); } if (iByte == 0) { assert(0); } } } catch(...) { assert(0); } return iByte; } //--------------------------------------------------------------------------- int TfmRadioTCPServer::GetConnectIndex(TdpIP *pclIP) { int iReturn = ciNoIndexFound; for (int iIndex=0; iIndex<ServerSocket->Socket->ActiveConnections; iIndex++) { if ((ServerSocket->Socket->Connections[iIndex]->RemoteAddress == pclIP->stripAddr.c_str()) || (ServerSocket->Socket->Connections[iIndex]->RemoteHost == pclIP->strHost.c_str())) { iReturn=iIndex; break; } } return iReturn; } //------------------------------------------------------------------------------ void __fastcall TfmRadioTCPServer::ServerSocketAccept(TObject *Sender, TCustomWinSocket *Socket) { // } //------------------------------------------------------------------------------ Best Regards Kresten Tolstrup |
Bob Gonder
![]() CBuilder Developer |
2006-11-20 10:35:11 PM
Re:TCP TServer- and TClientSocket
Kresten Tolstrup wrote:
QuoteI have tried to search the newgroup, but without any lock, Other links also discuss your problems. |