09/08/2010, 10:26
|
| | Fecha de Ingreso: agosto-2009
Mensajes: 85
Antigüedad: 15 años, 3 meses Puntos: 0 | |
Problema Multi Thread y Server Socket Borland C++ 6 Hola, estoy comunicando dos aplicaciones por medio de Socket, tengo un ServerSocket en C++ (Borland 6) que por cada conexión recibe un mensaje desde un Cliente Socket en Foxpro 6, mi problema es que no he logrado comunicar FoxPro con C++, alguna idea?
Dejo los códigos: Cliente Socket FoxPro 6
Código:
ThisForm.tcpClient.Object.RemoteHost = "172.16.24.128"
ThisForm.tcpClient.Object.RemotePort = 23
IF ThisForm.tcpClient.Object.State = 0
*WAIT WINDOW "Socket State 0: Cerrado!" TIMEOUT 1
ThisForm.tcpClient.Object.Connect()
IF ThisForm.tcpClient.Object.State = 7
WAIT WINDOW "Socket State 7: RE-Conectado OK" TIMEOUT 1
ENDIF
ENDIF
IF ThisForm.tcpClient.Object.State = 6
WAIT WINDOW "Socket State 6: Conectando..." TIMEOUT 1
ENDIF
IF ThisForm.tcpClient.Object.State = 7
*WAIT WINDOW "Socket State 7: Conectado OK" TIMEOUT 1
ThisForm.tcpClient.Object.SendData(This.Value)
WAIT WINDOW "Mensaje Enviado OK" TIMEOUT 1
*ThisForm.tcpClient.Object.Close()
ENDIF
IF ThisForm.tcpClient.Object.State = 9
WAIT WINDOW "Socket State 9: Error" TIMEOUT 1
ENDIF
ServerMain.cpp
Código:
//---------------------------------------------------------------------------
// ***************** ServerMain.cpp *****************
//---------------------------------------------------------------------------
// Requires: TServerSocket, TMemo
#include <vcl.h>
#pragma hdrstop
#include "ServerMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
// Wait 60 seconds for client
const int CLIENTWAITTIME = 60000;
// Size of buffer for reading/writing text across socket connection
const int BUFFERSIZE = 32;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Instead of using a client socket component that you place in your application
// from the Component palette, the server client thread must use the TServerClientWinSocket
// object that is created when the listening server socket accepts a client connection.
// This is available as the public ClientSocket property. In addition, you can use the
// protected HandleException method rather than writing your own thread-safe exception
// handling.
void __fastcall TMyServerThread::ClientExecute(void)
{
// make sure connection is active
while (!Terminated && ClientSocket->Connected)
{
try
{
// Now, use TWinSocketStream to read or write information
// over a blocking socket connection
TWinSocketStream *pStream = new TWinSocketStream(ClientSocket, CLIENTWAITTIME);
try
{
char buffer[BUFFERSIZE];
memset( buffer, 0, sizeof(buffer) );
// give the client 60 seconds to start writing
if (pStream->WaitForData(CLIENTWAITTIME))
{
if (pStream->Read(buffer, sizeof(buffer)) == 0)
// (if can't read in 60 seconds) than close the connection
ClientSocket->Close();
else
{
// Client to Server test text
Form1->Memo1->Lines->Add(AnsiString("(Client) ") +AnsiString(buffer) );
// Back again to Client
pStream->Write( buffer, sizeof(buffer));
}
// ...
// Process requests here.
// ...
}
else
ClientSocket->Close();
}
__finally
{
delete pStream;
}
}
catch (...)
{
HandleException();
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket, TServerClientThread *&SocketThread)
{
SocketThread = new TMyServerThread(false, ClientSocket);
}
//---------------------------------------------------------------------------
// *******************************************************
//---------------------------------------------------------------------------
ServerMain.h
Código:
//---------------------------------------------------------------------------
// ****************** ServerMain.h ******************
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef ServerMainH
#define ServerMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ScktComp.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TServerSocket *ServerSocket1;
TMemo *Memo1;
void __fastcall ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket, TServerClientThread *&SocketThread);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Threads for server connections are descendants of TServerClientThread.
// Thus, you may not use the New Thread object dialog.
// Instead, declare your thread manually as follows:
class PACKAGE TMyServerThread : public Scktcomp::TServerClientThread
{
public:
// if true, FreeOnTerminate is set to false before the thread terminates,
// and the thread is left in the thread cache. When KeepInCache is false,
// the thread is freed when execution terminates.
__fastcall TMyServerThread(bool CreateSuspended, TServerClientWinSocket* ASocket)
: Scktcomp::TServerClientThread(CreateSuspended, ASocket)
{ CreateSuspended = false; KeepInCache=true; FreeOnTerminate=false; };
// To implement this thread, you override the ClientExecute method instead of the Execute method.
void __fastcall ClientExecute(void);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
// *******************************************************
//---------------------------------------------------------------------------
|