Sending data over the Internet? (Networking question)

General FreeBASIC programming questions.
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

Re: Sending data over the Internet? (Networking question)

Post by squall4226 »

OOO I got it to send a message. Sort of. It only happened when I closed the client, but the server did receive the string "Test" with this code:

Code: Select all

#Include Once "snc.bi"
#Include Once "fbgfx.bi"
Using FB
ScreenRes 800,600,16
Dim shared result As Integer
Dim Shared As String tosend
tosend = "test"

var Client = NetworkClient("127.0.0.1",5469)
var ServerConnection = Client.GetConnection()

Do While Not MultiKey(SC_ESCAPE)
Sleep 2000,1
result = ServerConnection->PutData(StrPtr(tosend),5)
Print "Sent " & tosend
Print result
Print "______________"
loop
Edit: Success almost!

Image

By changing the length from 5 to 4, it sends and receives data but the string has a weird character at the end. The SNC source says the second argument of put data is the length in bytes, but that it's LEN+1 for strings. But apparently that was why it was only completing when I closed it - it was waiting for an extra byte.

Very close. Thanks for all help so far!
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

Re: Sending data over the Internet? (Networking question)

Post by squall4226 »

Full success! Thank you guys for your help as always! I shall try to integrate into my main program now.

Edit: Wait, it won't work across the net. It works on my LAN (even from another PC on the LAN) but not across the net. I have the port in question 5469 forwarded for both TCP and UDP in my router. So IDK. Hmmm.

For more info, I have determined when trying to connect across the Internet it gets stuck here:

Code: Select all

while ServerConnection->CanPut() = 0
		Print "Can't put"
		sleep 100
	Wend
I can tell because it spams Can't put.

It works on localhost, it works on my LAN from multiple machines, but yeah, not across the net ><

Edit again: Got it working 100% Thanks!
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Sending data over the Internet? (Networking question)

Post by badidea »

squall4226 wrote:... It only happened when I closed the client ...
There seems to a delay in data transmission, discussed here as well: http://www.freebasic.net/forum/viewtopi ... 30#p213332
If I let the client sent 10 text strings, they are received as a big blob of data 1-2 seconds later. Which makes sense, I guess, for transmitting a the contents of a html page across the network, but less for controlling for your keyboard connected relays. Setting up a new connection for each packet seems to work, but does not sound so nice.

Your string issue is probably related to BASIC strings vs. C strings (zstring).
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

Re: Sending data over the Internet? (Networking question)

Post by squall4226 »

Well now it's working for me with no delay on my lan. Here is the code I'm currently using:
Server:

Code: Select all

#Include Once "snc.bi"
#Include Once "fbgfx.bi"
Using FB
ScreenRes 800,600,16
Dim As Integer status

Dim Shared As zstring Ptr dataptr

var Server = NetworkServer(5469)
Do While Not MultiKey(SC_ESCAPE)
   var ClientConnection = Server.GetConnection()
   If ClientConnection Then
      while ClientConnection->CanGet() = 0
      	Print "Can't get"
         sleep 100
      wend
       status = ClientConnection->GetData(dataptr)
         Print status, *dataptr
         Print "______________________"
   End if
Loop
Client:

Code: Select all

#Include Once "snc.bi"
#Include Once "fbgfx.bi"
Using FB
ScreenRes 800,600,16
Dim shared result As Integer
Dim Shared As String tosend
Dim Shared As UInteger length
Dim Shared As String IP
Input "IP Address to connect to: ", IP


Do While Not MultiKey(SC_ESCAPE)
	Input "String to send: ", tosend
	If UCase(tosend) = "EXIT" Then end
	length = Len(tosend)
	Var Client = NetworkClient(IP,5469)
	var ServerConnection = Client.GetConnection()
	while ServerConnection->CanPut() = 0
		Print "Can't put"
		sleep 100
		If MultiKey(SC_ESCAPE) Then end
	Wend
	result = ServerConnection->PutData(StrPtr(tosend),length+1)
	Print "Sent " & tosend
	Print result
	Print "______________"
Loop
Moving the:

Code: Select all

   Var Client = NetworkClient(IP,5469)
   var ServerConnection = Client.GetConnection()
