Board index » delphi » Some problems in my simple chat program

Some problems in my simple chat program

Hi,

I'm trying to learn the usage of Indy with developing simple chat program.
But i have some questions to ask about it :

- I'm taking an error "Socket Error #10048 Address already in use". I don't
know Why this occurs? and How should i do to stop that?
- The other problem is in my server program. Until i send a message from
client, server message is not sending to client. I couldn't solve why. Can
you tell me the reason with a little bit explanation ?
- When i try to close the connection from Clien or Server, sometimes i'm
taking messages like "No Connection" or "Connection Closed Gracefully". Only
after 3 or 4 attempt i can close this dialog boxes. Is this an error?,
Should i do something to handle these messages?
- I want to send message with with "Enter" key press but i dont know how to
do it so i use "/" character. How can i do that ?

procedure TfrmServer.ServerExecute(AThread: TIdPeerThread);
var
  S, Msg: string;
begin
  S := Copy(edtMessage.Text, Length(edtMessage.Text), 1);
  if S = '/' then begin
    AThread.Connection.WriteLn(edtMessage.Text);
    memHistory.Lines.Add('My Words > ' + edtMessage.Text);
    edtMessage.Text := '';
  end;
  Msg := AThread.Connection.ReadLn;
  if Msg <> '' then
  begin
    Msg := 'Remote Users Words > ' + Msg;
    memHistory.Lines.Add(Msg);
  end;
end;

Sorry about this long message but in spite of my lots of try, i couldn't
find answers of these questions by myself.

Kind Regards..

 

Re:Some problems in my simple chat program


you should take a look at the chat demo

--
Tony Caduto
AM Software Design
Inexpensive Corporate Instant Messaging
http://www.amsoftwaredesign.com

Quote
"Rushit" <neonb...@hotmail.com> wrote in message

news:3e81efd4$1@newsgroups.borland.com...
Quote
> Hi,

> I'm trying to learn the usage of Indy with developing simple chat program.
> But i have some questions to ask about it :

> - I'm taking an error "Socket Error #10048 Address already in use". I
don't
> know Why this occurs? and How should i do to stop that?
> - The other problem is in my server program. Until i send a message from
> client, server message is not sending to client. I couldn't solve why. Can
> you tell me the reason with a little bit explanation ?
> - When i try to close the connection from Clien or Server, sometimes i'm
> taking messages like "No Connection" or "Connection Closed Gracefully".
Only
> after 3 or 4 attempt i can close this dialog boxes. Is this an error?,
> Should i do something to handle these messages?
> - I want to send message with with "Enter" key press but i dont know how
to
> do it so i use "/" character. How can i do that ?

> procedure TfrmServer.ServerExecute(AThread: TIdPeerThread);
> var
>   S, Msg: string;
> begin
>   S := Copy(edtMessage.Text, Length(edtMessage.Text), 1);
>   if S = '/' then begin
>     AThread.Connection.WriteLn(edtMessage.Text);
>     memHistory.Lines.Add('My Words > ' + edtMessage.Text);
>     edtMessage.Text := '';
>   end;
>   Msg := AThread.Connection.ReadLn;
>   if Msg <> '' then
>   begin
>     Msg := 'Remote Users Words > ' + Msg;
>     memHistory.Lines.Add(Msg);
>   end;
> end;

> Sorry about this long message but in spite of my lots of try, i couldn't
> find answers of these questions by myself.

> Kind Regards..

Re:Some problems in my simple chat program


Mr Caduto, anyway i looked at the demo but couldn't solve all of my problems
:(. For example in this demo i saw some code :

TSimpleClient = class(TObject)
  DNS,
  Name        : String;
  ListLink    : Integer;
  Thread      : Pointer;
end;
-----------------------------------------------------------------
procedure TfrmMain.btnPMClick(Sender: TObject);
var
  Msg : String;
  Client : TSimpleClient;
begin
  Msg := InputBox('Private Message', 'What is the message', '');
  Msg := Trim(Msg);
  Msg := edSyopName.Text + '> ' + Msg;
  if (Msg <> '') and
     (lbClients.ItemIndex <> -1) then
    begin
      Client := Clients.Items[lbClients.ItemIndex];
      // I didnt understand this usage :
      TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);
    end;
