AW: Reading TCP packets in Omnis

Stefan Csomor csomor at advancedconcepts.ch
Wed Feb 28 20:38:32 UTC 2024


Hi Jim 

My production code is spread too much but I’ve tried to hammer together a simpler example 

A oTCPServer object class 

Instance variable 
iServerSocket Integer 32 bit Initial Value -1 

Method $listen , Parameter pPort Integer 32 bit 

Local Variables 
lConnectionSocket Integer 32 bit Initial Value -1 
lStatus Integer 32 bit 

TCPSocket Returns iServerSocket 
TCPBind (iServerSocket,pPort) Returns lStatus 
If lStatus<>0 
TCPClose (iServerSocket) 
Calculate iServerSocket as -1 
If lStatus=-10048 
OK message {Server-Port [pPort] is already in use} 
Else 
OK message {Could not open Server (received status [lStatus]} 
End If 
Else 
TCPListen (iServerSocket) Returns lStatus 
If lStatus=0 
Working message (Cancel button) { Waiting for connection ... } 
Repeat 
TCPAccept (iServerSocket) Returns lConnectionSocket 
If canceled 
Break to end of loop 
End If 
Until lConnectionSocket>=0 
Close working message 
If lConnectionSocket<0 
Else 
# if you want not blocking reads 
TCPBlock (lConnectionSocket,1) 
Do method $read (lConnectionSocket) 
End If 
TCPClose (iServerSocket) 
Else 
TCPClose (iServerSocket) 
Calculate iServerSocket as -1 
OK message {Could not open Server (received status [lStatus]} 
End If 
End If 

Method $read, Parameter pConnectionSocket Integer 32 bit 

Local Variables 
lBuffer binary 
lStatus Integer 32 bit 

Repeat 
TCPReceive (pConnectionSocket,lBuffer) Returns lStatus 
If lStatus<=0 
If lStatus=-10035 
# no data available 
Else If lStatus=0|lStatus=10054 
TCPClose (pConnectionSocket) 
Calculate pConnectionSocket as -1 
Else 
# add breakpoint to catch other error situations 
End If 
Else 
Send to trace log [utf8tochar(lBuffer)] 
End If 
Until pConnectionSocket<0 

If you now have an object variable of this type, you could do a 

Do server.$listen(80) 

And connect against localhost:80 with your browser 

(the connection stays open until you close the browser) 

I hope this gives you something so start playing with … 

Best, 

Stefan 



Von: omnisdev-en <omnisdev-en-bounces at lists.omnis-dev.com> im Auftrag von jim jpcr.com <jim at jpcr.com>
Datum: Mittwoch, 28. Februar 2024 um 19:54
An: Omnis List Mail <omnisdev-en at lists.omnis-dev.com>
Betreff: Re: Reading TCP packets in Omnis 

Hi Jef,

This doesn’t appear to be quite what I am trying to do. I am not using a Modbus device. I have a serial-wifi interface that sends out a TCP packet to an IP:port address, so I am looking for Omnis code that is running on the IP and listening on the port. When a packet comes in I need to save it and then go right back to listening for the next packet.

Can you (or anyone else) send a a code sample that does this?

Thanks in advance,

Jim

--
Jim Pistrang
JP Computer Resources
413-237-5710
<https://jpcr.com <https://jpcr.com>>

On Feb 28, 2024, at 10:12 AM, Jef Leys | Desim <jef.leys at desim.be> wrote:

Hi,

Some sample code that works.


TCPConnect (FGebouw.GCadisIP,502) Returns socketUit
If flag true

Calculate binBuffer as binfromhex(FullString)
TCPSend (socketUit,binBuffer) Returns return
If flag true

TCPReceive (socketUit,buffer) Returns AntwoordLengte
If flag true

Calculate Antwoord as bintohex(buffer)
End If
End If
End If
TCPClose (socketUit)


Jef Leys

Op 28 feb. 2024, om 16:09 heeft jim jpcr.com <jim at jpcr.com> het volgende geschreven:

Hi $List,

I’m struggling to read incoming TCP packets in Omnis. I’ve attempted to write code using the various TCP commands, but with no success (and much head banging).

Does anyone have some sample code they can send me?

My code needs to listen for a packet on a specific port, and when it comes in it will call a method to write packet data to a database table and then go back to listening.

Thanks,

Jim



--
Jim Pistrang
JP Computer Resources
413-237-5710
<https://jpcr.com <https://jpcr.com>>

_____________________________________________________________
Manage your list subscriptions at https://lists.omnis-dev.com <https://lists.omnis-dev.com>
Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com <mailto:omnisdev-en at lists.omnis-dev.com>

_____________________________________________________________
Manage your list subscriptions at https://lists.omnis-dev.com <https://lists.omnis-dev.com>
Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com <mailto:omnisdev-en at lists.omnis-dev.com>

_____________________________________________________________
Manage your list subscriptions at https://lists.omnis-dev.com <https://lists.omnis-dev.com>
Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com <mailto:omnisdev-en at lists.omnis-dev.com> 






More information about the omnisdev-en mailing list