Recent

Author Topic: synapser send file by tcip  (Read 12828 times)

eldonfsr

  • Sr. Member
  • ****
  • Posts: 446
synapser send file by tcip
« on: April 10, 2012, 04:25:07 am »
I just started with a small app, before i was using synapser serialcom but now i wanna send file also by tcpip, i read information but a i am lite confouse if i have to bind the ip sending and received, or is only pc to pc by net cable, pls helpme with some ideas pls.


eldonfsr

  • Sr. Member
  • ****
  • Posts: 446
Re: synapser send file by tcip
« Reply #1 on: April 14, 2012, 02:51:23 am »
After some test i got connect 2 pc on my local net but the problem is i don get received nothing, i dont know if only is posible to work with ip: 127.0.0.1 or localhost or i have to use cable cruz fot connect both pc he my source, tell me and help me if i doing something wrong

on client conection:
procedure TFormAction.Button2Click(Sender: TObject);
var   mysock:TTCPBlockSocket;
      s,m:string;
begin
  mysock := TTCPBlockSocket.Create;
  mysock.ConvertLineEnd := True;
  mysock.Connect ('192.168.1.103', '12345');
//This will read a line terminated by #A0 (LF) or #D0A0 (CRLF)
//  m := mysock.RecvString (30000); //read welcome message
  if mysock.LastError= 0 then
   begin
     mySock.Accept;
     mysock.SendString ('USER me'+CRLF); //you must send CRLF yourself
     mysock.SendString ('USER me'+CRLF);
     mysock.SendString ('USER me'+CRLF);
     mysock.SendString ('USER me'+CRLF);
     s:=mysock.RecvPacket (20000);
     s:='PASS dontknow'+CRLF;
     mysock.SendBuffer (pointer(s), length(s));
     setlength (s, mysock.WaitingData);
     if s<>'' then
      mysock.RecvBuffer (@s[1], length(s));
      mysock.CloseSocket;
      mysock.Free;
     end
  else
    begin
     showmessage( 'Error connection '+inttostr(Mysock.LastError)+' '+ MySock.LastErrorDesc);
    end;
end;

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: synapser send file by tcip
« Reply #2 on: April 14, 2012, 08:34:31 am »
If connecting to pcs directly to each other you would need cross cables yes.

I would first rule out problems on lower levels: can the pcs ping each other? Try exchanging data with netcat or an existing chat program and see if it works.
Make sure firewalls for the relevant ports are set to allow traffic.

Also: which operating system are you using?

I'll leave comments on the source code to others, but you're only showing the client part. What are you trying to connect to on the server part? An existing server? What kind of server: ftp, scp,... Or your own server?
The synapse website has examples; there are alos examples on the Synapse page in the Lazarus wiki... and there are probably quite some in this forum as well.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: synapser send file by tcip
« Reply #3 on: April 14, 2012, 10:23:34 am »
mySock.Accept; is only used on the server after a listen, never on a client.
The mySock clean up code is completely wrong. mySock is only closed and freed when mysock.WaitingData is >0 which will probably never happen because you check it immediately after a SendBuffer.
A basic tcp client looks like this:
Code: [Select]
var
  sock: TTCPBlockSocket;
  bufout,bufin: string;
begin
  sock := TTCPBlockSocket.Create;
  try
    sock.connect('192.168.1.103', '12345');
    if sock.LastError = 0 then
      begin
      // your code here. for example
      sock.SendString(bufout);
      bufin := sock.RecvPacket(3000);
      //
      sock.CloseSocket;
      end;
  finally
    sock.free;
  end;
end;
There is no reason to use mysock.ConvertLineEnd when you explicitly add CRLF yourself. It will also mess up binary data that you might want to transfer .

For a simple TCP server look at the echo demo that comes with synapse in the demos directory.

Quote
i dont know if only is posible to work with ip: 127.0.0.1 or localhost or i have to use cable cruz fot connect both pc he my source
Before worrying about cables, test your code by running your server and client on the same machine. Let the client connect to '127.0.0.1','12345'. Only when everything works well, try your program on 2 machines.
Cross over cables are needed on older hardware. More and more computer network interfaces integrate Auto-MDIX which means that you can use standard patch cables. Having one Auto-MDIX interface card on either side of the cable is enough.


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: synapser send file by tcip
« Reply #4 on: April 14, 2012, 11:41:01 am »
Cross over cables are needed on older hardware. More and more computer network interfaces integrate Auto-MDIX which means that you can use standard patch cables. Having one Auto-MDIX interface card on either side of the cable is enough.
Live and learn ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

eldonfsr

  • Sr. Member
  • ****
  • Posts: 446
