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 theBuffer:=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 dataReceive 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:
WyltonMy 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 readVar
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 serverLen:=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: