Home > Back-end >  Delphi xe Indy control TIdTCPClient listening problems
Delphi xe Indy control TIdTCPClient listening problems

Time:10-28

TIdTCPServer transceiver is no problem, now there is trouble is TIdTCPClient listening

TCPClient. Connect successful, would create a thread
//create a thread
ThreadHandle:=CreateThread (nil, 0, @ TForm5 ClientListen, nil, 0, ThreadId);

Here is to simulate TIdTCPServer play, the original Socket. ReadLn there is no problem, now, into the Socket ReadBytes or. IOHandler. ReadBytes, have a problem, is the bytes, the server sends the client no response is also check len=0, but after the client sends bytes, the server receives as usual, at this time of the last byte sent to the client, also is to examine len> 0, very strange, that is, you have what good advice

Procedure TForm5. ClientListen;
Var
Len: integer;
S: a string;
Buffer: TBytes;
The begin
While true do

The begin

Try

Len:=Form5) TCPClient. Socket. InputBuffer. Size;

If len & gt; 0 then
The begin

//s=Form5) TCPClient) Socket. ReadLn (");
Form5) TCPClient) Socket. ReadBytes (buffer, len, false);
//Form5. TCPClient. IOHandler. ReadBytes (buffer, len, false);
//into the string
S:=stringof (buffer);
Form5) SendConsole) Lines. The Add (s);
end;
Except,

end;
end;
end;

CodePudding user response:

There is something wrong with the server-side logic?
After all, Indy mechanism is blocked,
Control becomes so well.

CodePudding user response:

The server is to use the

Buffer:=bytesof (Edit3. Text);
BzAContext. Connection. The IOHandler. Write (buffer);

BzAContext is in after the success of the server connection procedure TForm5 TCPServerConnect (AContext: TIdContext); Gain BzAContext:=AContext;

This server, the server using other gadgets sokit tested, no problem

CodePudding user response:

BzAContext is in after the success of the server connection procedure TForm5 TCPServerConnect (AContext: TIdContext); Gain BzAContext:=AContext;

You have a problem the BzAContext oh,
TForm5. TCPServerConnect (AContext: TIdContext), AContext are variables will change at any time,
So you this BzAContext also change at any time,

So your program written matter,
You can be in onExecute events with AContext reply the client can try received reply,

CodePudding user response:

Client TIdTCPClient use thread to monitor receives data
Receive data server TIdTCPServer in onExecute events, each one client connection AContext (parameters) is equivalent to a thread
Data is automatically triggered onExecute on arrival, and will maintain the TIdTCPServer AContext

So don't need to save the Connect event AContext

All the connection IdTCPServer. Contexts, Handle their judgment

CodePudding user response:

Wylton
My experiment in onExecute event AContext directly reply to the client is not receive,

Kaikai_kk
I understand what you say, if you don't use the zero AContext, what I use to send the client, because it's not always have onExecute events sent to the client, may at any time in other places, such as a button to trigger the

My server is using the AContext I get, it is can be sent to small tools sokit, he would be able to receive, sokit, my server can also be received normally, prove that the server is ok, the client or have a question,
When client can use WriteIn and ReadIn server, but I still require to send and receive bytes (without 0 d0a), a problem this is

Two good friend can give point you write code, other elder brothers friend can help me, thank you

CodePudding user response:

This, you look so so uncomfortable...
Open a thread starts, why?
Originally INDY design is very clear, TCP, a client, start a thread, one to one,

CodePudding user response:

Can you post your server-side code?

CodePudding user response:

This is my client read

Var
RxBuf: TIdBytes;
CB - is my another structure variable

SetLength (RxBuf, SizeOf (CB));//read will not change

IdTCPClient1. IOHandler. ReadBytes (RxBuf, SizeOf (CB), False);
Idglobal. BytesToRaw (RxBuf, CB, SizeOf (CB));

This cb assignment,


You
Var
Len: integer;
S: a string;
Buffer: TBytes;
The begin

Buffer: TBytes; -- -- -- -- -- this is there a problem?

CodePudding user response:

Wylton, I read something, bytes of variable size to do, your SizeOf (CB), is clearly a fixed size of the read, but you can give me the code, I can learn, experiment, thanks a lot, I wish to help me consider variable size, like the server
Len:=AContext. Connection. The IOHandler. InputBuffer. Size;
If len & gt; 0 then
The begin
AContext. Connection. IOHandler. ReadBytes (buffer, len, false);

//into the string
S:=stringof (buffer);

The Self. The Console. Lines. The Add (s);


end;

This can play, usable

CodePudding user response:

Your server to send code does not stick out, let others guess your mistake?

SizeOf (server to send) actually has a fixed maximum, so the file transfer and bigger things, all want to block,

CodePudding user response:

refer to 12th floor response:
your server to send code does not stick out, let others guess your mistake?

SizeOf (server to send) actually has a fixed maximum, so the file transfer and bigger things, all want to block,


Man upstairs to look at the second floor, already posted, there are other elder brothers say my BzAContext has a problem, actually a small tool sokit test, the server side can be, the key or the client receive on my side, I can test your receive fixed size, the best, did not know the size of, is like a server side length of the test, first to read,

CodePudding user response:

You can start the length value, and then send buffer.

CodePudding user response:

So difficult, the client no matter so much, he is indefinite length message

CodePudding user response:

Can such, packet is divided into two parts, starter packet type, and then send the packet content

Every time reading, according to the type of package, to read the packet length

CodePudding user response:

I no longer dared to enthusiasm, is my silly?
I put forward testing scheme, he did not dare to try,, also dare not to think,

CodePudding user response:

If all is my own programming is simple, now people have a ready-made program is indefinite length of hair and accept,


Wylton has nothing to do, you have helped me a lot of, the elder brothers to thank you for giving me those ideas
Shuihan20e what specific code can be posted some

CodePudding user response:

Could you tell me the original poster can put your server and client code sent to me, I am a student, are taught in the Delphi the content of the communication between server and client, struggling to find good routines, can't even that how to set up a few controls, anyway, thank you
[email protected]

CodePudding user response:

Today, I also met the same problem as you
I look at the source code

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related