[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
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

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

Post by Dinosaur »

Hi All

Just bought a new printer Epson XP-4100.
Unbelievable how difficult it is to get the printer you want in Australia due to so many people working from home.

My requirement was for ESC/P support.
Yes, many of the latest Inkjets support ESC/P.
Using it on a Beaglebone Black and didn't want to install CUPS , plus I need to send one line to the printer
and may not send another line for hours untill the page is full.

Previously (Parallel port ) it was just a case of sending the ESC codes and all was sweet.
I thought that using sncNetworkPrinter I could do the same.
However, it simply prints garbage.

Epson advises that unless you load a different driver, the printer defaults to ESC/P.
So I sent chr(27) + chr(64) which is ESC @ to initialize the printer.
Then a Sleep to see what happens, nothing, so press Enter to send next line.
A test line "Hello World"It then loads a new page and prints a few characters of garbage.

Am I being over optimistic to expect this to work this way ?
Or more likely I am completely missing something blatant.
Does anyone have any experience doing this either with joshy's snc or other way.

Regards

EDIT:
echo "Hello World" | nc 192.168.8.107 9100 works ??
But this doesn't

Code: Select all

' [S]imple [N]etwork [C]onnection
#include once "snc.bi"
' test of a printer client connection
const as string ServerIP   = "192.168.8.107"
const as ushort ServerPort = 9100
dim as Integer ErrCode
' connect to printer server
var client = NetworkClient(ServerIP,ServerPort)
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

' get a connection from ConnectionFactory
var connection = client.GetConnection()
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

' ready to send ?
while connection->CanPut()<>1: sleep 100 : wend
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

' put data on the connection
Dim as String TxData
Dim as Integer TxLen
TxData = chr(&h1b) + chr(&h40)
TxLen  = Len(TxData)
connection->PutData(@TxData,TxLen)
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)

sleep 100

TxData = "Hello World"
TxLen  = Len(TxData)
connection->PutData(@TxData,TxLen)
ErrCode = Client.GetLastError()
If ErrCode <> 0 Then Print GetNetworkErrorText(ErrCode)
end
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

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

Post by badidea »

Can you try with zstring ptr? Or use strPtr/sadd. I think @TxData is not what you want. Also 0 string termination may be needed.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

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

Post by Dinosaur »

Hi All

Thanks for the reply badidea.
Or more likely I am completely missing something blatant.
You were right (AGAIN) all it needed was StrPtr instead of @.

One of these days I am going to get over my phobia about pointers.

Regards
NorbyDroid
Posts: 70
Joined: May 21, 2016 22:55

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

Post by NorbyDroid »

I was wondering if someone could check this over and see what I am doing wrong.

The Client is sending four chars in a string to the Server but when the server gets the dats and checks the info, it isn't always showing the proper data.

Here is the server code:

Code: Select all

#Include "snc.bi"
#Include "sendreceive.bi"

ScreenRes 640,176,32
Width 80,11

Dim Shared As NetworkConnection Ptr Connection

Const As UShort PORT=80
Var Server=NetworkServer(PORT)
Var ErrCode=Server.GetLastError()

If ErrCode<>0 Then
  Print "Error: Can't create server! " & GetNetworkErrorText(ErrCode)
  Beep:Sleep:End 1
End If

Print "Server created.  Waiting for Client...."

While Connection=NULL
  Connection=Server.GetConnection()
Wend

Dim As Integer yPos
Dim As String GetData,SetData

Print "Client connected " & Server.ClientIP

yPos=1
While Asc(InKey())<>27
  GetData=""
  ErrCode=ReceiveData(Connection,GetData,4)
  SetData=Left(GetData,4)

  If ErrCode=-1 Then
    Print "Error receiving data disconnect client!"
    Connection=NULL
    Beep:Sleep:End 1
  Else
    'Print "[" & GetData & "] " & Len(GetData),
    Print "[" & SetData & "] " & Len(SetData),
  End If

  Sleep 10
Wend

Beep:Print "Server shutting down...."
If Connection<>NULL Then Connection=NULL
Sleep 2000
Here is the Client Code:

Code: Select all

#Include "snc.bi"
#Include "sendreceive.bi"

ScreenRes 640,224,32
Width 80,14

Dim As Integer tFrame,tPos,xPos
Dim As String TrainArt(10,6),strData,tData

Declare Sub NetSend(strText As String)

Const As String IP="192.168.1.101"
Const As UShort PORT=80

Dim Shared As NetworkConnection Ptr Connection

Var Client=NetworkClient(IP,PORT)
Var ErrCode=Client.GetLAstError()

If ErrCode<>0 Then
  Print "Error: can't create client! " & GetNetworkErrorText(ErrCode)
  Beep:Sleep:End 1
End If