Re: synapser send file by tcip
« Reply #5 on: April 15, 2012, 04:34:57 am »
yes i can ping pc to pc  here is code for server or machine where i going to receive the file.
Code: [Select]
procedure TFormAction.BRecibirClick(Sender: TObject);
var  i:integer;
  s,Ips:string;
  ListenerSocket, ConnectionSocket: TTCPBlockSocket;
begin
      ListenerSocket := TTCPBlockSocket.Create;
      try
        ListenerSocket.bind('192.168.1.103','12345');
        if ListenerSocket.LastError =0 then
        begin
          ListenerSocket.Accept;
          ListenerSocket.listen;
 //         i:=0;
 //         while ListenerSocket.CanRead(20000) do
 //         begin
 //          while i=0 do
 //           begin
 //             i:=ListenerSocket.WaitingData;
 //             showmessage(ListenerSocket.RecvPacket(10000)) ;
 //             Showmessage(ListenerSocket.RecvBufferStr(1000,10000));
 //           end;
 //           end;
            S:=ListenerSocket.RecvString(10000);
            while S<> '' do
            begin
                SynEdit.Lines.Append(S);
                S:=ListenerSocket.RecvString(10000);
                SynEdit.Refresh;
              end ;
          end
          else
          begin
             showmessage('Error ' + inttostr( ListenerSocket.LastError)+' '+listenerSocket.LastErrorDesc);
          end;
           listenerSocket.CloseSocket;
        finally
         ListenerSocket.Free;
        end;

      Synedit.Refresh;
end;   
when i use connect client pc dont get connect only when i use bind on server like this code the connection is good.  but i dont get to receive nothing of information, waitingdata always is 0. even in local mode.

thanks all for uours help.


ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: synapser send file by tcip
« Reply #6 on: April 15, 2012, 09:55:04 am »
For a simple TCP server look at the echo demo that comes with synapse in the demos directory.
Don't know where you got your code from but it would be a nice example of how not to do it ;)

First, before writing the first line of code, acquire some basic have to know knowledge on sockets: http://en.wikipedia.org/wiki/Berkeley_sockets

- ListenerSocket.bind('192.168.1.103','12345') : here you bind to one interface only, the one that has address '192.168.1.103'. Typically you'll want to listen on all interfaces. Use ListenerSocket.bind('0.0.0.0','12345')
- After a bind, listen is called to start accepting tcp connections. When an incoming connection is detected, the socket is signaled and the accept routine creates a new socket for the accepted connection. You have to use this new socket to handle the communication. So in short a server loop should look like this:
Code: [Select]
var
  ClientSock: TSocket;
  sock: TTCPBlockSocket;
begin
  sock := TTCPBlockSocket.Create;
  try
    sock.bind('0.0.0.0','12345');
    sock.listen;
    if sock.LastError <> 0 then
      exit;
    while not terminated do
    begin
      if sock.canread(1000) then
      begin
        ClientSock := sock.accept;
        if sock. lastError = 0 then
          TalkToClient(ClientSock);
      end;
    end;
  finally
    sock.Free;
  end;
end;
If you want to accept only one connection at a time,  TalkToClient can be a simple procedure. If you want multiple simultaneous connections, TalkToClient will have to hand over the ClientSock communication processing to a separate thread and return immediately. In both cases, the communication processing looks like this:
Code: [Select]
var
  s: string;
begin
  sock:=TTCPBlockSocket.create;
  try
    Sock.socket:=ClientSock ;
    sock.GetSins;
    with sock do
      begin
        repeat
          if client_terminated then break;
          s := RecvPacket(60000);
          if lastError<>0 then break;
             //create reply
          SendString(s);
          if lastError<>0 then break;
        until false;
      end;
  finally
    Sock.Free;
  end;
end;
Client_terminated is a boolean var used to close the connection from server side. A socket closed by the client will be detected by the lastError<>0 tests.

I repeat, look at the 'echo' demo in synapse. It is the simplest TCP server you can imagine that runs in the background and accepts multiple simultaneous connections.
« Last Edit: April 15, 2012, 10:56:11 am by ludob »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: synapser send file by tcip
« Reply #7 on: April 15, 2012, 12:21:02 pm »
  Having one Auto-MDIX interface card on either side of the cable is enough.

Afaik If you mess with very old hardware, (older 10mbit cards, or 10mbit ports of embedded systems) auto-mdi can cause problems, because of the limited discovery capabilties of the old hardware. Then you must use correct cabling and set the link size in device management (or ifconfig)

Systems I had this problem with were older powermacs (4400,7300 iirc it was a Farallon card), and a development board for the PIC97j60

 

TinyPortal © 2005-2018