Android Programming Press on the image to return to the main documentation page.

Network

The network library includes two objects for working with TCP: Socket, ServerSocket With Socket you can communicate with other devices and computers over TCP/IP.
ServerSocket allows you to listen for incoming connections. Once a connection is established you will receive a Socket object that will be used for handling this specific connection.
See the
Network tutorial.
It also includes two objects for working with UDP: UDPSocket and UDPPacket. See UDPSocket for more information.

List of types:

ServerSocket
Socket
UDPPacket
UDPSocket

ServerSocket

The ServerSocket object allows other machines to connect to this machine.
The ServerSocket listens to a specific port. Once a connection arrives, the NewConnection event is raised with a Socket object.
This Socket object should be used to communicate with this client.
You may call Listen again and receive more connections. A single ServerSocket can handle many connections.
For each connection there should be one Socket object.

Permissions:

android.permission.INTERNET
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_NETWORK_STATE

Events:

NewConnection (Successful As Boolean, NewSocket As Socket)

Members:


  Close

  GetMyIP As String

  GetMyWifiIP As String

  Initialize (Port As Int, EventName As String)

  IsInitialized As Boolean

  Listen

Members description:

Close
Closes the ServerSocket. This will not close any other sockets.
You should call Initialize if you want to use this object again.
GetMyIP As String
Returns the server's IP. Will return "127.0.0.1" (localhost) if no other IP is found.
This method will return the wifi network IP if it is available.
GetMyWifiIP As String
Returns the IP address of the wifi network.
Returns "127.0.0.1" (localhost) if not connected.
Initialize (Port As Int, EventName As String)
Initializes the ServerSocket.
Port - The port that the server will listen to. Note that you should call Listen to start listening. Port numbers lower than 1024 are restricted by the system.
EventName - The event Sub prefix name.
IsInitialized As Boolean
Tests whether the object is initialized.
Listen
Starts listening in the background for incoming connections.
When a connection is established, the NewConnection event is raised. If the connection is successful a Socket object will be passed in the event.
Calling Listen while the ServerSocket is listening will not do anything.

Socket

The Socket object is an endpoint for network communication.
If you are connecting to a server then you should initialize a Socket object and call Connect with the server address.
The Connected event will be raised when the connection is ready or if the connection has failed.
Sockets are also used by the server. Once a new incoming connection is established, the NewConnection event will be raised and an initialized Socket object will be passed as a parameter.
Once a socket is connected you should use its InputStream and OutputStream to communicate with the other machine.

Permissions:

android.permission.INTERNET

Events:

Connected (Successful As Boolean)

Members:


  Close

  Connect (Host As String, Port As Int, TimeOut As Int)

  Connected As Boolean [read only]

  Initialize (EventName As String)

  InitializeSSL (EventName As String, KeyStoreStream As java.io.InputStream, Password As String)

  InitializeSSLAcceptAll (EventName As String)

  InputStream As java.io.InputStream [read only]

  IsInitialized As Boolean

  OutputStream As java.io.OutputStream [read only]

  ResolveHost (Host As String) As String

  TimeOut As Int

Members description:

Close
Closes the socket and the streams.
It is safe to call this method multiple times.
Connect (Host As String, Port As Int, TimeOut As Int)
Tries to connect to the given address. The connection is done in the background.
The Connected event will be raised when the connection is ready or if it has failed.
Host - The host name or IP.
Port - Port number.
TimeOut - Connection timeout. Value is specified in milliseconds. Pass 0 to disable the timeout.
Connected As Boolean [read only]
Tests whether the socket is connected.
Initialize (EventName As String)
Initializes a new socket.
InitializeSSL (EventName As String, KeyStoreStream As java.io.InputStream, Password As String)
Initializes a new SSL socket.
EventName - Sets the sub that will handle the Connected event.
KeystoreStream - An InputStream that points to an alternate keystore. Pass Null to use the default keystore.
The keystore format should be BKS.
Password - Custom keystore password.
InitializeSSLAcceptAll (EventName As String)
Initializes a new SSL socket that accepts all certificates automatically. This method is less secure as the server certificate is not tested.
InputStream As java.io.InputStream [read only]
Returns the socket's InputStream which is used to read data.
IsInitialized As Boolean
Tests whether the object was initialized.
OutputStream As java.io.OutputStream [read only]
Returns the socket's OutputStream which is used to write data.
ResolveHost (Host As String) As String
Resolves the host name and returns the IP address.
This method is deprecated and will not work properly on Android 4+ devices.
TimeOut As Int
Gets or sets the timeout of the socket's InputStream. Value is specified in milliseconds.
By default there is no timeout.

