Network Problems.

XverhelstX

Well-Known Member
Licensed User
Longtime User
heey everyone,

I have the network example on my phone running and it gives me the IP: 10.48.22.129

Then I use my own made program (programmed with VB.Net) on my computer with the following code:

B4X:
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
    Dim sock As New TcpClient()
    Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
    Dim port As Integer = 10130
    Private Sub connect()
        If TextBox_IP.Text = "localhost" Then
            TextBox_IP.Text = "127.0.0.1"
        End If
        ip = IPAddress.Parse(TextBox_IP.Text)
        port = TextBox_Port.Text
        Try
            sock.Connect(ip, port)
            lblState.Text = "Connected!"
            Btn_Connect.Text = "Disconnect!"

        Catch ex As Exception
            MsgBox("Unable to connect to server. Other PC might be offline.")
            lblState.Text = "Disconnected!"
            Btn_Connect.Text = "Connect!"
        End Try
    End Sub

    Private Sub dat(ByVal dat As String)
        Dim nstream As NetworkStream = sock.GetStream()
        Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(dat)
        nstream.Write(bit, 0, bit.Length)
    End Sub


    Private Sub Btn_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Connect.Click
        connect()
    End Sub

ip: 10.48.22.129
port: 10130
and the code of the network example:

B4X:
'Activity module
Sub Process_Globals
   Dim ServerSocket1 As ServerSocket
   Dim Socket1 As Socket
   Dim Timer1 As Timer
   Dim InputStream1 As InputStream
   Dim OutputStream1 As OutputStream
   Dim awake As phonewakestate
End Sub

Sub Globals
   Dim lblName As Label
   Dim lblSize As Label
   Dim lblIPState As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Timer1.Initialize("Timer1", 200)
      File.MakeDir(File.DirRootExternal, "android") 'probably already exists
   End If
   'Initialize the server socket if it is not initialized.
   'ServerSocket1 will not be initialized when FirstTime=True and after the user
   'closed the activity and then opened it again (by pressing on the back key)
   If ServerSocket1.IsInitialized = False Then
      ServerSocket1.Initialize(10130, "ServerSocket1")
   End If
   ToastMessageShow("My IP: " & ServerSocket1.GetMyIP, True)
   
   Activity.LoadLayout("1")
   lblIPState.text = ServerSocket1.GetMyIP
End Sub
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
   If Successful Then
      Socket1 = NewSocket
      Timer1.Enabled = True
      InputStream1 = Socket1.InputStream
      OutputStream1 = Socket1.OutputStream
      ToastMessageShow("Connected", True)
   Else
      Msgbox(LastException.Message, "Error connecting")
   End If
   ServerSocket1.Listen 'Continue listening to new incoming connections
End Sub

Sub Activity_Resume
   ServerSocket1.Listen
   awake.KeepAlive(True)
   awake.PartialLock()
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then 
      Timer1.Enabled = False
      Socket1.Close
      ServerSocket1.Close 'stop listening
   End If
   awake.ReleaseKeepAlive()
   awake.ReleasePartialLock()
End Sub

Sub Timer1_Tick
   If InputStream1.BytesAvailable > 0 Then
      Timer1.Enabled = False
      'The protocol for sending a file is:
      'Long - File size
      'Int - Number of bytes for file name
      'Bytes() - File name
      'Bytes() - File
      Dim buffer(8192) As Byte
      Dim raf As RandomAccessFile
      raf.Initialize3(buffer, True)
      'raf is used to convert bytes to other values.
      'Note that the client side is a .Net application and its bytes order is 
      'little endian (which is not the default For Android).
      
      InputStream1.ReadBytes(buffer, 0, 12) 'read the file size (long) and name length (int)
      Dim fileSize As Long
      fileSize = raf.ReadLong(0)
      Dim strLen As Int
      strLen = raf.ReadInt(8)
      InputStream1.ReadBytes(buffer, 0, strLen) 'read the file name
      Dim fileName As String
      fileName = BytesToString(buffer, 0, strLen, "UTF-8") 'convert the bytes to string
      lblName.Text = "File Name: " & fileName
      Dim out As OutputStream
      out = File.OpenOutput(File.DirRootExternal, "/android/" & fileName, False)
      Dim count As Int
      Do While fileSize > 0
         count = InputStream1.ReadBytes(buffer, 0, Min(buffer.Length, fileSize))
         out.WriteBytes(buffer, 0, count)
         fileSize = fileSize - count
         lblSize.Text = "Bytes left: " & NumberFormat(fileSize, 0, 0)
         DoEvents 'If we hadn't previously disabled Timer1 then it would have raised this event again and caused havoc.
      Loop
      out.Close
      ToastMessageShow("File saved successfully: " & File.Combine(File.DirRootExternal, "/android/" & fileName), True)
      Timer1.Enabled = True
   End If
End Sub

I keep getting an error that I cannot seem to connect with my device. (Unable to connect to server. Other PC might be offline.)
I just want to establish a connection, not to send a file yet.

I cannot seem to find my error. Any help?

I also did port forward on port 10130

XverhelstX
 
Last edited:

XverhelstX

Well-Known Member
Licensed User
Longtime User
well the port forward was just for my router.
i got this error when i open the example:

An error occured on sub_main_app_start
Cannot establish a connecton because the target computer has declinded the connection actively. 127.0.0.1:5007
continue?
yes/no

raw translation from dutch > english

xverhelstx
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
oh yes ok. I wasn't on a wifi connection., Now i'm connected with it.
but what if you are not on a wifi connection.
my wifi ip adres is now 192.168.1.104

XverhelstX

EDIT: And it works connecting with my pc with wifi.
ther's now connected ;), but what now I need to know if you are somewhere else on another wifi and not on the local area.
connected with my vb.net project, doesnt work with b4ppc

XverhelstX
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two types of port forwarding. I referred to ADB port forwarding which is only required if you want to connect to a device connected with USB cable or with the emulator.

Try to search for more information about how to allow external incoming connections. I do not know the exact configuration required. It is not related to your code or Android.
 
Upvote 0

TomK

Member
Licensed User
Longtime User
verhelst, it's not too terribly weird to me to be honest, as I have known some carriers do tend to port block on EDGE connections, where they allow it on 3G/4G.

Sometimes you have to contact your carrier to see if they are, in fact, blocking those ports... I know AT&T has done it, SBF, Verizon, and it's possible this is what you're experiencing yourself.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
As long as you are working/connecting within your LAN (local network) there shouldn't be no need for portforwarding. However, a good idea, at least while testing, is to turn off your firewall on your server.

If you want to access your server from the "outside", then you need to set up the right port(s) for portforwarding in your router. There are many guides for that which you can find by searching the internet. Of course, you also need to know the ip-address which your Service Provider has assigned to your home-network. Unless you have a static IP-address, this IP-address tends to change (every time you switch off your ADSL-router or if it gets disconnected). To keep track of your IP-address easily, I suggest using a Dynamic Address service (normally they are free).

You should also be aware of the fact that certain mobile-operators block certain ports.
 
Last edited:
Upvote 0
Top