Inside the loop is what caused it to start receiving data even before I closed the program. Though I don't know if this is the right way, I do know my friend in the U.K. sent me a message with it in the U.S. and it worked.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Sending data over the Internet? (Networking question)

Post by badidea »

This way, the client opens and terminates a connection every 100 ms. If that is a problem, I don't know.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Sending data over the Internet? (Networking question)

Post by D.J.Peters »

@squall4226 here are a simple server and client.

you need the lates version of the *.zip file

I added server.ClientIP and server.ClientPort MrSwiss asked for this feature.

Read it line by line ask if something are unclear.

Joshy

file: server.bas

Code: Select all

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

' a user network port must be >9999
' 0-9999 are reserved  for OS, WEB, FTP ...
var aPort = 12345
' create a server
var aServer = NetworkServer(aPort)

print "server is runnng"
dim as NetworkConnection ptr aClientConnection
while asc(inkey())<>27 ' {ESC]
  ' no client connected ?
  if aClientConnection=0 then
    print "wait for a new client ..."
    aClientConnection = aServer.GetConnection()
    if aClientConnection then 
      print "client ip: " & aServer.ClientIP & " port: " & aServer.ClientPort & " connected."
    end if
  end if

  ' any client connected and new data waiting ?
  if aClientConnection<>0 andalso aClientConnection->CanGet()=1 then
    dim as zstring ptr pData
    print "get data from client"
    var nBytes = aClientConnection->GetData(pData,1)
    if nBytes<=0 then
      print "client disconnected !"
      delete aClientConnection : aClientConnection=0
    else
      print "client data: " & *pData
    end if
    sleep 100
  else
    sleep 1000
  end if
wend
print "end of server loop press any key ..."
' disconnect a client
if aClientConnection then delete aClientConnection
sleep
file: client.bas

Code: Select all

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

var aServerAddress = "127.0.0.1" ' same as "localhost"

' a user network port must be >9999 port's 0-9999 are reserved (OS, WEB, FTP, ...)
var aServerPort = 12345

' create a client
var aClient = NetworkClient(aServerAddress,aServerPort)
' get the connection for commnication
var aServerConnection = aClient.getConnection()

dim as integer number
while asc(inkey())<>27 ' {ESC]
  ' can we send data to server ?
  dim as integer timeout = 5000
  while aServerConnection->CanPut()=0 andalso timeout>0 : sleep 100 : timeout-=100 : wend
  if timeout<0 then
    print "server disconnected !"
    delete aServerConnection : aServerConnection=0
    exit while
  end if

  number+=1
  dim as string message = "message " & number
  print "send: " & message
  var nBytes = aServerConnection->PutData(strptr(message),len(message)+1)
  if nBytes<=0 then
    print "server disconnected !"
    exit while
  end if
  sleep 1000
wend
print "end of client loop press any key ..."
' disconnect if a connection
if aServerConnection then delete aServerConnection
Last edited by D.J.Peters on Mar 20, 2016 17:46, edited 2 times in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sending data over the Internet? (Networking question)

Post by MrSwiss »

@Joshy,

you've asked for it, here goes ;-)
  • Why can't the Server get the IP-Adress of the Client (on a new Connection)?
    Those are normally in the Network-Packet-Headers (Target-Address, Source-Address).
    Why can the Server only "receive" and not "send" too?
    The same goes for the Client "send", but no "receive"?
More low Level technical Questions:
  • I don't understand the Differences (Advantages/Disadvantages), of the Virtual-Functions, you are using in SNC.bi?
    From SNC-Thread: Destructor calls, if called internally: when, on which Condition(s)?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Sending data over the Internet? (Networking question)

Post by D.J.Peters »

@MrSwiss you don't allowed to ask :lol:

Of course you can send and receive data over a network connection in both direction.
Virtual getLastError makes no sense and are removed from latest zip file.
Advanced users can overwrite virtual getConnection if needed or simple ignore it.
Last but not least It's up to the user how to implement it's own protocol.

Hello server I'm a new client, here are my user name and IP I hope you are lucky with it. :-)

Remember it's only a simple include file that makes sockets and it's usage easy for beginners on Windows and Linux.

