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:
ip: 10.48.22.129
port: 10130
and the code of the network example:
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
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: