Anet networking module for android

Monkey Forums/User Modules/Anet networking module for android

Sub_Zero(Posted 2012) [#1]
http://code.google.com/p/anet-module/

Current Version: v1.40

EDIT: Now also includes UDP!! (v1.10)
EDIT2: Added multithreading support for server receive methods (v1.20)
EDIT3: Added ReadLine and WriteLine methods (v1.30)
EDIT4: Fixed bugs in BindSocket, UDP server is now working! (v1.40)

Included is the following functions (a la blitzmax)

This will be familiar to some of you ;)

Network functions, also introducing WriteBytes and Readbytes

BindSocket:Bool(port:Int)
CloseSocket:Void()
ConnectSocket:Bool(remoteIP:String, remotePort:Int)
CreateTCPSocket:Bool()
CreateUDPSocket:Bool()
FlushStream:Void()
HostIp:String(hostname:String)
HostIps:String[](hostname:String)
HostName:String(hostIP:String)
SocketAccept:Bool(timeout:Int = 0)
SocketConnected:Bool()
SocketLocalIP:String()
SocketLocalPort:Int()
SocketReadAvail:Int()
SocketRemoteIP:String()
SocketRemotePort:Int()
ReceiveUDP:Bool()
ReadByte:Int()
ReadBytes:Int[](offset:Int, length:Int)
ReadInt:Int()
ReadLine:String()
ReadString:String(length:Int)
WriteByte:Int(outbyte:Int)
WriteBytes:Int(outbytes:Int[], offset:Int, length:Int)
WriteLine:Int(line:String)
WriteInt:Int(integer:Int)
WriteString:Int(text:String)
_data:Int[]
_offset:Int
_length:Int
_ip:String
_port:Int




Documentation:

BindSocket:Bool(port:Int)
Returns true if socket binds to local port

Closesocket:Void()
Close socket

ConnectSocket:Bool(remoteIP:String, remotePort:Int)
Returns true if socket is connected successfully

CreateTCPSocket:Bool()
Returns true if tcp socket is created successfully

Method FlushStream:Void()
Writes remaining bytes (if any) from buffer to output stream

HostIp:String(hostname:String)
Returns ip address as string

HostIps:String[](hostname:String)
Returns ip addresses as string array

HostName:String(hostIP:String)
Returns hostname as string

SocketAccept:Bool(timeout:Int = 0)
Listens to socket, and returns true if a connection is accepted.

SocketConnected:Bool()
Returns true if socket is connected

SocketLocalIP:String()
Returns local ip address as string

SocketLocalPort:Int()
Returns local port as int

SocketReadAvail:Int()
Returns number of bytes which can be read from the stream without causing the socket to block, or 0 if none.

SocketRemoteIP:String()
Returns remote ip address as string

SocketRemotePort:Int()
Returns remote port as int

ReceiveUDP:Bool()
Receive a UDP packet from the bound socket.
The packet data is stored in the _data[] field
The length of the data is stored in the _length field
The ip address of the sender is stored in the _ip field
The port of the sender is stored in the _port field

ReadByte:Int()
Read a byte from stream

ReadBytes:Int[](offset:Int, length:Int)
Read a byte array from stream

ReadInt:Int()
Read a integer from stream

ReadLine:String()
Read a line of text from stream

ReadString:String(length:Int)
Read a string from stream

WriteByte:Int(outbyte:Int)
Write a byte to stream

WriteBytes:Int(outbytes:Int[], offset:Int, length:Int)
Write a byte array to stream

WriteInt:Int(integer:Int)
Write a integer to stream

WriteLine:Int(line:String)
Write a line of text to stream

WriteString:Int(text:String)
Write a string to stream


All functions that return ints return -2 on error

All write methods are valid for UDP, for reading; use ReceiveUDP() instead.

All tested and working ok :)