Print "Client connected with server " & IP
Connection=Client.GetConnection()
ErrCode=Client.GetLAstError()

If ErrCode<>0 Then
  Print "Error: Can't get a conection! " & GetNetworkErrorText(ErrCode)
  Beep:Sleep:End 1
End If

xPos=1
tFrame=1

While Asc(InKey())<>27
  For tLoop As Integer=1 To 10
    tData=Right(" "+str(tLoop),2)+Right(" "+str(tFrame),2)
    Print "{" & tData & "} " & Len(tData),
    NetSend tData

    Sleep 50
  Next

  tPos+=1
  If tPos Mod 6=0 Then
    tFrame+=1:If tFrame>6 Then tFrame=1
	  xPos+=1:If xPos>160 Then tPos=1:xPos=1
  EndIf
Wend

Beep:Print "Client shutting down...."

If Connection<>NULL Then Connection=NULL
Sleep 2000

Function NetReceive(strLength As Integer) As String
Dim As Integer tCode
Dim As String strText

tCode=ReceiveData(Connection,strText,strLength)

If tCode=-1 Then
  Print "Error receiving data disconnect client!"
  Connection=NULL
  Beep:Sleep:End 1
Else
  NetReceive=strText
End If
End Function

Sub NetSend(strText As String)
Dim As Integer tCode

tCode=SendData(Connection,strText)

If tCode=-1 Then
  Print "Error sending data disconnect client!"
  Connection=NULL
  Beep:Sleep:End 1
End If
End Sub
What I was trying to accomplish is for the Client to send a string of ascii art to the Server and the Server displays it on the screen. Thank you for your help.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

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

Post by badidea »

@NorbyDroid, If you look at the implementation of SendData() in sendreceive.bi, then you can see that 1 additional byte is transmitted:

ErrCode = connection->putData(strptr(strData),nChars+1)

This is a 0 character to mark the end of text.

In your code, add 1 byte in receive:

ErrCode=ReceiveData(ConnectionX,GetData,5)

I think a more robust solution can be made, but this should work.

BTW, if you are transmissing binary data, it is more efficient to use putData() directly.
praetor
Posts: 8
Joined: Nov 16, 2022 3:50

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

Post by praetor »

Sorry to necr-raise this post, but I saw the SMC was updated fairly recently, but when I try and use it, I get the following error.

I'm using the latest FreeBASIC on the latest Ubuntu.

/home/wfisher/Downloads/SimpleNetworkConnection/snc.bi(151) error 181: Invalid assignment/conversion, at parameter 4 of SETSOCKOPT() in 'if setsockopt(this.sock, IPPROTO_TCP, TCP_NODELAY, cptr(const any ptr,@tmp), len(tmp)) < 0 then'
/home/wfisher/Downloads/SimpleNetworkConnection/snc.bi(158) error 181: Invalid assignment/conversion, at parameter 4 of SETSOCKOPT() in 'if setsockopt(this.sock, SOL_SOCKET, SO_REUSEADDR, cptr(const any ptr,@tmp), len(tmp)) < 0 then'
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 »

from linux manual pages: https://linux.die.net/man/2/setsockopt
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
I can't see any problem with the fourth parameter ! cptr(const any ptr,@tmp)

Joshy

Code: Select all

constructor NetworkConnection(byval aSocket     as SNC_SOCKET, _
                              byval bSetNoDelay as boolean   , _
                              byval bReuseAddr  as boolean)
  this.sock = aSocket
  if this.sock = SNC_SOCK_ERROR then lasterror = ERROR_SOCKET : exit constructor
  dim as long tmp
  if bSetNoDelay = true then
    tmp = 1
    if setsockopt(this.sock, IPPROTO_TCP, TCP_NODELAY, cptr(const any ptr,@tmp), len(tmp)) < 0 then
      lasterror = ERROR_TCP_NODELAY
      exit constructor
    end if
  end if
  if bReuseAddr = true then
    tmp = 1
    if setsockopt(this.sock, SOL_SOCKET, SO_REUSEADDR, cptr(const any ptr,@tmp), len(tmp)) < 0 then
      lasterror = ERROR_SO_REUSEADDR
      exit constructor
    end if
  end if  
  lasterror = ERROR_NO_ERROR
end constructor
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 »

I changed the the hard coded server path's in the examples so they can work with simple 'http' (not 'https') on my server.
(get it from first post)

Joshy
praetor
Posts: 8
Joined: Nov 16, 2022 3:50

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

Post by praetor »

Any reason why this may not work then? Any help is greatly appreciated!

Thanks
D.J.Peters wrote: Nov 17, 2022 2:48 from linux manual pages: https://linux.die.net/man/2/setsockopt
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
I can't see any problem with the fourth parameter ! cptr(const any ptr,@tmp)

Joshy

Code: Select all

