multithreading - How to make a bidirectional TCP connection using Indy? -


i use tidtcpclient , tidtcpserver (with own protocol) , want make tcp connection in both computers can send commands , read replays. of course, 1 server , other client, until connection established, after that, both must "servers , clients". don't know how explain more clearly... mainly, computer "a" send commands computer "b", sometimes, events happen on computer "b" , must communicated computer "a". so, every computer should listen time commands, in same time must able send commands when events happening.

for sending commands it's simple: write socket. able replay them, application must read socket, , if read, can't write.... searched internet , found here , here answers similar questions, in said should used 2 threads, 1 writing , 1 reading. don't understand how cand use same object, connection socket, in 2 threads... if 1 thread reads changed other (the basic thread problem) ?

i have made tests chat application following code , seems it's working fine i'm not sure if correct way it... mean, create connection , after pass socket object 2 threads, 1 read , 1 write.

constructor treadingthread.create(asocket: tidiohandlersocket); begin   fsocket := asocket;   inherited create(false); end;  procedure treadingthread.execute; var   cmd: string; begin   while not terminated   begin     cmd := fsocket.readln;     trim(cmd);     if cmd <> ''     synchronize(     procedure     begin       form1.display.lines.add(cmd);     end);   end; end;  procedure tform1.bconnectclick(sender: tobject); begin  tcpclient.connect; end;  procedure tform1.inputkeypress(sender: tobject; var key: char); begin  if key=#13 begin   tcpclient.socket.writeln(input.text);   input.clear;   key:=#0;  end; end;  procedure tform1.tcpclientconnected(sender: tobject); begin  readthread:= treadingthread.create(tcpclient.socket);  display.lines.add('server: connected.'); end; 

computer "a" send commands computer "b", sometimes, events happen on computer "b" , must communicated computer "a". so, every computer should listen time commands, in same time must able send commands when events happening.

when sends command b, or vice versa, has no way of knowing whether next message received response command or unsolicited event. can't send command , sit waiting response, may receive other messages in meantime. have read messages asynchronously , handle them on per-message basis arrive.

to facilitate this, every message must identify (command, response, or event). when command sent, sender should include user-defined value in echoed in response. allow sender correlate response commands, , have multiple commands in flight @ same time. unsolicited events not have user-defined value in them, don't response.

this way, events can sent @ time, , when command received can processed in parallel else, , response sent whenever ready (even out of order of other commands, if desired).

for sending commands it's simple: write socket. able replay them, application must read socket, , if read, can't write....

yes, can. can't in same thread, if thread blocked doing other things.

i searched internet , found here , here answers similar questions, in said should used 2 threads, 1 writing , 1 reading.

yes, 1 solution, , 1 end using indy, given blocking nature. not solution when working sockets in general, there better solutions (overlapped i/o, etc).

but don't understand how cand use same object, connection socket, in 2 threads...

what makes think can't shared? socket has separate inbound , outbound buffers. 1 thread can reading inbound data while thread writing outbound data @ same time. not interfere each other.

what if 1 thread reads changed other (the basic thread problem) ?

nothing being changed on reading side while writing side working, , vice versa.

what have watch out multiple threads reading socket @ same time, or multiple threads writing socket @ same time. not safe without adequate synchronization between threads. 1 thread reading while 1 thread writing safe without synchronizing 2 threads.

i have made tests chat application following code , seems it's working fine i'm not sure if correct way it... mean, create connection , after pass socket object 2 threads, 1 read , 1 write.

that fine. make sure reading thread terminated before connection object destroyed.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -