Sending data over the Internet? (Networking question)

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

Sending data over the Internet? (Networking question)

Postby squall4226 » Mar 18, 2016 2:09

(This may be a dupe, sorry if it is. I originally couldn't log into this account and made a new one. But it wouldn't show my post since it was a new account and said it required admin approval)

Hey guys. So basically this is the situation. I've got a program I wrote that controls the NUM/CAPS/SCROLL LOCK lights on the keyboard. This is because I built a device from an old keyboard that basically connects a transistor and a relay to those lights. With that I can control real world devices with a program just by controlling those lights. It works well.

But now I want to be able to do it across the Internet. I've never done any networking in FB so I am completely lost.

Basically I need to do two things. I need to add code to my current program to receive commands across the net, and I need to write a small client program that will send those commands. It should be very simple, it literally only needs to be able to send "ON" or "OFF" to one of three possible switches.

I have no idea where to begin. Any ideas/tutorials/relevant keywords/whatever would be much appreciated!
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

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

Postby MrSwiss » Mar 18, 2016 13:08

For Starters try:
SNC (simple network connection) by D.J.Peters.

Forum-Search is your Friend :-)

Please DELETE the Duplicate ...
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 18, 2016 22:11

I saw that post and looked through it, and it does seem to have promise but I can't figure out how to make it do what I want. I looked at the examples and they are more about retrieving stuff from websites and such. I'm not sure how to make it do program to program communication.

As for the duplicate, I will delete it if I can.
D.J.Peters
Posts: 7773
Joined: May 28, 2005 3:28

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

Postby D.J.Peters » Mar 18, 2016 23:10

pseudo code
set your receiver as server

Code: Select all

var Server = NetworkServer(12345)
var ClientConnection = Server.GetConnection()
and your sender as client

Code: Select all

var Client = NetworkClient("use the IP from server or domain name")
var ServerConnection = Client.GetConnection()
If server and client are connected both can use PutData() or GetData() to communicate.

Thats all what are your problem with it ?

Joshy
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 19, 2016 5:19

That looks like the clarification I needed, yes. I will test it here shortly and report back.

One question: Assuming I get the connection established, and let's say the client sends a PutData(), the server HAS to use GetData() to get it right? It couldn't just magically get it. So I guess there needs to be some sort of polling period or something? I can probably figure this out with experimentation now, but I thought I'd ask in case someone answers before I get to coding just in case.

But yeah, assuming the IP can be either LAN or WAN this should be the right path. Thank you.

edit: Also, it should be possible to test the client and server on the same machine using 127.0.0.1 shouldn't it?

Sidenote: Glad to see you still active here. You are like an FB legend.
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

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

Postby MrSwiss » Mar 19, 2016 8:48

squall4226 wrote:edit: Also, it should be possible to test the client and server on the same machine using 127.0.0.1 shouldn't it?

Yes, Localhost (127.0.0.1) is a valid way for Test's. Just make certain that both, Server/Client are using the same Port-Number.
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 19, 2016 9:13

So I made a post a minute ago but I deleted it because I am moron with pointers lol. But now I have a real problem. The client is working, but the server explodes no matter what I do. I first tried:

Code: Select all

#Include Once "snc.bi"
#Include Once "fbgfx.bi"
Using FB
ScreenRes 800,600,16
Dim Shared As ZString Ptr dataptr
var Server = NetworkServer(5469)
var ClientConnection = Server.GetConnection()
Do While Not MultiKey(SC_ESCAPE)
   ClientConnection->GetData(dataptr)
   Print *dataptr
   Print "_________________"
   Sleep 2000,1
Loop


But it instacrashes. If I comment out the ClientConnection->GetData(dataptr) line it doesn't crash, but obviously does nothing.

Then I tried adding

Code: Select all

while ClientConnection->CanGet()<>1
  sleep 100
  Print "Waiting..."
Wend


To see if it was spazzing because the connection wasn't ready. But that explodes it too. So I can't test any further until I know why ><
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

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

Postby MrSwiss » Mar 19, 2016 9:15

@squall4226,

it is mandatory to use a FBC Version of AFAIK 1.02.0 or greater, for SNC to work.
Get yourself the current 1.05.0 Compiler(s) 32/64 bits, first. (see first post in Thread for more Info)
Most of the Hassles reported, are in this Context.

Otherwise, some more Information regarding Compiler/OS etc. would help.
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 19, 2016 9:16

I am using Freebasic 1.05.0 at the moment(64 bit version, though I have the 32 bit installed as well. Maybe that?) on Windows 10.

Edit: Switched to 32 bit compiler, still explodes ><
MrSwiss
Posts: 3182
Joined: Jun 02, 2013 9:27
Location: Switzerland

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

Postby MrSwiss » Mar 19, 2016 9:26

I've to install FBC on WIN 10 x86 first to Test (I've no WIN 10 x64 available), my "production" System is WIN 8.1 x64 Pro.
BTW: I don't trust WIN 10 jet ... (MS is simply getting to nosy, these days)
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 19, 2016 9:27

I have a 7 partition, I will run a test there really quick.

And meanwhile, I have pretty much all the snooping from 10 blocked. I used a program called O&O Shutup10. Overall I like the OS, but yeah not the spying or the forced updates etc.
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 19, 2016 9:32

I tried on my Win 7 x64 partition and had the same result. Windows 7 was kind enough to give me more information than 10 however:

Problem signature:
Problem Event Name: APPCRASH
Application Name: server3.exe
Application Version: 0.0.0.0
Application Timestamp: 56ed198d
Fault Module Name: server3.exe
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 56ed198d
Exception Code: c0000005
Exception Offset: 00001b21
OS Version: 6.1.7601.2.1.0.256.1
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
badidea
Posts: 1416
Joined: May 24, 2007 22:10
Location: The Netherlands

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

Postby badidea » Mar 19, 2016 12:18

This should work as server:

Code: Select all

#Include Once "inc/snc.bi"
#Include Once "fbgfx.bi"
Using FB
ScreenRes 800,600,16

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
         sleep 100
      wend
      var status = ClientConnection->GetData(dataptr)
      if dataptr then
         Print status, *dataptr
      end if
   end if
Loop

squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 19, 2016 13:52

Thank you for your response. The server compiles and run now. However, it receives no data, or only extremely occasionally it receives a garbled data.

This is the 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
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(@tosend,5)
Print "Sent " & tosend
Print result
Print "______________"
loop


The client SEEMS to work. Result = 5 when the server is running, and -1 when it is not. But the server never prints that it has received any data.
squall4226
Posts: 284
Joined: Dec 21, 2008 15:08
Contact:

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

Postby squall4226 » Mar 19, 2016 13:59

It seems to be getting stuck, and it's still the server. I used a beep to debu the server and found:

Code: Select all

       status = ClientConnection->GetData(dataptr)



Once the code gets here it stalls. A beep before will fire once I open client, but a beep after will not. It will only get unstuck here once I close the client, and then it prints status as -1 and some garbled text. So either the client is sending data wrong or... magic. IDK. I hate networking ><

Return to “General”

Who is online

Users browsing this forum: No registered users and 18 guests