constructor NetworkConnection(byval aSocket     as SNC_SOCKET, _
                              byval bSetNoDelay as boolean   , _
                              byval bReuseAddr  as boolean)
  this.sock = aSocket
  if this.sock = SNC_SOCK_ERROR then lasterror = ERROR_SOCKET : exit constructor
  dim as long tmp
  if bSetNoDelay = true then
    tmp = 1
    if setsockopt(this.sock, IPPROTO_TCP, TCP_NODELAY, cptr(const any ptr,@tmp), len(tmp)) < 0 then
      lasterror = ERROR_TCP_NODELAY
      exit constructor
    end if
  end if
  if bReuseAddr = true then
    tmp = 1
    if setsockopt(this.sock, SOL_SOCKET, SO_REUSEADDR, cptr(const any ptr,@tmp), len(tmp)) < 0 then
      lasterror = ERROR_SO_REUSEADDR
      exit constructor
    end if
  end if  
  lasterror = ERROR_NO_ERROR
end constructor
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 »

I can compile all examples on Windows with success !

fbc32 --version
FreeBASIC Compiler - Version 1.10.0 (2022-06-20), built for win32 (32bit)
Copyright (C) 2004-2022 The FreeBASIC development team.

fbc64 --version
FreeBASIC Compiler - Version 1.10.0 (2022-06-20), built for win64 (64bit)
Copyright (C) 2004-2022 The FreeBASIC development team.

Joshy
praetor
Posts: 8
Joined: Nov 16, 2022 3:50

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

Post by praetor »

I'm not working with Windows. I'm working with linux.
D.J.Peters wrote: Nov 18, 2022 13:39 I can compile all examples on Windows with success !

fbc32 --version
FreeBASIC Compiler - Version 1.10.0 (2022-06-20), built for win32 (32bit)
Copyright (C) 2004-2022 The FreeBASIC development team.

fbc64 --version
FreeBASIC Compiler - Version 1.10.0 (2022-06-20), built for win64 (64bit)
Copyright (C) 2004-2022 The FreeBASIC development team.

Joshy
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 »

show me the declaration of "setsockopt" in your include file please
praetor
Posts: 8
Joined: Nov 16, 2022 3:50

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

Post by praetor »

looks like what's supposed to be in there:

extern int setsockopt (int __fd, int __level, int __optname, const void *__optval, socklen_t __optlen) __THROW;
D.J.Peters wrote: Nov 18, 2022 23:08 show me the declaration of "setsockopt" in your include file please
bfg
Posts: 9
Joined: Feb 02, 2008 0:45

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

Post by bfg »

Thank you so much for your work!
I'm trying your examples and everything is fine in my windows but when I try linux...

Code: Select all

[bfg@bfg-wks SimpleNetworkConnection]$ fbc client.bas 
/home/bfg/Download/SimpleNetworkConnection/snc.bi(151) error 181: Invalid assignment/conversion, at parameter 4 of SETSOCKOPT() in 'if setsockopt(this.sock, IPPROTO_TCP, TCP_NODELAY, cptr(const any ptr,@tmp), len(tmp)) < 0 then'
/home/bfg/Download/SimpleNetworkConnection/snc.bi(158) error 181: Invalid assignment/conversion, at parameter 4 of SETSOCKOPT() in 'if setsockopt(this.sock, SOL_SOCKET, SO_REUSEADDR, cptr(const any ptr,@tmp), len(tmp)) < 0 then'
This is an archlinux x86_64 and I've tested fbc 1.09.0 from sourceforge and the one from github fresh compiled.

What can I do?
praetor
Posts: 8
Joined: Nov 16, 2022 3:50

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

Post by praetor »

This is the same problem I'm having on Ubuntu with the latest FB.
bfg wrote: Nov 20, 2022 10:27 Thank you so much for your work!
I'm trying your examples and everything is fine in my windows but when I try linux...

Code: Select all

[bfg@bfg-wks SimpleNetworkConnection]$ fbc client.bas 
/home/bfg/Download/SimpleNetworkConnection/snc.bi(151) error 181: Invalid assignment/conversion, at parameter 4 of SETSOCKOPT() in 'if setsockopt(this.sock, IPPROTO_TCP, TCP_NODELAY, cptr(const any ptr,@tmp), len(tmp)) < 0 then'
/home/bfg/Download/SimpleNetworkConnection/snc.bi(158) error 181: Invalid assignment/conversion, at parameter 4 of SETSOCKOPT() in 'if setsockopt(this.sock, SOL_SOCKET, SO_REUSEADDR, cptr(const any ptr,@tmp), len(tmp)) < 0 then'
This is an archlinux x86_64 and I've tested fbc 1.09.0 from sourceforge and the one from github fresh compiled.

What can I do?
Post Reply