But feel free and write a complete network library with XYZ features.

Joshy
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sending data over the Internet? (Networking question)

Post by MrSwiss »

@Joshy,

thanks for your Answers, just the one about the Destructors (and their calling) is still open.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Sending data over the Internet? (Networking question)

Post by D.J.Peters »

Destructors are called by FB on end of the application
or by user if needed "delete conection : connection=0"

added only for you :-)
server.ClientIP
server.ClientPort

Joshy

Code: Select all

 if aClientConnection=0 then
    print "wait for a new client ..."
    aClientConnection = aServer.GetConnection()
    if aClientConnection then 
      print "client ip: " & aServer.ClientIP & " port: " & aServer.ClientPort & " connected."
    end if
  end if
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sending data over the Internet? (Networking question)

Post by MrSwiss »

@Joshi,

thanks a lot, but :-((
fbc wrote:Build error(s)
C:\FreeBASIC\FB1050\64bit\fbc -s console "SNC_SRV_CLI.bas"
C:\FreeBASIC\FB1050\SNC\snc.bi(119) warning 35(0): Mixing signed/unsigned operands
C:\FreeBASIC\FB1050\SNC\snc.bi(136) warning 35(0): Mixing signed/unsigned operands
C:\FreeBASIC\FB1050\SNC\snc.bi(236) warning 35(0): Mixing signed/unsigned operands
C:\FreeBASIC\FB1050\SNC\snc.bi(240) warning 35(0): Mixing signed/unsigned operands
SNC_SRV_CLI.bas(18) error 18: Element not defined, ClientIP in 'print "client ip: " & aServer.ClientIP & " port: " & aServer.ClientPort & " connected."'

Build error(s)
All those Warnings are also troubling me ...
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Sending data over the Internet? (Networking question)

Post by D.J.Peters »

Ma be you was faster and a new upload was in progress or you use a old version floating around on your HD.

I don't get any warnings here.

Joshy
snc wrote: D:\done\SimpleNetworkConnection>fbc -version
FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for win32 (32bit)
Copyright (C) 2004-2016 The FreeBASIC development team.
standalone

D:\done\SimpleNetworkConnection>fbc -w all server.bas

D:\done\SimpleNetworkConnection>fbc -w pendantic server.bas

D:\done\SimpleNetworkConnection>fbc -gen gcc -w pendantic server.bas

D:\done\SimpleNetworkConnection>fbc -gen gcc -w all server.bas
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sending data over the Internet? (Networking question)

Post by MrSwiss »

D.J.Peters wrote:Ma be you was faster and a new upload was in progress or you use a old version floating around on your HD.
I don't get any warnings here.
Just downloaded again, everything OK now :-))
Thanks a lot, again!
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

Re: Sending data over the Internet? (Networking question)

Post by squall4226 »

At least for me I get warnings with the 64bit compiler but not the 32bit. IDK why.

Meanwhile @D.J. Peters - thanks for those examples. They compile and run fine and I think I understand them just fine. I do note in your comment that you say the port has to be > 9999.

I knew some ports were reserved but I didn't know it was all the way to 10000. My original program was using 5469 which seemed to work just fine, even across the Internet(Of course I port forwarded it to this PC in my router).

So is it a situation where they are reserved and it's just "Bad practice" to use a port under 10000, or could it actually break something? I mean ofc if you are using a common port like 21 or 80 that could be bad, but yeah assuming it's not a commonly used port, but it's under 10k, what's the drawback?

That's really my only question now. I will be working on integrating this code into my main program that controls my device later.

Thank you very much!
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sending data over the Internet? (Networking question)

Post by MrSwiss »

squall4226 wrote:At least for me I get warnings with the 64bit compiler but not the 32bit. IDK why.
Just checked now, you're right.
Not OK on 64 bit FBC.

Think I've found the Problem (in winsock2.bi, Line: 239):
type SOCKET as UINT_PTR --> UINT defined as ULong (in windef.bi, Line: 109)

The assigning of -1 seems to be the Problem here.

As to your Question about Port(s) below 10k:
you'll simply never know, whether they are used or not (some of them are marked "reserved").
The safe way is, to only use only Port(s) above 9999.
Post Reply