[S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
BobPaw
Posts: 41
Joined: Dec 13, 2014 2:03
Location: Texas, USA

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby BobPaw » Jun 23, 2015 1:13

Examples for sending across localhost, wireless lan, and lan would be helpful. I can't seem to send text, though.
MrSwiss
Posts: 2975
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby MrSwiss » Jun 23, 2015 14:05

BobPaw wrote:Examples for sending across localhost, wireless lan, and lan would be helpful. I can't seem to send text, though.

It's all the same, isn't it?

Why don't you post your code that "doesn't work" here? Then, others might help to solve your problems.
Simply asking for examples appears to be rather "cheap" ... and ...

BTW: this forum section [Tips and Tricks] reads ... Source-code only - please, don't post questions here.
Tourist Trap
Posts: 2644
Joined: Jun 02, 2015 16:24

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby Tourist Trap » Jun 23, 2015 15:40

MrSwiss wrote:(...)

Strictly speaking there is no question in what has been said by BobPaw. Just a remark related to some need for some more lan network related examples.
grindstone
Posts: 639
Joined: May 05, 2015 5:35
Location: Germany

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby grindstone » Jun 23, 2015 21:35

Tourist Trap wrote:Strictly speaking there is no question in what has been said by BobPaw.
That's hairsplitting. The hint means "don't post anything but source code here".

Regards
grindstone
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby D.J.Peters » Sep 23, 2015 11:48

Get any google satellite maps by latitude and longitude:
idea from chung "free air / marine combat flight simulator"

Joshy

zoomed over my garden :-)
Image
file: sncGetGoogleMaps.bas

Code: Select all

' [S]imple [N]etwork [C]onnection
#include once "snc.bi"
#include once "snc_utility.bi"

var typejpg = "jpg-baseline"
var mapstyle = "feature:road%7Cvisibility:off&style=element:labels%7Cvisibility:off&style=element:geometry.stroke%7Cvisibility:off"

' !!! change it !!!
var lat=38.8976763 ' white house from top :-)
var lng=-77.0365298
var zoom=20
var scale=1
var size=1024

var path="/maps/api/staticmap?center=" & lat & "," & lng & _
         "&zoom="  & zoom & _
         "&scale=" & scale & _
         "&size="  & size & "x" & size & _
         "&maptype=satellite" & _
         "&format=" & typejpg & _
         "&style=" & mapstyle


' test of a client connection
var ServerName = "maps.googleapis.com"
var ServerPath = path
var ServerFile = "test_" & lat & "_" & lng & ".jpg"

' connect to web server at port 80
dim as NetworkClient client = type(ServerName,80)

' get a connection from ConnectionFactory
var connection = client.GetConnection()

' build an HTTP GET request
dim as string request = HTTPGet(ServerName,ServerPath,"")

' ready to send ?
while connection->CanPut()<>1
  sleep 100
wend
' put data on the connection
connection->PutData(strptr(request),len(request))

' ready to receive ?
while connection->CanGet()<>1
  sleep 100
wend
print "receive " & ServerFile
dim as zstring ptr buffer
var nBytes = connection->GetData(buffer)
print "number of received bytes " & nBytes
' get last char position of the HTTP asci header
var LastChar=instr(*buffer,HeaderEnd)-1
var Header  =left(*buffer,LastChar)
' is it a OK answer ?
if instr(Header,"200 OK")<1 then
  print "can't get " & ServerName & ServerPath & " !"
  beep:sleep:end
end if
' get first byte behind the HTTP header
var DataStart=LastChar+4
' save it
open ServerFile for binary access write as #1
dim as ubyte ptr FileBuffer=@buffer[DataStart]
nBytes-=DataStart
put #1,,*FileBuffer,nBytes
close #1
print "file saved ..."
' free the buffer (allocate by snc.bi)
deallocate buffer
sleep
Last edited by D.J.Peters on Sep 25, 2017 16:23, edited 2 times in total.
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby D.J.Peters » Sep 23, 2015 20:58

https://developers.google.com/maps/docu ... maps/intro

One more static map test.

Joshy

Code: Select all

'' [S]imple [N]etwork [C]onnection
#include once "snc.bi"
#include once "snc_utility.bi"

const as string HOST            = "maps.googleapis.com"
const as string PATH            = "/maps/api/staticmap?"

'const as string MAP_TYPE        = "&maptype=satellite"
const as string MAP_TYPE        = "&maptype=roadmap"
'const as string MAP_TYPE        = "&maptype=terrain"
'const as string MAP_TYPE        = "&maptype=hybrid"

const as string MAP_ZOOM        = "&zoom=18"

const as string MAP_SCALE       = "&scale=2"

const as string IMAGE_SIZE      = "&size=512x512" ' !!! multiply by MAP_SCALE of 2 = 1024x1024 pixels !!!

'const as string IMAGE_FORMAT    = "&format=gif"
const as string IMAGE_FORMAT    = "&format=png"
'const as string IMAGE_FORMAT    = "&format=jpg"
'const as string IMAGE_FORMAT    = "&format=jpg-baseline"

const as string STYLE_ROAD      = "&style=feature:road%7Cvisibility:on" ' or off

