将字符串从android发送到visual basic 2010

时间:2012-07-04 07:13:15

标签: android visual-studio-2010 wifi android-wifi

我想从Android发送一个字符串到我的电脑(visual basic 2010)我试试这个,但它没有用。请有人帮帮我..

我的Android代码来自: 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方法..它看起来像:

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

请我真的需要通信他们..

1 个答案:

答案 0 :(得分:5)

我认为问题是由于您正在混合TCP和UDP通信引起的。 Visual Basic应用程序期望通过UDP接收数据,而Android App正在通过TCP发送。我从你的代码创建了Android和VB项目,并从android应用程序调用了以下UDP发送功能,通讯工作正常。来自Android的字符串在VB应用程序的列表框中正确显示。尝试使用Android应用程序中的UDP套接字,如下所示:

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);
        }

[我尝试在Android模拟器上运行此代码,并在同一台机器上运行VB应用程序。如果您尝试使用不同的计算机并仍然遇到上述代码的问题,请确保某些防火墙未阻止端口10000的udp流量。您可以从Android应用程序中提供详细的例外日志,以便我们在必要时更好地了解问题]