VB.Net 服务器和 Android 客户端(套接字)发送和接收
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14107006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.Net Server & Android Client (Socket) Send & Receive
提问by Shifatul
I am new to Android programming I wrote a simple Server(VB.NET) / Client(Java/Android) program. Text from Android/Java is send successfully to VB.Net but Response from VB.Net is not received in Android/Java (buffer.readLine()returnsnull)
我是 Android 编程的新手,我编写了一个简单的服务器(VB.NET)/客户端(Java/Android)程序。来自 Android/Java 的文本已成功发送到 VB.Net,但在 Android/Java 中未收到来自 VB.Net 的响应(buffer.readLine()返回null)
Am I missing something?
我错过了什么吗?
Here are my Codes
这是我的代码
VB.NET (Server)
VB.NET(服务器)
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim server As New TcpListener(9999)
Dim client As New TcpClient
Dim stream As NetworkStream
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
Me.Text = "Waiting...."
Dim str As String
server.Start()
client = server.AcceptTcpClient
stream = client.GetStream()
Dim r_byt(client.ReceiveBufferSize) As Byte
stream.Read(r_byt, 0, client.ReceiveBufferSize)
Str = Encoding.ASCII.GetString(r_byt)
Label1.Text = str
End Sub
Private Sub Responce_Click(sender As Object, e As EventArgs) Handles Responce.Click
Dim s_byt() As Byte = Encoding.ASCII.GetBytes("Got it" & vbCr)
stream.Write(s_byt, 0, s_byt.Length)
stream.Flush()
stream.Close()
client.Close()
server.Stop()
End Sub
Android/Java(Client)
安卓/Java(客户端)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
Button buttonSend, buttonReceive;
private static Socket socket = null;
PrintStream stream = null;
BufferedReader buffer = null;
String string = "a";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
socket = new Socket("192.168.0.104", 9999);
stream = new PrintStream(socket.getOutputStream());
stream.println("Hi Server...");
buffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
string = buffer.readLine();
Log.d("ServerActivity", " - " + string);
buffer.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
回答by Luis
It looks like you have a couple of issues:
看起来你有几个问题:
In the server side, you are trying to read something from the socket into
r_byt, and you are not writing anything to it on cliente side. When you press the send button on server side,r_bytstill null and that's what you receive on cliente side.On client side the call to socket read is blocking and after a few seconds will result in a ANR error (Application not Responding) in the cliente. You should move the socket read to a different thread from the UI. The newer Android versions don't even let you read from a socket in the UI thread.
在服务器端,您试图将套接字中的某些内容读入
r_byt,而在客户端没有向其写入任何内容。当您按下服务器端的发送按钮时,r_byt仍然为空,这就是您在客户端收到的。在客户端,对套接字读取的调用被阻塞,几秒钟后将导致客户端出现 ANR 错误(应用程序未响应)。您应该将套接字读取从 UI 移动到不同的线程。较新的 Android 版本甚至不允许您从 UI 线程中的套接字读取。
Regards.
问候。

