发送字符串从机器人到Visual Basic 2010
我想从Android的字符串发到我的电脑(Visual Basic中2010)我尝试这一个,但它不工作。请人帮我。
I want to send a string from Android to my PC (visual basic 2010) i try this one but it's not working. Please anyone help me..
我得到我的Android code来源: http://thinkandroid.word$p$pss.com/2010/03/27/incorporating-socket-programming-into-your-applications/
I get my Android Code from : http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
和它是这样的:
package com.zelacroix.bukumenu;
import java.io.*;
import java.net.*;
import android.app.Activity;
import android.os.*;
import android.util.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class TesKirim extends Activity {
private EditText serverIp;
private Button connectPhones;
private String serverIpAddress = "192.168.1.2";
private boolean connected = false;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teskirim);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
Toast.makeText(getApplicationContext(),
"masuk if",
10)
.show();
cThread.start();
}
}
}
});
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr =
InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr , 10000);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),
true);
// where you issue the commands
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
};
};
我的Visual Basic使用UDP方法。而且它是这样的:
my Visual Basic is using UDP method.. And its Look Like :
Imports System.Net.Sockets
Imports System.Net
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim udp_Client As New UdpClient(10000)
Dim remoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim receiveBytes As [Byte]() = udp_Client.Receive(remoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
ListBox1.Items.Add(returnData.ToString)
udp_Client.Close()
Catch ex As Exception
ListBox1.Items.Add(ex.ToString())
End Try
End Sub
End Class
请我really2需要的comunicate他们..
Please I really2 need to comunicate them..
我认为这个问题是由于因为你是混合TCP和UDP通讯。在Visual Basic应用程序能接收通过UDP和Android应用程序的数据传送通过TCP。我从你的code创建的Android和VB项目,称为继UDP从Android应用程序发送功能和通信工作正常。从Android的字符串在VB应用程序列表框中显示正确。尝试使用UDP套接字从Android应用程序如下:
I think the problem is caused since you are mixing TCP and UDP communication. The Visual basic app is expecting to receive data over UDP and the Android App is sending over TCP. I created the Android and VB projects from your code and called following UDP send function from the android app and the communication works fine. The string from Android appears correctly in the listbox in the VB app. Try using UDP sockets from your android app as follows:
private void udp_send(String serverIpAddress) throws IOException
{
String messageStr="Hello Android!";
int server_port = 10000;
DatagramSocket s = new DatagramSocket();
InetAddress local = InetAddress.getByName(serverIpAddress);
int msg_length=messageStr.length();
byte[] message = messageStr.getBytes();
DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
s.send(p);
}
[我想这code在Android模拟器,并在同一台机器上运行的VB应用程序。如果你正试图翻过不同的机器,仍然面临的问题与上面的code,确保UDP流量为10000端口没有被一些防火墙停止。你可以给从Android应用程序的详细异常日志,为我们更好,如果有必要]
[i tried this code on an android simulator and the VB app running on the same machine. in case you are trying accross different machines and still face problems with the above code, make sure that the udp traffic for the port 10000 is not stopped by some firewall. You can give the detailed exception log from the Android app for us to better understand the problem if necessary]