const as string STYLE_LANDSCAPE = "&style=feature:landscape%7Celement:geometry%7Csaturation:100"

const as string STYLE_WATER     = "&style=feature:water%7Csaturation:-100%7Cinvert_lightness:false" ' or true

const as string STYLE_LABELS    = "&style=element:labels%7Cvisibility:on" ' or off

const as string STYLE_GEOMETRY  = "&style=element:geometry.stroke%7Cvisibility:on" ' or off

const as string STYLE_ALL = STYLE_LANDSCAPE & STYLE_ROAD & STYLE_WATER & STYLE_LABELS & STYLE_GEOMETRY

' use any latitude and longitude
const lat= 38.8976763 ' white house
const lng=-77.0365298
'const as string CENTER = "center=" & lat & "," & lng

' or a address
const as string CENTER = "center=USA+white+house"

var REQUEST = CENTER       & _
              IMAGE_SIZE   & _
              IMAGE_FORMAT & _
              MAP_TYPE     & _
              MAP_ZOOM     & _
              MAP_SCALE    & _
              STYLE_ALL


' save as ... gif/png/jpg
var LocalFile = "test.png"

' connect to web server at port 80
dim as NetworkClient client = type(HOST,80)

' get a connection from ConnectionFactory
var connection = client.GetConnection()

' build an HTTP GET request
var strRequest = HTTPGet(HOST,PATH & REQUEST)

' ready to send ?
while connection->CanPut()<>1
  sleep 100
wend
' put data on the connection
connection->PutData(strptr(strRequest),len(strRequest))

' ready to receive ?
while connection->CanGet()<>1
  sleep 100
wend

print "receive " & LocalFile
dim as zstring ptr buffer
var nBytes = connection->GetData(buffer)
print "number of received bytes " & nBytes

' get last char position of the HTTP asci header
var LastChar=instr(*buffer,HeaderEnd)-1
var Header  =left(*buffer,LastChar)
' is it a OK answer ?
if instr(Header,"200 OK")<1 then
  print "can't get map from " & HOST & PATH & " !"
  beep:sleep:end
end if

' get first byte behind the HTTP header
var DataStart=LastChar+4

' save it
open LocalFile for binary access write as #1
dim as ubyte ptr FileBuffer=@buffer[DataStart]
nBytes-=DataStart
put #1,,*FileBuffer,nBytes
close #1
print "file saved ..."
' free the buffer (allocate by snc.bi)
deallocate buffer
sleep

Dinosaur
Posts: 1148
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby Dinosaur » Oct 31, 2015 17:42

Hi All

Joshy, I am setting up a network for which your SNC may work.
Your opinion appreciated.

3 PC's with Eth Running Linux Mint have to connect to a 4th PC (also Linux Mint) with an Eth port.
All 4 are running an FB application.
The 4th PC has GSM Modem to send emails.(I have that working)
No connection to the Net other then the Emails out.

Each of the three PC's will send a Text file 2 or three times a day to the 4th PC from within the FB application.

Before I start using your library, do you see any obstacles ?

EDIT:
an example of the settings would get me even more excited ;)
I am using the basic ip addr on each : 192.168.0.1 to 4 and will have to put a hub inbetween the slaves & Master.
I did test your example and it compiled and worker straight away.
Other Linux tests that I have done became more complicated with passwords.

Regards
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby D.J.Peters » Nov 02, 2015 0:33

pseudo code

PC4 = 198.168.0.4
file: modemserver.bas

Code: Select all

' PC4 = modem server
#define PORT 12345 ' use a free port >=10000
var Server = NetworkServer(PORT)
var runing = true
while running
  var Connection = Server.GetConnection()
  if Connection then
    while Connection->CanGet()=0
      sleep 100
    wend
    dim as zstring ptr message=0
    var status = Connection->Get(message)
    if message then
       ModemSend(message)
       deallocate message
    end if
  end if
  if inkey()="q" then running=false
  sleep 500
wend


PC1 198.168.0.1
PC2 198.168.0.2
PC3 198.168.0.3
PC4 198.168.0.4 <-- you need only the server IP and PORT
file: modemclient.bas

Code: Select all

#define IP "192.168.0.4"
#define PORT 12345 ' use server IP and PORT
var Client = NetworkClient(IP,PORT)
var Connection = Client.GetConnection()
while Connection->CanPut()=0
  sleep 100
wend
var emailstuff = "bla bla ..."
var size = len(emailstuff)+1
Connection->Put(strptr(emailstuff),size)
Dinosaur
Posts: 1148
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby Dinosaur » Nov 03, 2015 2:17

Hi All

Joshy you are a champion.
That has solved my networking problem.

Now I have to integrate it into my application, but after testing the little snippet you
sent (with some little mods) I am happy it will do what I want.

Once again , many thanks

Regards
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby D.J.Peters » Nov 03, 2015 2:53

Some short Tips:

In case of a client:
if connection is 0 that means the server isn't running or busy
In case of a server:
if connection is 0 that means no new client has connected.

In case of both server and client:
after a successful connection->CanPut() or connection->CanGet()
if connection->Get() or connection->Put() returns 0
that means client or server closed the connection (disconnected).

