gpt4 book ai didi

java - android tcp/ip 套接字不会将数据发送到计算机

转载 作者:可可西里 更新时间:2023-11-01 02:33:32 37 4
gpt4 key购买 nike

我试图制作一个应用程序,将数据从 android 发送到计算机(java 到 vb.net)。数据发送在 eclipse java 项目中有效,但在 android 项目中无效。但我有一个问题。我的计算机中没有传入数据到 Visual Basic。VB代码:

 Imports System.Net.Sockets
Imports System.Threading
Imports System.IO

Public Class Form1

Dim Listener As New TcpListener(65535)
Dim Client As New TcpClient
Dim Message As String = ""

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
ListThread.Start()
End Sub

Private Sub Listening()
Listener.Start()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Client = New TcpClient("192.168.100.3", 65535)

Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(TextBox2.Text)
Writer.Flush()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()

Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While

MsgBox(Message, MsgBoxStyle.OkOnly)
End If
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Listener.Stop()
End Sub

End Class

java代码:

package com.example.androidclient2;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView serverMessage;
Thread m_objThreadClient;
Socket clientSocket;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serverMessage=(TextView)findViewById(R.id.textView1);
}
public void Start(View view)
{
m_objThreadClient=new Thread(new Runnable() {
public void run()
{
try
{
clientSocket= new Socket("192.168.100.3",65535);
ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
oos.writeObject("Hellow there");
Message serverMessage= Message.obtain();
ObjectInputStream ois =new ObjectInputStream(clientSocket.getInputStream());
String strMessage = (String)ois.readObject();
serverMessage.obj=strMessage;
mHandler.sendMessage(serverMessage);
oos.close();
ois.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});

m_objThreadClient.start();

}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
messageDisplay(msg.obj.toString());
}
};
public void messageDisplay(String servermessage)
{
serverMessage.setText(""+servermessage);
}

}

感谢您的帮助。

最佳答案

今天刚刚在 android 上创建并测试了这个类:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class TcpClient {

private static final String TAG = TcpClient.class.getSimpleName();

private Socket socket;
private PrintWriter out;
private boolean connected;

public TcpClient()
{
socket = null;
out = null;
connected = false;
}


public void connect(Context context, String host, int port)
{
new ConnectTask(context).execute(host, String.valueOf(port));
}

private class ConnectTask extends AsyncTask<String, Void, Void> {

private Context context;

public ConnectTask(Context context) {
this.context = context;
}

@Override
protected void onPreExecute() {
showToast(context, "Connecting..");
super.onPreExecute();
}

@Override
protected void onPostExecute(Void result) {
if (connected) {
showToast(context, "Connection successfull");
}
super.onPostExecute(result);
}

private String host;
private int port;

@Override
protected Void doInBackground(String... params) {
try {
String host = params[0];
int port = Integer.parseInt(params[1]);
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream(), true);
} catch (UnknownHostException e) {
showToast(context, "Don't know about host: " + host + ":" + port);
Log.e(TAG, e.getMessage());
} catch (IOException e) {
showToast(context, "Couldn't get I/O for the connection to: " + host + ":" + port);
Log.e(TAG, e.getMessage());
}
connected = true;
return null;
}


}

public void disconnect(Context context)
{
if ( connected )
{
try {
out.close();
socket.close();
connected = false;
} catch (IOException e) {
showToast(context, "Couldn't get I/O for the connection");
Log.e(TAG, e.getMessage());
}
}
}


/**
* Send command to a Pure Data audio engine.
*/
public void send(String command)
{
if ( connected ) out.println(command +";");
}

private void showToast(final Context context, final String message) {
new Handler(context.getMainLooper()).post(new Runnable() {

@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
});
}
}

即使我在您的代码中没有看到明显的错误,它也可能对您有所帮助。

该设备是否与您的计算机位于同一个 WiFi 网络中?例如,它无法通过 USB 工作。

关于java - android tcp/ip 套接字不会将数据发送到计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131316/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com