[英]basic4android Server Socket
I'm trying to do a two ways communication between a PC running .NET Client-Server and an Android device, (the code is made with Basic4Android). 我正在尝试在运行.NET Client-Server的PC和Android设备之间进行两种通信方式(代码是使用Basic4Android编写的)。 Sending from Android to PC works fine, the problem occours when i try to send from the PC to Android.
从Android发送到PC的效果很好,当我尝试从PC发送到Android时,出现了问题。 I'm trying to use the ServerSocket but when the PC tries to connect to the Android the device, time-out is reached and an exception is raised.
我正在尝试使用ServerSocket,但是当PC尝试连接到Android设备时,达到了超时并且引发了异常。 The code i'm using is the following:
我正在使用的代码如下:
PC .NET 电脑
Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
sock.Connect(remoteip, 8565)
Dim buffer() As Byte = UTF8.GetBytes(string)
sock.Send(buffer)
sock.Close()
and the Android code: 和Android代码:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim ss As ServerSocket
Dim IS1 As InputStream
Dim timerListener As Timer
End Sub
Sub Activity_Create(FirstTime As Boolean)
timerListener.Initialize(timerListener, 1)
ss.Initialize(8565, "ss")
ss.Listen
End Sub
Sub ss_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
IS1 = NewSocket.InputStream
timerListener.Enabled = True
Else
ToastMessageShow("Error.", True)
End If
End Sub
Sub timerListener_Tick
Dim cv As ByteConverter
If IS1.BytesAvailable > 0 Then
Dim buffer() As Byte
IS1.ReadBytes(buffer, 0, IS1.BytesAvailable)
Dim result As String = cv.StringFromBytes(buffer, "UTF8")
ToastMessageShow(result, True)
timerListener.Enabled = False
End If
End Sub
What can be the issue? 可能是什么问题? Thank you in advance!!
先感谢您!!
You should use AsyncStreams to read the data. 您应该使用AsyncStreams读取数据。
In your PC read a byte from the stream after you write the message. 写入消息后,在PC中从流中读取一个字节。 This will cause the thread to wait for the byte to be available instead of closing the socket before the device read the message.
这将导致线程等待字节可用,而不是在设备读取消息之前关闭套接字。
In Sub timerListener_Tick(), control the timer action: 在Sub timerListener_Tick()中,控制计时器操作:
timerListener.Enabled = False
...
timerListener.Enabled = True
Timer will receive the messages periodly. 计时器将定期接收消息。
Closing the PC socket straight after sending will cause you a send error because it's closing when you are in fact still sending, better to use a global variable to hold the socket instance, but if you want to send synchronous (wait for the transfer to commit) use the example from MSDN. 发送后立即关闭PC套接字会导致发送错误,因为实际上您仍在发送时它将关闭,最好使用全局变量来保存套接字实例,但是如果您要同步发送(等待传输提交) )使用MSDN中的示例。
Synchronous Client Socket Example 同步客户端套接字示例
I wouldn't recommend it though, personally, I would prefer to use async transfers, like one of the above post recommends. 我个人不建议这样做,但我更喜欢使用异步传输,就像上面的帖子中推荐的那样。 See-> Asynchronous Client Socket Example
请参见-> 异步客户端套接字示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.