end;

Can you help me please.. Still i couldn't solve my old questions..

Best Regards,

Re:Some problems in my simple chat program


Quote
Rushit wrote:
> Hi,

> I'm trying to learn the usage of Indy with developing simple chat program.
> But i have some questions to ask about it :

> - I'm taking an error "Socket Error #10048 Address already in use". I don't
> know Why this occurs? and How should i do to stop that?

This is displayed becuse your attempting to start a server on an
address/port that is already in use!! :) simple enough.

Quote
> - The other problem is in my server program. Until i send a message from
> client, server message is not sending to client. I couldn't solve why. Can
> you tell me the reason with a little bit explanation ?

This is normally the case. however if your client is connected to your
sever, then all you have to to is use the
TIdPeerThread.connection.writeln (or to that effect, read the help file)
to write data to existing clients.

Quote
> - When i try to close the connection from Clien or Server, sometimes i'm
> taking messages like "No Connection" or "Connection Closed Gracefully". Only
> after 3 or 4 attempt i can close this dialog boxes. Is this an error?,
> Should i do something to handle these messages?

Ok, and you should have read the help file..  "connection Closed
Gracefully" is an error you will get in the IDE when running your
program and a client/server disconnects the connection.  this is well
documented in the class that will "popup" in your face when you get that
error in the IDE..........

Quote
> - I want to send message with with "Enter" key press but i dont know how to
> do it so i use "/" character. How can i do that ?

well, in whatever conrtol you are using, you would capture the
"onKeyPress" event and then see if the Key is #13 or not and then
process accordingly.

Quote

> Sorry about this long message but in spite of my lots of try, i couldn't
> find answers of these questions by myself.

Actually you can find the answers yourself if you study (for more than a
day for a few hours) the chat demo for Indy.. This is where i got all
the information i needed to start my chat client/server and it's well on
the way to being released..

hth

--
Jack Mays

Re:Some problems in my simple chat program


First of all thank you a lot for detailed reply. I'm not a lazy person as
you may guess :). All the day i tried to solve all my problems about indy
with trying to make some simple demos. But still i couldn't understand the
usage of TIdPeerThread. Maybe i'm wasting your time but i want from you to
explain something more about this demo to me. In "Chat Demo", server use
TIdPeerThread with the Client variant which is in TSimpleClient type. Can
you explain its usage to me ? Why the type of TSimpleClient declared ?

TSimpleClient = class(TObject)
  DNS,
  Name        : String;
  ListLink    : Integer;
  Thread      : Pointer;
end;

TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);

Kind Regards,

Re:Some problems in my simple chat program


Quote
Rushit wrote:
> First of all thank you a lot for detailed reply. I'm not a lazy person as
> you may guess :). All the day i tried to solve all my problems about indy
> with trying to make some simple demos. But still i couldn't understand the
> usage of TIdPeerThread. Maybe i'm wasting your time but i want from you to
> explain something more about this demo to me. In "Chat Demo", server use
> TIdPeerThread with the Client variant which is in TSimpleClient type. Can
> you explain its usage to me ? Why the type of TSimpleClient declared ?

> TSimpleClient = class(TObject)
>   DNS,
>   Name        : String;
>   ListLink    : Integer;
>   Thread      : Pointer;
> end;

> TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);

Sure, not a problem :)

The reason the object is defined is so that you may easliy track info
for a user (connected client).  My object is actually defined somewhat
like this.

TUserInfo = class(TObject)
   Hostname, UserID, UserName, Email, Password: string;
   IPaddr, Port, ListLink: integer;
   thread: pointer
end;

...
...
...

-

When your client connects (IdTCPServer.OnConnect) or executes a command
(IdTCPServer.OnExecute) a thread is passed, TIdPeerThread, that tells
you which client is doing the connecting or executing.

