Article View: borland.public.cppbuilder.internet.socket
Article #4115Re: Help using sockets.....
From: Paul
Date: Wed, 06 Apr 2005 20:40
Date: Wed, 06 Apr 2005 20:40
168 lines
6028 bytes
6028 bytes
Its all as per the tutorial really. I may zip it up and try it on a XP
machine to see if its my W98SE installation, hopefully not.
Here's the server main form and thread:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket, TServerClientThread
*&SocketThread)
{
SocketThread = new TMyServerThread(false, ClientSocket);
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::SocketOpenClick(TObject *Sender)
{
// open thread blocking socket
ServerSocket1->Active = true;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::SocketCloseClick(TObject *Sender)
{
// close thread blocking socket
ServerSocket1->Active = false;
}
//--------------------------------------------------------------------------
-
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(pStream->TimeOut))
{
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();
}
}
}
And here's the client main form:
//--------------------------------------------------------------------------
-
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
-
void __fastcall TForm2::Button1Click(TObject *Sender)
{
AnsiString Server = ClientSocket1->Host;
if (ClientSocket1->Active)
{
Memo1->Lines->Add("Button Click : Port was open, now closed.");
ClientSocket1->Active = false;
}
if (InputQuery("Computer to connect to", "Address Name:", Server))
{
if (Server.Length() > 0)
{
ClientSocket1->Host = Server;
ClientSocket1->Active = true;
}
}
}
//--------------------------------------------------------------------------
-
void __fastcall TForm2::Button2Click(TObject *Sender)
{
ClientSocket1->Active = false;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm2::ClientSocket1Read(TObject *Sender, TCustomWinSocket
*Socket)
{
Memo1->Lines->Add("Read Event Handler");
if (ClientSocket1->Active == true)
if (Socket->Connected == true)
Memo1->Lines->Add( AnsiString("(Server) ") +
Socket->ReceiveText() );
}
//--------------------------------------------------------------------------
-
void __fastcall TForm2::ClientSocket1Write(TObject *Sender, TCustomWinSocket
*Socket)
{
Memo1->Lines->Add("Write Event Handler");
if (ClientSocket1->Active == true)
{
Memo1->Lines->Add("..Port Active");
if (Socket->Connected == true)
{
AnsiString str = "....Send Text";
int i = Socket->SendText("This text is passed to and from");
str += i;
Memo1->Lines->Add(str);
}
}
}
> "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:425445db@newsgroups.borland.com...
>
> Please show a more complete snippet that shows everything you are doing.
> You have shown nothing about the code that surrounds your reading and
> writing, so there's no way to know if you are even handling the socket
> properly in the first place.
>
> Gambit
Technical Details
Message-ID:
<425448ec$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<425388a2@newsgroups.borland.com> <425396df$1@newsgroups.borland.com> <4254080d$1@newsgroups.borland.com> <42542096$1@newsgroups.borland.com> <4254244a$1@newsgroups.borland.com> <425426cb$1@newsgroups.borland.com> <425445db@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,b9337a02b76d7efd X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Paul" <plmacca@clara.co.uk> From: "Paul" <plmacca@clara.co.uk> Newsgroups: borland.public.cppbuilder.internet.socket References: <425388a2@newsgroups.borland.com> <425396df$1@newsgroups.borland.com> <4254080d$1@newsgroups.borland.com> <42542096$1@newsgroups.borland.com> <4254244a$1@newsgroups.borland.com> <425426cb$1@newsgroups.borland.com> <425445db@newsgroups.borland.com> Subject: Re: Help using sockets..... Date: Wed, 6 Apr 2005 21:40:50 +0100 Lines: 171 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 81.129.191.106 Message-ID: <425448ec$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1112819948 81.129.191.106 (6 Apr 2005 13:39:08 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail