[S]imple [N]etwork [C]onnection - Advice needed

New to FreeBASIC? Post your questions here.
Post Reply
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

[S]imple [N]etwork [C]onnection - Advice needed

Post by Gablea »

Hi all,

I was wondering if someone can help me

i am trying to send some data to a Windows application I created (via TCP/IP) but when ever I run the Freebasic App it crashes

Below is my code

Code: Select all

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

Dim request 		As String = "Sale|1599"
Dim status 			As String
Dim pbuffer 		As ZString Ptr
Dim BufferSize		As Integer

'Client
var Client = NetworkClient("127.0.0.1",15975)

'Server:
var Server = NetworkServer(15976)

'Connections:
var ServerConnection = Client.GetConnection()
var ClientConnection = Server.GetConnection()

'send any data:
if ClientConnection->CanPut() then
'status = ClientConnection->PutData(Trim(request),len(request))
ClientConnection->PutData(strptr(request),len(request))
end if

'receive any data:
if ServerConnection->CanGet() then
	ServerConnection->GetData(pbuffer)
end if

'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)
What I am trying to do is send a command the the Server app (windows program) the command "Sale|1599" should be received by the App on port 15975 and then the Module would then send back on 15976 loads of status messages once it has sent back the final command the module then closes the 15976 port and then waits again for another request

What I would the the Freebasic app to do is send the "Sale|1599" to the add on module and then print to the debug window (for now) everything the add on program is sending back on 15976.

I will at some point need to have this working over a LAN Network (so I can not keep using 127.0.0.1)

if some one can tell me where I have gone wrong or show me what I miss understood I would be most greatful
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by D.J.Peters »

Your call NetworkClient("127.0.0.1",15975) try to connect to a running server on same local machine via port 15975

Where is the code of your running server ?

Before a client can connet to anything you have to create and run the server at first.

Looks like you are misunderstood something about concept of server and clients.

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

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by D.J.Peters »

Here are a minimal server.bas and client.bas http://www.freebasic.net/forum/viewtopi ... 8t#p217498

Joshy
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by Gablea »

My server is the Visual Basic application that is used by my windows apps to send and receive data.

I assume TCP/IP communication would be the same in freebasic
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by Gablea »

Right I have done some more work and i can now send Data to my Card Module BUT i can not get anything back :(

I am getting the following message from the processing module
No connection could be made because the target machine actively refused it.
127.0.0.1:15976
this is the changes I have made to the FreeBASIC interface

Code: Select all

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

	Dim request 		As String = "Sale|1599"
	Dim status 			As String
	Dim pbuffer 		As ZString Ptr
	Dim BufferSize		As Integer

'Client
	  Var ToModule = NetworkClient("127.0.0.1",15975)
	Var FromModule = NetworkClient("127.0.0.1",15976)

'Connections:
	  Var ToModuleConnection = ToModule.GetConnection()
	Var FromModuleConnection = FromModule.GetConnection()

'send any data:
	Print "Sending Request to Card module for " & Chr(156) & "15.99"
	
	if ToModuleConnection->CanPut() then
		ToModuleConnection->PutData(strptr(request),len(request))
	end if

	Sleep 1000

'receive any data:
	Print "Trying to get data from 'FromModuleConnection'"
Do
	if FromModuleConnection->CanGet() then
		FromModuleConnection->GetData(pbuffer)
		Print pbuffer
	end if
Loop

'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)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by MrSwiss »

Gablea wrote:No connection could be made because the target machine actively refused it. 127.0.0.1:15976
This means:
you'll have to look at the Code of the Card-Reader and/or its Documentation.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by Gablea »

MrSwiss wrote:This means: you'll have to look at the Code of the Card-Reader and/or its Documentation.
The add on module works perfectly with my Windows applications and it also uses TCP/IP Communications

Am I using the right way of using different port numbers

As the Module uses 15975 to receive data from other applications and uses 15976 to send data to other applications
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by MrSwiss »

Gablea wrote:As the Module uses 15975 to receive data from other applications and uses 15976 to send data to other applications
OK, but is the Card-Reader working as Server or Client ... more/better Details needed.
Plse. be more specific on the Applications "working"; e.g. Programming Language, as Server / as Client,
your own / somebody else(s) Code ... (is it FreeBASIC Code)?
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by Gablea »

It is a application I need to connect to is one I made in Visual basic.NET 2008 to allow communication between my Windows EPoS software
and my supplier Card Network

the program works I think as both server and client (as it received on one port and sends on another) but I am not 100% sure on this

The FreeBASIC app is sending data to the Module with no problem (because the card machine comes to life and is asking for the Card to be
entered) when the Module is trying to send data back to the FreeBASIC app this is where I am getting the communication error.

I have to use 2 different ports as this is a requirement from my card processing people and the Reason why I need to use the IP address is because
the module does not run on the Local Machine but on another Computer (Windows 10 my software runs on Linux) My suppliers MY be doing a linux
version but this will not happen this side of 2017 (but they say as long as I am running the card processing software on windows then I can process
cards)

so the idea is

Linux app connects to 192.168.1.101 port 15975 and sends data (working)
Linux app acepts data from 192.168.1.101 port 15976 - not working I just need it to print what ever it is receing to the screen for now
Gunslinger
Posts: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by Gunslinger »

Hello, i like to say there are still some problems with the Simple Netwerk Connection and i am still working on this.
Problems i do know are, closing the connection is done wrong it's missing the Shutdown(socket) making the connection timeout go wrong.
other problem is the delay in site the getdata works for tcp streams from http. but is not working very good in relaying data or making games.

As far as i know TCP/IP stream is a data stream and you never know of all bytes are revived or delayed on the network
The delay in site the getdata works on lan, only not over internet for 100%
When you send large pack of data it get splited in parts of ~1500 bytes and received over time.

Gablea is running on localhost can work fine i think.

I do like to thank D.J.Peters for making this so far.
I'am just a beginner in freebasic programming and not done testing this SNC wrapper, when i do i'll inform the maker.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by MrSwiss »

Gablea wrote:Linux app connects to 192.168.1.101 port 15975 and sends data (working)
Linux app acepts data from 192.168.1.101 port 15976
Since the Card-Reader seems to work as Server:
in the Client you'll have to "open" 2 Connections (Port 15975 to SEND, Port 15976 to RECEIVE).
In the Clients Code you have to implement a "Listener" that regularly checks for anything new received!

NOTICE: if you want to print "pBuffer" Data (a Pointer), you'll have to "de-reference" the Ptr:

Code: Select all

Print *pBuffer
Otherwise you are printing the Address the Ptr points to (any Number).
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by Gablea »

Thanks for the advice But I am not sure how I can create or implement a Listener is there any examples?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: [S]imple [N]etwork [C]onnection - Advice needed

Post by MrSwiss »

See here: Original Thread
Post Reply