There are 2 ways you can send data to connected clients.  One way is if
you are inside the OnExecute procedure and want to send data to the same
user that fired the Onexecute, you would use:

     Athread.Connection.Writeln(msg);

but, if you want to send data to a client that isn't the one firing the
OnExecute command you would do something like this:

// send message to all connected users
for I:=0 to SimpleClient.Count-1 do
begin
   TIdPeerThread(SimpleClient[i]).connection.writeln(msg);
end;

After workin all night/day, i probably didn't explain it very well, but
if your still confused, i'll try to help out more tomorrow when i have a
freash mind.

hth

--
Jack Mays

Re:Some problems in my simple chat program


Thanks Jack, I understood that i can't fire both(writeln and readln) in
OnExecute event when developing chat program. Thank you. But still i didn't
understand this code :

Client := Clients.Items[lbClients.ItemIndex];

Client is in TSimpleClient type, Clients is a listbox and Clients.Items
property is string.. Are them incompatible types ? Sorry but i'm little
confused about it.. Can you give me a little example about how can i send
data to a client from outside the OnExecute event ? I'm not trying to
communicate with multiple clients, there is only one..

Kind Regards,

// --- my procedure :
procedure TfrmServer.bbtnSendClick(Sender: TObject);
var
  X: TSimpleClient;
begin
  // some code.. What should i do in here ?
  TIdPeerThread(X.Thread).Connection.WriteLn(edtMessage.Text);
end;

// --- from Chat Demo :
procedure TfrmMain.btnPMClick(Sender: TObject);
var
  Msg : String;
  Client : TSimpleClient;
begin
  Msg := InputBox('Private Message', 'What is the message', '');
  Msg := Trim(Msg);
  Msg := edSyopName.Text + '> ' + Msg;
  if (Msg <> '') and
     (lbClients.ItemIndex <> -1) then
    begin
      Client := Clients.Items[lbClients.ItemIndex];
      TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);
    end;
end;

Quote
"Jack Mays" <jackm...@NOSPAM.earthlink.net> wrote in message

news:3E835A72.6030205@NOSPAM.earthlink.net...
Quote

> Rushit wrote:
> > First of all thank you a lot for detailed reply. I'm not a lazy person
as
> > you may guess :). All the day i tried to solve all my problems about
indy
> > with trying to make some simple demos. But still i couldn't understand
the
> > usage of TIdPeerThread. Maybe i'm wasting your time but i want from you
to
> > explain something more about this demo to me. In "Chat Demo", server use
> > TIdPeerThread with the Client variant which is in TSimpleClient type.
Can
> > you explain its usage to me ? Why the type of TSimpleClient declared ?

> > TSimpleClient = class(TObject)
> >   DNS,
> >   Name        : String;
> >   ListLink    : Integer;
> >   Thread      : Pointer;
> > end;

> > TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);

> Sure, not a problem :)

> The reason the object is defined is so that you may easliy track info
> for a user (connected client).  My object is actually defined somewhat
> like this.

> TUserInfo = class(TObject)
>    Hostname, UserID, UserName, Email, Password: string;
>    IPaddr, Port, ListLink: integer;
>    thread: pointer
> end;

> ...
> ...
> ...

> -

> When your client connects (IdTCPServer.OnConnect) or executes a command
> (IdTCPServer.OnExecute) a thread is passed, TIdPeerThread, that tells
> you which client is doing the connecting or executing.

> There are 2 ways you can send data to connected clients.  One way is if
> you are inside the OnExecute procedure and want to send data to the same
> user that fired the Onexecute, you would use:

>      Athread.Connection.Writeln(msg);

> but, if you want to send data to a client that isn't the one firing the
> OnExecute command you would do something like this:

> // send message to all connected users
> for I:=0 to SimpleClient.Count-1 do
> begin
>    TIdPeerThread(SimpleClient[i]).connection.writeln(msg);
> end;

