我正在尝试让我的服务器在vb.net上制造,以将消息发送到在android上制造的客户端。我在vb.net上有一个客户端和服务器,我可以毫无问题地发送和接收消息(文本)。但是,当我尝试使客户端在Android上正常工作时,我无法接收到客户端(android)的消息,但是如果我能够将其发送到服务器(vb.net)则发送消息。这,我不知道如何继续
服务器VB.NET
Imports System.Net.Sockets
Imports System.Text
Public Class Servidor
Dim Tcp As TcpListener
Dim th As New Threading.Thread(AddressOf Rutina)
Dim ejecuto = False
Private Sub Servidor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
End Sub
Dim tcpservercliente As New TcpClient
Public Function Rutina()
Try
Do
If ejecuto = True Then
Exit Do
End If
If Tcp.Pending = True Then
tcpservercliente.Client = Tcp.AcceptSocket
End If
If tcpservercliente.Available > 0 Then
Dim databytes(1000) As Byte
Dim decode As New ASCIIEncoding
tcpservercliente.Client.Receive(databytes)
txtRecibido.Text += vbCrLf & "Cliente Android: " & decode.GetString(databytes)
End If
Loop
Catch ex As System.InvalidOperationException
MsgBox("Error: " & ex.Message)
End Try
End Function
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Try
Tcp = New TcpListener(System.Net.IPAddress.Parse("192.168.1.8"), 1371)
Tcp.Start()
th.Start()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Servidor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
th.Abort("Rutina")
Application.Exit()
End Sub
Private Sub btnEnviar_Click(sender As Object, e As EventArgs) Handles btnEnviar.Click
Try
Dim decode As New ASCIIEncoding
tcpservercliente.Client.Send(decode.GetBytes(txtMensajeEnviar.Text))
Catch ex As System.Net.Sockets.SocketException
MsgBox(ex.Message)
End Try
End Sub
客户端Android
//CLASS RM
public class RM extends AsyncTask<Void, Void, Void> {
Socket socket;
BufferedReader input;
@Override
protected Void doInBackground(Void... voids) {
try {
socket = new Socket("192.168.1.8",1371);
InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
input = new BufferedReader(streamReader);
if(input.ready()){
Log.i("AsyncTask", "Ready ");
}else{
Log.i("AsyncTask", "No Ready");
}
input.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
//VOID MAIN ACTIVITY
RM mr = new RM();
mr.execute();
我试图看到它返回的时间总是空的。从服务器发送的邮件未到达
对不起,我的英语不好
编辑: 这是我用来从客户端(android)向服务器(vb.net)发送消息的类
package com.example.app_test_client;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class MessageSender extends AsyncTask<String, Void, Void> {
Socket socket;
PrintWriter pw;
@Override
protected Void doInBackground(String... voids) {
String mensaje_enviar = voids[0];
try {
socket = new Socket("192.168.1.8",1371);
pw = new PrintWriter(socket.getOutputStream());
pw.write(mensaje_enviar);
pw.flush();
pw.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
代码主要活动(客户端ANDROID)
package com.example.app_test_client;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
EditText mensaje_enviar;
TextView mensaje_recibido;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mensaje_enviar = (EditText)findViewById(R.id.txtTexto);
mensaje_recibido = (TextView)findViewById(R.id.lblMensaje);
}
public void enviar(View v){
MessageSender MensajeRemitente = new MessageSender();
MensajeRemitente.execute(mensaje_enviar.getText().toString());
}
}
无效的“ enviar”我在“ onClick”按钮中拥有它。从客户端(android)向服务器(vb.net)发送消息的所有这些对我来说都是有效的。
基于此类“ MessageSender”,我做了另一个等同于在客户端(android)上接收消息的操作,但没有成功
答案 0 :(得分:1)
如果您试图在Android设备上接收消息,那么您将无法使用String messageReceived = input.readLine();
阅读消息本身。您的代码如下所示:
...
socket = new Socket("192.168.1.8",1371);
InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
input = new BufferedReader(streamReader);
String messageReceived = input.readLine();
...
您将在messageReceived
中收到服务器发送的消息。
无需过多讨论,在读取服务器发送的数据时,您需要知道发送的数据何时“结束”,而当前实现的最简单方法是打印“行结束”(发送邮件时,在VB中使用vbCrLf
字符)。因此,您需要在Sub btnEnviar_Click
中添加vbCrLf
,如下所示:
tcpservercliente.Client.Send(decode.GetBytes(txtMensajeEnviar.Text & vbCrLf))
String messageReceived = br.readLine();
结尾的“一行结尾”。如果服务器没有发送任何“行尾”(如您当前的实现中那样),它将一直等待,因此程序似乎挂起了。这就是您正在发生的情况,当您注意到阅读消息后无法执行其他任何操作时,它只是在等待永远不会发生的事情。input.ready()
。只有从服务器完全发送完数据后,情况才会如此,并且很有可能在调用数据时仍在发送数据。