UDPPacket

A packet of data that is being sent or received.
To send a packet call one of the Initialize methods and then send the packet by passing it to UDPSocket.Send.
When a packet arrives you can get the data in the packet from the available properties.

Events:

None

Members:


  Data() As Byte [read only]

  Host As String [read only]

  HostAddress As String [read only]

  Initialize (Data() As Byte, Host As String, Port As Int)

  Initialize2 (Data() As Byte, Offset As Int, Length As Int, Host As String, Port As Int)

  IsInitialized As Boolean

  Length As Int [read only]

  Offset As Int [read only]

  Port As Int [read only]

  toString As String

Members description:

Data() As Byte [read only]
Gets the data array received.
Host As String [read only]
This method is deprecated and will not work properly on Android 4+ device.
Use HostAddress instead.
HostAddress As String [read only]
Gets the IP address of the sending machine.
Initialize (Data() As Byte, Host As String, Port As Int)
Initializes the packet and makes it ready for sending.
Data - The data that will be send.
Host - The target host name or IP address.
Port - The target port.
Initialize2 (Data() As Byte, Offset As Int, Length As Int, Host As String, Port As Int)
Similar to Initialize. The data sent is based on the Offset and Length values.
IsInitialized As Boolean
Length As Int [read only]
Gets the length of available bytes in the data. This can be shorter than the array length.
Offset As Int [read only]
Gets the offset in the data array where the available data starts.
Port As Int [read only]
Gets the port of the sending machine.
toString As String

UDPSocket

UDPSocket supports sending and receiving UDPPackets. Sending packets is done by calling the Send method.
When a packet arrives the PacketArrived event is raised with the packet.
This example sends a string message to some other machine. When a packet arrives it converts it to string and shows it:

Sub process_globals
  Dim UDPSocket1 As UDPSocket
End Sub

Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
    UDPSocket1.Initialize("UDP", 0, 8000)
  End If
  Dim Packet As UDPPacket
  Dim data() As Byte
  data = "Hello from Android".GetBytes("UTF8")
  Packet.Initialize(data, "10.0.0.1", 5000)
  UDPSocket1.Send(Packet)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
  Dim msg As String
  msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
  Msgbox("Message received: " & msg, "")
End Sub

Permissions:

android.permission.INTERNET

Events:

PacketArrived (Packet As UDPPacket)

Members:


  Close

  Initialize (EventName As String, Port As Int, ReceiveBufferSize As Int)

  IsInitialized As Boolean

  Port As Int [read only]

  Send (Packet As UDPPacket)

  toString As String

Members description:

Close
Closes the socket.
Initialize (EventName As String, Port As Int, ReceiveBufferSize As Int)
Initializes the socket and starts listening for packets.
EventName - The name of the Sub that will handle the events.
Port - Local port to listen on. Passing 0 will cause the OS to choose an available port automatically.
ReceiveBufferSize - The size of the receiving packet. Packets larger than this value will be truncated.
Pass 0 if you do not want to receive any packets.
IsInitialized As Boolean
Tests whether this object is initialized.
Port As Int [read only]
Gets the local port that this socket listens to.
Send (Packet As UDPPacket)
Sends a Packet. The packet will be sent in the background (asynchronously).
toString As String

Top