Communication between computers on LAN lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- julinalex07 · 2015-05-28 06:56 Hello Guys, I was working on a simple program that should be able to communicate to it's sibling program that is installed on another computer in the same lan.. There are only a few differences between these two programs. I have googling for a few days now, but am not able to find any example or tutorial that is helpful. I know about synapse and i have already used it for a couple of times for serial port communication but i dont know if it can be used in this case , i don't even know what protocol i should use ,tcp or udp.. or should i save the data to be transmitted onto a file and sent it over ftp.... Any help would be really appreciated Thanks in advance ---------------------------------------------------------------------- jma_sp · 2015-05-28 07:12 Hello julianalex07, as you says ararat synapse should work for it but its seem to be complex. I only have used  it to echo (ping) to other computers in lan..., also i have been using serial for reading info from mobile to test how it works (contacts, imei, ....) (under windows assigning the exact port and with drivers of device previously installed). I know is not the same, but an alternative is to use an intermediate database server as MariaDB or Postgres, and interchange information among two or more equipments in the local net. It gets easy access from/to all computers in your local net as clients of a server. I you finally will be able to have two working equipments in net, by example by tpc/ip point to point it will be very interesting for a lot of people. Best Regards. Saludos. ---------------------------------------------------------------------- rvk · 2015-05-28 07:52 You can definitely use synapse for this. It's not that hard, although transfer via file (smb) might be simpler. A file via ftp is about as much complex/simple as a direct TCP-connection. It all depends on your requirements. If you need to send data back and forth (real-time communication) you should use a TCP-connection. If you just want to push a string to another computer (without knowing 100% if it arrives) you can use UDP. ---------------------------------------------------------------------- avra · 2015-05-28 08:34 https://code.google.com/p/palabre-messenger/ ---------------------------------------------------------------------- julinalex07 · 2015-05-28 17:38 Thanks for the reply guys  I have been using Lazarus for a long time , but i am really new into this networking stuff I went through the palabre messenger source but it is really complex with hashing , authentication and stuff like that and i don't really understand anything. I thought about using some sort of intermediate storage or db but had no idea about implementing this so left that, if anyone can show me within example it would be really great  Same with TCP in synapse , if someone can provide an example (simple ones ) or even just steps ( like , create subject , connect to host like that )on how to achieve that , it would be really appreciated guys. Thanks in advance ---------------------------------------------------------------------- Paul Breneman · 2015-05-28 18:35 Welcome julinalex07 to the Lazarus forums! My (biased) recommendation is that you take a look at the TCP/IP stuff in Control Terminal (it uses Synapse): http://ctrlterm.com/ ---------------------------------------------------------------------- rvk · 2015-05-28 21:54 Quote from: julinalex07 on May 28, 2015, 07:38:32 pmif someone can provide an example (simple ones ) or even just steps ( like , create subject , connect to host like that )on how to achieve that , it would be really appreciated guys. I guess all those complete programs are very difficult to decipher. I understand you like a small simple project which shows you how to setup a TCP connection on both end. I threw something together which does just that (see project attached). For Windows at the moment but converting it to Linux shouldn't be a problem. First you need a thread for listening for an incoming connection (click the Listen button on one computer). I used a button because for testing on my computer I just used one PC with a connection to 127.0.0.1 (localhost). You would have this on different computers and you could start Listen on both computers without a problem. On one of the computers you can click Connect. A connection is made to the given-IP. (Of course that computer needs to be "listening".) Then, after the listening thread sees an incoming message, it signals the main form to create a TCPBlockSocket. That form, in turn, creates a Receiving thread. I tried receiving via a TTimer but that just slows things down. When the receiving thread receives a string it signals the main form to add it to the memo. For sending a string you can just use the just created TTCPBlockSocket. The receiving thread also creates it's own TCPBlockSocket with the socket from the main form. It kind of looks complex with those two threads but the listening thread is only used for listening for an incoming connection and the receiving thread is used for receiving in the background (otherwise your program is constantly checking for incoming traffic). (b.t.w. I added blcksock to my project but maybe it's in a different path than yours so you might need to change it) ---------------------------------------------------------------------- Jurassic Pork · 2015-05-29 00:03 hello, there is also the Lnet library to communicate between computers on Lan. 1 - Download the library 2 - Install the packages lnetbase and lnetvisual 3 - Open the project examples/visual/tcpudp 4 - Build the project Now you have a program to communicate on lan. The program can be client or server. Click on the host button to be server. Friendly, J.P ---------------------------------------------------------------------- julinalex07 · 2015-05-29 04:28 Thanks a bunch guys , Now i think i can build the program.. Again thanks a lot guys ---------------------------------------------------------------------- jma_sp · 2015-05-29 07:44 Thanks Jurassic Pork, i will try  Lnet Library in a little project to connect two computers and learn how it works. Also  Paul Breneman with ctrlterm.com, best began with simple programs and it seems a good example, thanks. avra palabre-messenger is too complicated to me by the moment but thanks. I will look if all of then are listed in softare related to lazarus/freepascal. ulinalex07 What was you planning to create? A simple chat? or a simple game? as an idea you can implement a simple battleship game ---------------------------------------------------------------------- macmike · 2015-05-29 10:30 I'd been thinking of asking a similar question but the incredibly helpful replies in this thread have just answered it for me - thanks ---------------------------------------------------------------------- Zittergie · 2015-06-01 19:40 Rvk:  is it possible to talk to android and ios devices this way.  Do you have experience with android development to send msg from phone/tablet to Lazarus App? ---------------------------------------------------------------------- rvk · 2015-06-01 20:00 I don't have experience with Android/iOS development but yes, you can communicate over a TCP connection with Android/iOS. I don't know what you need to use on the mobile-side but any library which does TCP-connection should be able to connect to your Lazarus side. It doesn't matter if you use Synapse or lnet at the Lazarus side. (But of course if you're outside of your own network you need to configure you router correctly and open up/forward the appropriate ports) For the Android side: http://forum.lazarus.freepascal.org/index.php?topic=16001.0 https://www.youtube.com/watch?v=_emMNy2e8JU http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-Android-and- network-td4031396.html (so I gather that for Android you should also be able to use both Synapse and lnet) ---------------------------------------------------------------------- taazz · 2015-06-01 20:07 Quote from: rvk on June 01, 2015, 10:00:41 pmI don't have experience with Android/iOS development but yes, you can communicate over a TCP connection with Android/iOS. I don't know what you need to use on the mobile-side but any library which does TCP-connection should be able to connect to your Lazarus side. It doesn't matter if you use Synapse or lnet at the Lazarus side. (But of course if you're outside of your own network you need to configure you router correctly and open up/forward the appropriate ports) I'm looking for any documentation that explains how a server less chat program might work and how a chat application would be set to work over http. If you have any to recommend would be eternally grateful. Quote from: rvk on June 01, 2015, 10:00:41 pmFor the Android side: http://forum.lazarus.freepascal.org/index.php?topic=16001.0 https://www.youtube.com/watch?v=_emMNy2e8JU http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-Android-and- network-td4031396.html (so I gather that for Android you should also be able to use both Synapse and lnet) and indy since android is linux based shouldn't be a problem. ---------------------------------------------------------------------- Zittergie · 2015-06-01 20:09 OK, Thanks. I let you know what i come up with. In the mean time it works fine on MacOS, Windows and Linux ARM. See youtube:  https://m.youtube.com/watch?v=ovSGkMuce1Q ---------------------------------------------------------------------- rvk · 2015-06-01 20:48 Quote from: taazz on June 01, 2015, 10:07:31 pmQuote from: rvk on June 01, 2015, 10:00:41 pm(But of course if you're outside of your own network you need to configure you router correctly and open up/forward the appropriate ports) I'm looking for any documentation that explains how a server less chat program might work and how a chat application would be set to work over http. Do you mean in combination with traffic through a router? Real "server less" chat outside your own network would always need an intermediate server to channel traffic-information. Once the connection with that server is established it could connect both clients with each other without the intermediate server in the middle. But that depends on the setup of the routers. Some routers are set so strict even that wouldn't work and in that case all traffic needs to go over that server. (You can see this with LogMeIn and Teamviewer. Sometimes a direct connection between two clients can't be established and then traffic will flow over their servers instead of direct from point to point.) In the end, it doesn't matter which port the chat application uses. If the communication is done correctly it shouldn't be a problem to use http-port for it. As an example you could look at the description of chownat (and linked papers about NAT Traversal): http://samy.pl/chownat/ and http://samy.pl/pwnat/ "Autonomous NAT Traversal"-paper It says it can create a connection between two computers both behind a router without port forwarding. Of course in that example one machine's IP would be known. In a server less chat both IP's are not known and in that case you could need that intermediate server (which in turn can poke a whole in the routers NAT to let the connection through). Nat traversal wiki As you can see... I don't have hands on (practical) experience in doing NAT traversal in Pascal but I have investigated the underlying theory in the past. ---------------------------------------------------------------------- taazz · 2015-06-01 21:32 Quote from: rvk on June 01, 2015, 10:48:37 pmQuote from: taazz on June 01, 2015, 10:07:31 pmQuote from: rvk on June 01, 2015, 10:00:41 pm(But of course if you're outside of your own network you need to configure you router correctly and open up/forward the appropriate ports) I'm looking for any documentation that explains how a server less chat program might work and how a chat application would be set to work over http. Do you mean in combination with traffic through a router? Real "server less" chat outside your own network would always need an intermediate server to channel traffic-information. Complete and outer server less is not going to work most ISPs block those high traffic broadcast addresses and ports if by Traffic information you mean ip addresses and communication details then yes we seem to be on the same wave length here. Of course that is the easy part any http server could handle that with a small database that would monitor activity using some sort of "heart bit" implementation. Nothing really complicated here I guess (and since its a http server the "over http" part is covered). Quote from: rvk on June 01, 2015, 10:48:37 pm Once the connection with that server is established it could connect both clients with each other without the intermediate server in the middle. Lets take the router out the equation for a moment so I can understand the frame on top of which we are building. Both clients have connected with the server using http then the server sends the current ip addresses of those clients to each other ee client1's ip to client2 and client2's ip to client1. Now either client1 or client2 or both should open a port and wait for connections while at the same time trying to connect to the remote IP on the same port? (simplistic I know) Quote from: rvk on June 01, 2015, 10:48:37 pmBut that depends on the setup of the routers. Some routers are set so strict even that wouldn't work and in that case all traffic needs to go over that server. I'm not clear as to what you mean by "that" does the server do anything more than exchange ip addresses and other info (eg some public keys for cryptography), something to fool the routers to think that the connection was requested from the client and allow it? Quote from: rvk on June 01, 2015, 10:48:37 pm (You can see this with LogMeIn and Teamviewer. Sometimes a direct connection between two clients can't be established and then traffic will flow over their servers instead of direct from point to point.) In the end, it doesn't matter which port the chat application uses. If the communication is done correctly it shouldn't be a problem to use http-port for it. It should, I know routers that block all packets except http ee no nntp. smtp, pop3, imap etc so when I say over http I mean not only port 80 but http communication (get, set, put etc) this is to make sure that the more strict routers that allow only http packets to pass and block all others are letting us communicate. Quote from: rvk on June 01, 2015, 10:48:37 pm As an example you could look at the description of chownat (and linked papers about NAT Traversal): http://samy.pl/chownat/ and http://samy.pl/pwnat/ "Autonomous NAT Traversal"-paper It says it can create a connection between two computers both behind a router without port forwarding. Of course in that example one machine's IP would be known. In a server less chat both IP's are not known and in that case you could need that intermediate server (which in turn can poke a whole in the routers NAT to let the connection through). Nat traversal wiki As you can see... I don't have hands on (practical) experience in doing NAT traversal in Pascal but I have investigated the underlying theory in the past. Those are enough for now thank you. ---------------------------------------------------------------------- rvk · 2015-06-01 22:11 Quote from: taazz on June 01, 2015, 11:32:42 pmQuote from: rvk on June 01, 2015, 10:48:37 pm Once the connection with that server is established it could connect both clients with each other without the intermediate server in the middle. Lets take the router out the equation for a moment so I can understand the frame on top of which we are building. Both clients have connected with the server using http then the server sends the current ip addresses of those clients to each other ee client1's ip to client2 and client2's ip to client1. Now either client1 or client2 or both should open a port and wait for connections while at the same time trying to connect to the remote IP on the same port? (simplistic I know) Yes. That's the most simplest form. But taking the router out of the equation is the most problematic Quote from: taazz on June 01, 2015, 11:32:42 pmQuote from: rvk on June 01, 2015, 10:48:37 pmBut that depends on the setup of the routers. Some routers are set so strict even that wouldn't work and in that case all traffic needs to go over that server. I'm not clear as to what you mean by "that" does the server do anything more than exchange ip addresses and other info (eg some public keys for cryptography), something to fool the routers to think that the connection was requested from the client and allow it? Yes, if you include the routers again in the equation... the server could be used to puncture the whole into the router. Most routers are setup to allow traffic from ALL outside IP's once a internal client has established a connection with an outside machine. This is the simplest method of punching. The inside client makes a connection with a server and an outside client can piggy back on that same port back to the inside client. But other routers are setup more strict and only allow traffic from that same IP to where an inside client has connected. If the outside client is behind a less strict server then the inside client can again connect directly to the outside client. But when both are behind a more strict router you have a problem. Neither client can get a direct connection with each other. The router2 doesn't allow client1 to connect to client2 and router1 doesn't allow client2 to connect to client1. And no method of whole punching can help there at which point the server need to be mediator. (But I seem to remember that there are several puncture techniques so maybe more than one should be used if the other fails) Quote from: taazz on June 01, 2015, 11:32:42 pmQuote from: rvk on June 01, 2015, 10:48:37 pm(You can see this with LogMeIn and Teamviewer. Sometimes a direct connection between two clients can't be established and then traffic will flow over their servers instead of direct from point to point.) In the end, it doesn't matter which port the chat application uses. If the communication is done correctly it shouldn't be a problem to use http-port for it. It should, I know routers that block all packets except http ee no nntp. smtp, pop3, imap etc so when I say over http I mean not only port 80 but http communication (get, set, put etc) this is to make sure that the more strict routers that allow only http packets to pass and block all others are letting us communicate. Yeah. That's really strict. Almost as strict as just using a http- proxy. Those are the hardest. If you do have a server (mediating) you might just want to use http-traffic for the complete chat. I'm not sure if you can use the whole punching technique with these kind of firewalls. They (should) normally check if the endpoint stays the same. So in that case you would need to keep using the http-server for the entire chat. (And in case of a proxy this technique is not possible at all and you'll always need the server to channel traffic) As for the format... You could just use a TCP-component and send (dummy) http-headers over port 80 followed by a binary package. I think you can fool the firewalls which just checks for HTTP-traffic. ---------------------------------------------------------------------- taazz · 2015-06-01 23:08 Quote from: rvk on June 02, 2015, 12:11:50 amQuote from: taazz on June 01, 2015, 11:32:42 pmQuote from: rvk on June 01, 2015, 10:48:37 pm Once the connection with that server is established it could connect both clients with each other without the intermediate server in the middle. Lets take the router out the equation for a moment so I can understand the frame on top of which we are building. Both clients have connected with the server using http then the server sends the current ip addresses of those clients to each other ee client1's ip to client2 and client2's ip to client1. Now either client1 or client2 or both should open a port and wait for connections while at the same time trying to connect to the remote IP on the same port? (simplistic I know) Yes. That's the most simplest form. But taking the router out of the equation is the most problematic Quote from: rvk on June 02, 2015, 12:11:50 amQuote from: taazz on June 01, 2015, 11:32:42 pmQuote from: rvk on June 01, 2015, 10:48:37 pmBut that depends on the setup of the routers. Some routers are set so strict even that wouldn't work and in that case all traffic needs to go over that server. I'm not clear as to what you mean by "that" does the server do anything more than exchange ip addresses and other info (eg some public keys for cryptography), something to fool the routers to think that the connection was requested from the client and allow it? Yes, if you include the routers again in the equation... the server could be used to puncture the whole into the router. Most routers are setup to allow traffic from ALL outside IP's once a internal client has established a connection with an outside machine. This is the simplest method of punching. The inside client makes a connection with a server and an outside client can piggy back on that same port back to the inside client. so something along the lines of  client1 open connection on port 4096 on server1. server1 acknowledged connection accepted. server1 inform client2 that port 4096 is open, client2 open connection on port 4096 on client1. ? Quote from: rvk on June 02, 2015, 12:11:50 am But other routers are setup more strict and only allow traffic from that same IP to where an inside client has connected. Well I haven't see any router that ignores outside connection requests, to accept any connection from any one even the server that has already been connected by the client pc in the lan if the http server trys to open a port back on the client that made a data request to it then the client should never receive it the router should stop it. Quote from: rvk on June 02, 2015, 12:11:50 amIf the outside client is behind a less strict server then the inside client can again connect directly to the outside client. But when both are behind a more strict router you have a problem. Neither client can get a direct connection with each other. The router2 doesn't allow client1 to connect to client2 and router1 doesn't allow client2 to connect to client1. And no method of whole punching can help there at which point the server need to be mediator. so far that is my experience with a small exception UPNP. If the router supports UPNP and it is enabled then the client application should be able to add an exception on the router to forward the connection to a port to the client machine on the network. But yeah that is mostly a home router setup not a corporate one.  Quote from: rvk on June 02, 2015, 12:11:50 am (But I seem to remember that there are several puncture techniques so maybe more than one should be used if the other fails) any technique is good to know so if you have any reference or even a title, short description or name of a technique so I can google it would be grate. thanks for your time so far. Quote from: rvk on June 02, 2015, 12:11:50 am Quote from: taazz on June 01, 2015, 11:32:42 pmQuote from: rvk on June 01, 2015, 10:48:37 pm(You can see this with LogMeIn and Teamviewer. Sometimes a direct connection between two clients can't be established and then traffic will flow over their servers instead of direct from point to point.) In the end, it doesn't matter which port the chat application uses. If the communication is done correctly it shouldn't be a problem to use http-port for it. It should, I know routers that block all packets except http ee no nntp. smtp, pop3, imap etc so when I say over http I mean not only port 80 but http communication (get, set, put etc) this is to make sure that the more strict routers that allow only http packets to pass and block all others are letting us communicate. Yeah. That's really strict. Almost as strict as just using a http- proxy. Those are the hardest. If you do have a server (mediating) you might just want to use http-traffic for the complete chat. I'm not sure if you can use the whole punching technique with these kind of firewalls. They (should) normally check if the endpoint stays the same. So in that case you would need to keep using the http-server for the entire chat. (And in case of a proxy this technique is not possible at all and you'll always need the server to channel traffic) As for the format... You could just use a TCP-component and send (dummy) http-headers over port 80 followed by a binary package. I think you can fool the firewalls which just checks for HTTP-traffic. Isn't that how the images are transfered from http to client? Why would I need a dummy header if I can simple use the binary data transfer? So far I haven't seen a router that only allows html but I've heard stories about routers that cut off javascript from the incoming html documents. ---------------------------------------------------------------------- Paul Breneman · 2015-06-02 00:20 I use Hamachi (http://vpn.net/) and Skype, and I think this is the simple explanation of what they do: http://en.wikipedia.org/wiki/UDP_hole_punching ---------------------------------------------------------------------- taazz · 2015-06-02 05:32 Quote from: Paul Breneman on June 02, 2015, 02:20:07 amI use Hamachi (http://vpn.net/) and Skype, and I think this is the simple explanation of what they do: http://en.wikipedia.org/wiki/UDP_hole_punching thank you. interesting reading