Thank you to the two people who replied. I got really good advice which
I followed and this seems to have given me a working server, but I still
have client problems.
- I created a message-pump in a distinct thread for the DLL server, as
advised.
- Both client and server were changed to async programming style, and
this worked for the server.
I have another problem though, which seems stange, and may be related to
timing (or something).
My client wants to sometimes connect to the server, send a few lines of
data, and then disconnect.
When I want to send data this is what I do
procedure sendData(asMessage: string);
begin
mystringlist.add(asMessage);
if mytwsocket.state <> wsConnected then
begin
mytwsocket.addr := '127.0.0.1'; // client and server are on same
machine
mytwsocket.port := '17072';
mytwsocket.connect();
end;
end;
Then I have handlers for OnSessionAvailable and OnDataSent.
OnSessionAvailable doesn't seem to get called ever.
In OnDataSent I do this
begin
if mystringlist.count > 0 then
begin
mytwsocket.sendStr(mystringlist[0]+#$D#$A);
mystringlist.delete(0); // we've sent this one
end else
begin
mytwsocket.close(); // no more data to send, so close
end;
end;
Here's what I've observed. The client can send once, and that's it. I
can't send twice. However, if I put a breakpoint at the end of the
function that connects to the server,it works - almost like there is
some kind of timing issue.
I'm working on Windows 2003.
Should I use threads for my clients as well? Any advice is greatly received.
Wilfried Mestdagh wrote:
> Hello Pete,
>
>
>> if myclient.state <> wsConnected then
>> begin
>> myclient.connect;
>> loop for 5 seconds begin
>>
>
> You have to think async. TWSocket uses events. think on events as a
> OnClick event of a butten. You don't write loops to wait until a user
> click a button. So you have to change to:
>
> MyClient.Connect; // that's all
>
> and in the OnSessionAvailable event you start do
> TWSocket(Sender).sendstr(thedatastring);
>
>
>> myclient.processMessages;
>>
>
> General a very bad idea to call the message pump yourself.
>
>
>> myclient.close();
>>
>
> If your client has send all the data then you still dont know if the
> other end has received an handled the data. if you design your proto
> yourself then the receiver can close. if you dont then call
> ShutDown(1);
>
>
>> myserver.OnsessionAvailable
>> begin
>> myserversocket.dup(myserversocket.accept());
>> end;
>>
>
> Better to use TWSocketSer4ver.
>
> ---
> Rgds, Wilfried [TeamICS]
> http://www.overbyte.be/eng/overbyte/teamics.html
> http://www.mestdagh.biz
>
> Monday, November 26, 2007, 18:58, Pete Williams wrote:
>
>
>> Hello again
>>
>
>
>> I'm trying to write a very simple client/server socket application using
>> TWSocket. However, I think I may not understand the use of states correctly.
>>
>
>
>> What I want is for the client to connect to the server, send some data,
>> and then disconnect. If it has more data to send, I want it to connect
>> again and repeat the process.
>>
>
>
>> Here's what is happening at the moment. The client can connect to the
>> server and successfully send as much data as it chooses. However, once
>> it disconnects it can't reconnect.
>>
>
>
>> The code on the client is broadly thus (using a pseudo code):
>>
>
>
>> if myclient.state <> wsConnected then
>> begin
>> myclient.connect;
>> loop for 5 seconds begin
>> myclient.processMessages;
>> if myclient.state = wsConnected then
>> begin
>> lbConnected := TRUE:
>> break;
>> end;
>> end loop;
>> end else
>> lbConnected := TRUE;
>>
>
>
>> if lbConnected then
>> myclient.sendstr(thedatastring);
>>
>
>
>> myclient.close();
>>
>
>
>> On the server, this is what I have:
>>
>
>
>> myserver.OnsessionAvailable
>> begin
>> myserversocket.dup(myserversocket.accept());
>> end;
>>
>
>
>> I'm also handling the OnDataAvailable event, and this works fine - but
>> only the first time (or at least until the client disconnects).
>>
>
>
>> I've tried recalling listen() in the OnSessionClosed event, but it does
>> nothing.
>>
>
>
>> The state of the client on the 2nd attempt to reconnect remains at closed.
>>
>
>
>> If anyone can point me in the right direction I'd be grateful. Maybe I
>> need some form of clean-up on the server after the disconnect?
>>
>
>
>> Cheers,
>>
>
>
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be