> After workin all night/day, i probably didn't explain it very well, but
> if your still confused, i'll try to help out more tomorrow when i have a
> freash mind.

> hth

> --
> Jack Mays

Re:Some problems in my simple chat program


Clients is a TList. lbclients is the listbox.

Quote
"Rushit" <neonb...@hotmail.com> wrote in message

news:3e848407@newsgroups.borland.com...
Quote
> Thanks Jack, I understood that i can't fire both(writeln and readln) in
> OnExecute event when developing chat program. Thank you. But still i
didn't
> understand this code :

> Client := Clients.Items[lbClients.ItemIndex];

> Client is in TSimpleClient type, Clients is a listbox and Clients.Items
> property is string.. Are them incompatible types ? Sorry but i'm little
> confused about it.. Can you give me a little example about how can i send
> data to a client from outside the OnExecute event ? I'm not trying to
> communicate with multiple clients, there is only one..

> Kind Regards,

> // --- my procedure :
> procedure TfrmServer.bbtnSendClick(Sender: TObject);
> var
>   X: TSimpleClient;
> begin
>   // some code.. What should i do in here ?
>   TIdPeerThread(X.Thread).Connection.WriteLn(edtMessage.Text);
> end;

> // --- from Chat Demo :
> procedure TfrmMain.btnPMClick(Sender: TObject);
> var
>   Msg : String;
>   Client : TSimpleClient;
> begin
>   Msg := InputBox('Private Message', 'What is the message', '');
>   Msg := Trim(Msg);
>   Msg := edSyopName.Text + '> ' + Msg;
>   if (Msg <> '') and
>      (lbClients.ItemIndex <> -1) then
>     begin
>       Client := Clients.Items[lbClients.ItemIndex];
>       TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);
>     end;
> end;

> "Jack Mays" <jackm...@NOSPAM.earthlink.net> wrote in message
> news:3E835A72.6030205@NOSPAM.earthlink.net...

> > Rushit wrote:
> > > First of all thank you a lot for detailed reply. I'm not a lazy person
> as
> > > you may guess :). All the day i tried to solve all my problems about
> indy
> > > with trying to make some simple demos. But still i couldn't understand
> the
> > > usage of TIdPeerThread. Maybe i'm wasting your time but i want from
you
> to
> > > explain something more about this demo to me. In "Chat Demo", server
use
> > > TIdPeerThread with the Client variant which is in TSimpleClient type.
> Can
> > > you explain its usage to me ? Why the type of TSimpleClient declared ?

> > > TSimpleClient = class(TObject)
> > >   DNS,
> > >   Name        : String;
> > >   ListLink    : Integer;
> > >   Thread      : Pointer;
> > > end;

> > > TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);

> > Sure, not a problem :)

> > The reason the object is defined is so that you may easliy track info
> > for a user (connected client).  My object is actually defined somewhat
> > like this.

> > TUserInfo = class(TObject)
> >    Hostname, UserID, UserName, Email, Password: string;
> >    IPaddr, Port, ListLink: integer;
> >    thread: pointer
> > end;

> > ...
> > ...
> > ...

> > -

> > When your client connects (IdTCPServer.OnConnect) or executes a command
> > (IdTCPServer.OnExecute) a thread is passed, TIdPeerThread, that tells
> > you which client is doing the connecting or executing.

> > There are 2 ways you can send data to connected clients.  One way is if
> > you are inside the OnExecute procedure and want to send data to the same
> > user that fired the Onexecute, you would use:

> >      Athread.Connection.Writeln(msg);

> > but, if you want to send data to a client that isn't the one firing the
> > OnExecute command you would do something like this:

> > // send message to all connected users
> > for I:=0 to SimpleClient.Count-1 do
> > begin
> >    TIdPeerThread(SimpleClient[i]).connection.writeln(msg);
> > end;

> > After workin all night/day, i probably didn't explain it very well, but
> > if your still confused, i'll try to help out more tomorrow when i have a
> > freash mind.

> > hth

> > --
> > Jack Mays

Other Threads