BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

using TIdTCPServer

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket)
View previous topic :: View next topic  
Author Message
Simon Guertin
Guest





PostPosted: Thu May 05, 2005 2:15 pm    Post subject: using TIdTCPServer Reply with quote



Hi, I am a long time Java user, so I think in Java a little bit. I need
to create a TCP server and I would like to know if using the
TIdTCPServer will be good for me or if I should implement a server that
looks like this (at the bottom of the email):

my question is, : I need to know where to put all my processing of the
connection.. other than in the TCPServerExecute() method of the
IndyServer. Should I re-write my own server..can I change the way the
threads are started?

my application will be a server that accepts connection to register and
activate a software online.. with a little bit of UI.

thank you

Simon

java server->
the server:
public class KKMultiServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening)
new KKMultiServerThread(serverSocket.accept()).start();
serverSocket.close();
}
}




thread that process the connection:
public class KKMultiServerThread extends Thread {
private Socket socket = null;

public KKMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}

public void run() {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye"))
break;
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu May 05, 2005 4:59 pm    Post subject: Re: using TIdTCPServer Reply with quote




"Simon Guertin" <sguertin_AT (AT) trellianetworks (DOT) com> wrote


Quote:
Hi, I am a long time Java user, so I think in Java a little bit. I
need to create a TCP server and I would like to know if using
the TIdTCPServer will be good for me or if I should implement
a server that looks like this (at the bottom of the email):

TIdTCPServer already does exactly the same thing that your Java code is
doing. When activated, it runs a continous loop that accepts new client
connections and spawns a thread for each one.

Quote:
my question is, : I need to know where to put all my processing
of the connection.. other than in the TCPServerExecute() method
of the IndyServer.

The OnExecute event *is* where you are supposed to place your processing
code.

Quote:
Should I re-write my own server..can I change the way the threads
are started?

Why? Do you have a problem with the way they are currently handled?


Gambit



Back to top
Simon Guertin
Guest





PostPosted: Thu May 05, 2005 6:19 pm    Post subject: Re: using TIdTCPServer Reply with quote



I would have like to have all my processing code in another class. This
class will takeover the client communication since it is not really the
best place to take care of that in the event of TIdTCPServer OnExecute
Event. By processing everything in the OnExecute Method, it will not be
good programming practice I beleive. I think the way the Java class
handles the client connexion is a good way of OO programming.

thanks for the answer

Simon


Remy Lebeau (TeamB) wrote:
Quote:
"Simon Guertin" <sguertin_AT (AT) trellianetworks (DOT) com> wrote in message
news:427a2a63$1 (AT) newsgroups (DOT) borland.com...


Hi, I am a long time Java user, so I think in Java a little bit. I
need to create a TCP server and I would like to know if using
the TIdTCPServer will be good for me or if I should implement
a server that looks like this (at the bottom of the email):


TIdTCPServer already does exactly the same thing that your Java code is
doing. When activated, it runs a continous loop that accepts new client
connections and spawns a thread for each one.


my question is, : I need to know where to put all my processing
of the connection.. other than in the TCPServerExecute() method
of the IndyServer.


The OnExecute event *is* where you are supposed to place your processing
code.


Should I re-write my own server..can I change the way the threads
are started?


Why? Do you have a problem with the way they are currently handled?


Gambit



Back to top
Jonathan Benedicto
Guest





PostPosted: Thu May 05, 2005 7:28 pm    Post subject: Re: using TIdTCPServer Reply with quote

"Simon Guertin" <sguertin_AT (AT) trellianetworks (DOT) com> wrote

Quote:
I would have like to have all my processing code in another class.
This class will takeover the client communication since it is not
really the best place to take care of that in the event of
TIdTCPServer OnExecute Event. By processing everything in the
OnExecute Method, it will not be good programming practice I beleive.
I think the way the Java class handles the client connexion is a good
way of OO programming.

AFAIK, you can best resemble the way the Java server works by using a
TServerSocket component, in "blocking" mode, and using your own thread
class.

To do this, look in the BCB help for 'server threads, example,' in the
index.

HTH

Jonathan



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu May 05, 2005 7:45 pm    Post subject: Re: using TIdTCPServer Reply with quote


"Simon Guertin" <sguertin_AT (AT) trellianetworks (DOT) com> wrote


Quote:
I would have like to have all my processing code in another class.

Then do so. There is nothing preventing you from doing that.

Quote:
This class will takeover the client communication since it is
not really the best place to take care of that in the event of
TIdTCPServer OnExecute Event.

Why not? Please elaborate.

Quote:
By processing everything in the OnExecute Method, it will not
be good programming practice I beleive. I think the way the
Java class handles the client connexion is a good way of OO
programming.

Your Java code is not doing anything that cannot be done in Indy as well.
For example:

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
AThread.Connection.WriteLn('whatever your greeting is');
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
inputLine, outputLine: String ;
begin
inputLine = AThread.Connection.ReadLn;
//...
AThread.Connection.WriteLn('whatever your response is');
if (should disconnect) then AThread.Connection.DisconnectSocket;
end;


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu May 05, 2005 7:48 pm    Post subject: Re: using TIdTCPServer Reply with quote


"Jonathan Benedicto" <incorrect (AT) no (DOT) server> wrote


Quote:
AFAIK, you can best resemble the way the Java server works
by using a TServerSocket component, in "blocking" mode, and
using your own thread class.

Indy does the exact same thing. You just don't have to write your own
thread class, Indy'sa default class does most of the work for you. If you
want to write your own thread class, though, you can. Although i would not
recommend this approach, you can derive a new class from TIdPeerThread and
override its Run() method. Then assign the class type to the server's
ThreadClass property before activating the server.


Gambit



Back to top
Jonathan Benedicto
Guest





PostPosted: Thu May 05, 2005 7:51 pm    Post subject: Re: using TIdTCPServer Reply with quote

"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote

Quote:

Indy does the exact same thing. You just don't have to write your
own
thread class, Indy'sa default class does most of the work for you.
If you
want to write your own thread class, though, you can. Although i
would not
recommend this approach, you can derive a new class from
TIdPeerThread and
override its Run() method. Then assign the class type to the
server's
ThreadClass property before activating the server.

Thank you for this info. I personally would use the OnExecute event,
but I have used TServerSocket in the manner I suggested before I found
out about Indy.

Jonathan



Back to top
Simon Guertin
Guest





PostPosted: Fri May 06, 2005 7:42 pm    Post subject: Re: using TIdTCPServer Reply with quote

Thank you for the explanation, I have one more question..
let's say that in this case, 5 clients connect at the same time.. I
greet them in the OnConnect method..then in the execute.. I need to know
who is the client..so I write on the socket a command and then I wait
for the answer.. but is'nt the method iDTCPServerExecute will be called
by many objects at the same time and change my local variables?
I need the store the client's retreived info in the Thread's Data object?

let's say I ask for a client name, then I return to him a private key..
and I then wait for a command from the client.. If this is not done from
within the peerthread..how can I do that?

thanks

Simon

Quote:

Your Java code is not doing anything that cannot be done in Indy as well.
For example:

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
AThread.Connection.WriteLn('whatever your greeting is');
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
inputLine, outputLine: String ;
begin
inputLine = AThread.Connection.ReadLn;
//...
AThread.Connection.WriteLn('whatever your response is');
if (should disconnect) then AThread.Connection.DisconnectSocket;
end;


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri May 06, 2005 8:46 pm    Post subject: Re: using TIdTCPServer Reply with quote


"Simon Guertin" <sguertin_AT (AT) trellianetworks (DOT) com> wrote


Quote:
is'nt the method iDTCPServerExecute will be called by many
objects at the same time

Yes.

Quote:
and change my local variables?

No. Local variables are just that - local. Each call of a function has its
own copy of the local variables from every other call of the same function.
Each thread also has its own stack, so multiple threads will not step over
each other's stack. The stack is used for storing local variables, setting
up function calls, and other miscellaneous things (temporary values, etc)
that the compiler needs.

Quote:
I need the store the client's retreived info in the Thread's Data object?

Yes. Alternatively, you can derive a new class from TIdPeerThread to
include additional properties/methods, and then cast the event handler's
AThread to your custom class type when needed. You would also have to set
the server's ThreadClass property so it knows which class type to
instantiate for each client.


Gambit



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.