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

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

(idea comes from: Allegro Simplificator C++ wrapper)

Simple Network Connection

download: SimpleNetworkConnection.zip Nov 17, 2022

NOTE: Your FreeBASIC include files must be uptodate.

Client:
var Client = NetworkClient(address,port)
(address can be "domain.xyz" or IP "xxx.xxx.xxx.xxx")

Server:
var Server = NetworkServer(port)

Connections:
var ServerConnection = Client.GetConnection()
var ClientConnection* = Server.GetConnection()
(*)if a new client connected otherwise NULL.

send any data:
if Conection->CanPut() then
status = Connection->PutData(pBuffer,BufferSize)
end if

receive any data:
if Conection->CanGet() then
status = Connection->GetData(pBuffer [,maxSize])
end if

!!! pBuffer from connection->GetData() are allocate by "snc.bi" you have to free it with deallocate(pBuffer) !!!

status= 0 Server/Client Disconnected
status=-1 PutData/GetData Error
status> 0 nBytes sended or received

If something goes wrong you can check the ErrorCode.

ErrorCode = Client.GetLastError()
ErrorCode = Server.GetLastError()
ErrorCode = Connection->GetLastError()
if ErrorCode then print GetNetworkErrorText(ErrorCode)

That's all no more and nor less.

Joshy
Last edited by D.J.Peters on Nov 17, 2022 4:17, edited 34 times in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

First Tests:
sncDownloadAudio.bas

sncDownloadHTMLSite.bas

sncDownloadImage.bas

sncDownloadTXTFile.bas

sncDownloadXMLDocument.bas

sncGetWANIPfromPHPScript.bas

sncGetGoogleMaps.bas

server.bas

client.bas

I added simple server and client.
Last edited by D.J.Peters on Sep 09, 2020 14:05, edited 5 times in total.
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

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

Post by super_castle »

Here is the error:
D:\FreeBASIC\pebi-bas\snc.bi(306) error 18: Element not defined, S_un in 'addr.sin_addr.S_un.s_addr = INADDR_ANY'

from

WSAStartup(WINSOCK_VERSION, @wd)

greeting
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

super_castle wrote:D:\FreeBASIC\pebi-bas\snc.bi(306) error 18: Element not defined, S_un in 'addr.sin_addr.S_un.s_addr = INADDR_ANY'
D.J.Peters wrote:You need the newest include files they are included in latest FreeBASIC Git build.
Currently there are one compatibility issue in constructor NetworkServer. (should be fixed soon)

Code: Select all

constructor NetworkServer(byval port as ushort,byval maxConnections as integer)
  base()
  ...
  #ifdef __FB_LINUX__
  addr.sin_addr.s_addr = INADDR_ANY
  #else
  addr.sin_addr.S_un.s_addr = INADDR_ANY
  #endif
  ...
end constructor
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

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

Post by super_castle »

hello,thanks.

greeting
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

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

Post by super_castle »

not function with new include files usw....

greeting
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

super_castle wrote:not function with new include files usw....
give me more infos please ?

With the latest GIT builds all works here.

Joshy
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

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

Post by super_castle »

three programs, three error :

D:\FreeBASIC\fbc -s gui "sncDownloadAudio.bas"
D:\FreeBASIC\free-prog\SimpleNetworkConnection\snc.bi(74) error 41: Variable not declared, WINSOCK_VERSION in 'WSAStartup(WINSOCK_VERSION, @wd)'
D:\FreeBASIC\free-prog\SimpleNetworkConnection\snc.bi(310) error 18: Element not defined, S_un in 'addr.sin_addr.S_un.s_addr = INADDR_ANY'

D:\FreeBASIC\fbc -s gui "sncDownloadImage.bas"
D:\FreeBASIC\free-prog\SimpleNetworkConnection\snc.bi(74) error 41: Variable not declared, WINSOCK_VERSION in 'WSAStartup(WINSOCK_VERSION, @wd)'
D:\FreeBASIC\free-prog\SimpleNetworkConnection\snc.bi(310) error 18: Element not defined, S_un in 'addr.sin_addr.S_un.s_addr = INADDR_ANY'

D:\FreeBASIC\fbc -s gui "sncGetWANIPfromPHPScript.bas"
D:\FreeBASIC\free-prog\SimpleNetworkConnection\snc.bi(74) error 41: Variable not declared, WINSOCK_VERSION in 'WSAStartup(WINSOCK_VERSION, @wd)'
D:\FreeBASIC\free-prog\SimpleNetworkConnection\snc.bi(310) error 18: Element not defined, S_un in 'addr.sin_addr.S_un.s_addr = INADDR_ANY'

greeting
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

#define WINSOCK_VERSION MAKEWORD(2, 2)

It's defined in FreeBASIC/inc/win/winsock2.bi

First downlad the compiler from GIT build.
http://users.freebasic-portal.de/stw/builds/

than get the lastest include files from here:
https://sourceforge.net/projects/fbc/fi ... Builds/the latest include files

Joshy
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

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

Post by super_castle »

Hi, I can not find the correct files.
Can you please show exactly once?

Thank You.
greeting
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

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

Post by super_castle »

Hello, this error from :
https://sourceforge.net/projects/fbc/fi ... %20Builds/


----------------------------------------------------------
:\FreeBasic1\fbc -s gui "sncDownloadImage.bas"
D:\FreeBasic1\SimpleNetworkConnection\snc.bi(144) error 72: Array access, index expected, found '[' in 'FD_SET_(sock, @write_fds)'
Aborting due to runtime error 12 ("segmentation violation" signal)
Build error(s)
-----------------------------------------------------------
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

@super_castle I tested Simple Network Connection with FreeBASIC 1.02 successfull.

Joshy
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

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

Post by super_castle »

Hello, with FreeBASIC 1.02 its ok.

Thanks.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

added: "snc_utility.bi"

Joshy
Post Reply