• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

How to use TcpServer and TcpClient to realise two computer communication and transfer information?

How to use TcpServer and TcpClient to realise two computer communication and transfer information?
  • 2
1 Solution
Suggest you use Indy for this. There's a vast number of samples included on exactly your question. If you need a sample poster here, just let me know.
kousunAuthor Commented:
Yes ,Can you tell me some difference about the winsocket and the Indy?
I want to exchange some information (such as data or login
information) in two computers within the network,the computer has there own Ip address.
Well.. I started using Indy because I needed threaded tcp-data (means basically that you can send and receive multiple data-packets independant from eachother) and Indy also provided easy Socks (proxy) support etc.. so I never really tried Winsock.. Indy was just perfect for me.

If you need to exchange some data, this can be done really easy.. you can for example start an idTCPserver that starts listening and a certain port (say 1025) and have the programm respond to data received there. You could create some kind of protocol yourself or use the Indy command protocol that is also available. If you use the latter than you can pre-define some commands the server should listen for and respond to it.. this is all done visually, so it's easy to understand. Or you can do it manually.. the basics of your program would be something like:

procedure TfrmServer.IdTCPServerExecute(AThread: TIdPeerThread); // the part that get executed when something arrives..
s  : string;
fStream : TFileStream;

s := (AThread.Connection.ReadLn); // this reads whatever came in, prosumably it is a text command, e.g. 'SENDPIC'
if s='SENDPIC' then
    if FileExists('d:\images\test.jpg') then
        // open file stream to image requested
        fStream := TFileStream.Create('d:\images\test.jpg',fmOpenRead + fmShareDenyNone);
        // copy file stream to write stream
        // free the file stream
    AThread.Connection.WriteLn('ERR - Requested file does not exist');

The Client part is not much different.. it is really easy.
Have a look at Indy's demos. The best one is the Image-server demo, it tackles many aspects and is easy to understand..

good luck
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now