DruggedBunny(Posted 2012) [#2]
Cool, will have to try this at some point, thanks!


Sub_Zero(Posted 2012) [#3]
no problem ;)


Sub_Zero(Posted 2012) [#4]
I've added UDP support in the latest version


Sub_Zero(Posted 2012) [#5]
nwe version again


Rushino(Posted 2012) [#6]
Is this still being developped ?


Sub_Zero(Posted 2012) [#7]
Not at the moment.

Do you see the need of any enhancements?


Rushino(Posted 2012) [#8]
Not at all but i was looking to see what have been done here :) from what it look what you did seem rather easy to work with. Cause i will have to look soon into networking and i dunno if i should head with pubnub or sockets..


Sub_Zero(Posted 2012) [#9]
For what's been done, check out the changelog at http://code.google.com/p/anet-module/wiki/ChangeLog


ImmutableOctet(SKNG)(Posted 2013) [#10]
Hey Sub_Zero, have you ever gotten around to testing UDP servers in ANet?

All tested except for server functions (My emulator wouldn't give me a local address), any feedback here is appreciated. ;)

I ran the UDP client example, and it works perfectly with my own socket library(For Windows). However as you said, ANet seems to have trouble getting the IP address of the tablet I'm using. The TCP examples work without an issue(Though I've only tested the TCP server with Monkey's TcpStream class), but the UDP server doesn't seem to be able to find an IP address.

So do you know what the problem is, and if so, can you fix it? If you're not sure, I may end up editing it myself, but I'd rather not mess with the 'official' version of the module.


c.k.(Posted 2013) [#11]
Can this be expanded for iOS, HTML5, and other monkey-compatible platforms? Or is similar functionality already available for those? If already available, can these mods all be combined so we have one API for all?


ImmutableOctet(SKNG)(Posted 2013) [#12]
If you're referring to ANet, then not without rewriting it quite a bit(It's Java based). My library on the other hand is C++ based, so it could technically run on most of Monkey's platforms with the right libraries/versions of libraries, and a bit of work. However, if you want to throw a bunch of libraries/modules together, you could always implement separate modules for the different platforms, then make a few identical wrappers.

And though I haven't looked into it very much, I'm pretty sure the HTML5 target would be forced to use TCP sockets, and not be able to host servers. iOS seems more likely, but I'm not much of an Objective C guy, and I don't really have an 'i' device. I have no idea about Sub_Zero though.

So in conclusion, it is possible, but we'd need a good team for it, or someone who's really dedicated. But, if you want good enough functionality, and only TCP sockets, Ferdi ported "Push Button" to Monkey recently.

By the way, does Sub_Zero even come on these days?


Sub_Zero(Posted 2013) [#13]
Hey all

Thanks for the replies, I haven't been around here much lately, but now I'm finally here.

@Sonickidnextgen:
Thanks for testing. I will have a look at the ip stuff to see if i can get it sorted.

@c.k.:
Ofcourse it can be done on other platforms, unsure about html5 though.
As for iOS i have the same problem as Sonickidnextgen, i have no i-devices.


Sub_Zero(Posted 2013) [#14]
OK....

Anet v1.40 is now up!

I've fixed the UDP server now.

The problem was that android breaks at getting the correct ip where regular java programs don't. I had to apply a fix for the problem :)

Happy coding folks :)


ImmutableOctet(SKNG)(Posted 2013) [#15]
Thanks Sub_Zero, now I won't have to worry about editing it for my projects.

But after looking into it, when I use "SocketLocalIP()", it gives me the integer IP address converted into a string, but its byte order is reversed. Was this intentional, or is this a bug?

EDIT: I tested it a bit more, and it worked normally on an actual device, so I'm not sure if this is a bug or not.


Supertino(Posted 2013) [#16]
I've no use for this yet, but good to know someone has done the hard work :)


Sub_Zero(Posted 2013) [#17]
@Sonickidnextgen: Sounds like the emulator has a bug regarding SocketLocalIP()


ImmutableOctet(SKNG)(Posted 2013) [#18]
Yeah, I'm pretty sure that was the case.