Joshy

pseudo code

Code: Select all

 ...
var Client = NetworkClient(IP,PORT)
dim as integer timeout = 10 ' seconds
var Connection = Client.GetConnection()
while Connection=0 and timeout>0
  sleep 1000:timepout-=1
  Connection = Client.GetConnection()
wend
if timeout<0 then
  print "server " & IP & " is busy try again later !"
  beep : sleep : end
end if

while Connection->CanGet()=0
   sleep 100
wend
...
Last edited by D.J.Peters on Nov 03, 2015 3:26, edited 3 times in total.
BasicCoder2
Posts: 3344
Joined: Jan 01, 2009 7:03

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby BasicCoder2 » Nov 03, 2015 3:02

Joshy this looks like something for the Project section?
Wish I had the expertise to know what it actually does and how t use it but it looks good.
.
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby D.J.Peters » Nov 03, 2015 3:21

BasicCoder2 wrote:Joshy this looks like something for the Project section?
No isn't a project is only a tip with source code how to use FreeBASIC latest network include files for Windows, Linux/Unix, ARM and in near future MAC also.
BasicCoder2 wrote:Wish I had the expertise to know what it actually does and how t use it but it looks good..
No problem ask if you like to use it.

If you need to send or receive data over a network (internet or local or network)

Simple Network Connection is the right solution for FreeBASIC.

Joshy
Dinosaur
Posts: 1148
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby Dinosaur » Nov 03, 2015 18:24

Hi All

Joshy, thank you for the additions, when I complete the entire module I will post it here for others to
use or criticise.

What settings would I have to change to add a hub in between the 3 clients and the Server ?

Regards

EDIT: I think I have answered my own question. I just connected both to a hub, and they found each other
BRILLIANT.
Dinosaur
Posts: 1148
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby Dinosaur » Nov 03, 2015 22:06

Hi All

Joshy, the addition of Timeout does not get a chance to work.
If I connect the two, and send a test Message, all is OK.
If I then close the server program, and run the Client again, I get an Exit Code 141.

Code: Select all

#include once "snc.bi"
#include once "snc_utility.bi"

'file: client.bas
   
#define IP "192.168.0.2"
#define PORT 12345 ' use server IP and PORT
   'First see if we can connect to Server.
   var Client = NetworkClient(IP,PORT)
   dim as integer timeout = 10
   var Connection = Client.GetConnection()
   while Connection = 0 and timeout > 0
     sleep 1000:timeout -= 1
     Connection = Client.GetConnection()
   wend
   if timeout < 0 then
     print "server " & IP & " is busy try again later !"
     beep : sleep : end
   end if
   'If we got to here, then slight pause
   while Connection->CanPut()=0
     sleep 100
   wend
   var emailstuff = "Test Message"
   var size = len(emailstuff)+1
   Connection->PutData(strptr(emailstuff),size)


Any ideas ?
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: [S]imple [N]etwork [C]onnection win and lin 32/64-bit.

Postby D.J.Peters » Nov 04, 2015 3:28

Dinosaur wrote:Any ideas ?
Sure in a real application you have ever to check for errors. EVER and EVER and EVER ... :-)
You have to test the result of NetworkClient() before you ask for a new connection.

Joshy

Code: Select all

#include once "snc.bi"
#include once "snc_utility.bi"

'file: client.bas
#define IP "192.168.0.2"
#define PORT 12345 ' use server IP and PORT

' First see if we can connect to Server.

print "connect to server " & IP & " at port " & PORT & " wait ..."
var Client    = NetworkClient(IP,PORT)
var ErrorCode = Client.GetLastError()
if ErrorCode <> ERROR_NO_ERROR then
  if ErrorCode = ERROR_RESOLVE then
    print "error: can't resolve server IP"
  elseif ErrorCode = ERROR_CONNECT then
    print "error: no reply from server"
  else
    print "unknow error: " & GetNetworkErrorText(ErrorCode)
  end if
  beep: sleep: end 1
end if

print "connected ..."
print


print "get server coneection ..."
dim as integer timeout = 10
var Connection = Client.GetConnection()
while Connection = 0 and timeout > 0
  sleep 1000:timeout -= 1
  Connection = Client.GetConnection()
wend
if timeout < 0 then
  print "can't get connection  time out !"
  ErrorCode = Client.GetLastError()
  if ErrorCode <> ERROR_NO_ERROR then
    print "error: description: " & GetNetworkErrorText(ErrorCode)
  end if
  beep: sleep: end 2
end if

print "wait to send:"
' If we got to here, then slight pause
while Connection->CanPut()=0
  sleep 100
wend

print "send data:"
var emailstuff = "Test Message"
var size = len(emailstuff)+1
var status = Connection->PutData(strptr(emailstuff),size)
if (status<0) then
  ErrorCode = Client.GetLastError()
  if ErrorCode <> ERROR_NO_ERROR then
    print "error: PutData() description: " & GetNetworkErrorText(ErrorCode)
  end if
  beep: sleep: end 3
end if
print "all ok ..."
sleep

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 4 guests