Android Socket通讯

时间:2014-04-07 00:01:59

标签: android vb.net sockets

我正在创建一个与计算机通信的Android应用程序。这是MainActivity的代码。

package com.pcriot.maxsoft.testapp;

import java.io.OutputStream;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView textView1 = null;
    EditText editText1 = null;
    Button button1 = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView1 = (TextView)findViewById(R.id.textView1);
        editText1 = (EditText)findViewById(R.id.editText1);
        button1 = (Button)findViewById(R.id.button1);

        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                final String message = editText1.getText().toString();

                new Thread(new Runnable() {
                    public void run() {
                        try {
                            Socket socket = new Socket("10.0.0.100", 11000);

                            OutputStream OStream = socket.getOutputStream();
                            OStream.write(message.getBytes(), 0, message.getBytes().length);
                            OStream.flush();
                            OStream.close();

                            socket.close();
                        } catch (Exception e) {
                            textView1.setText(e.toString());
                        }
                    }
                }).start();
            }
        });
    }
}

我还在VB.NET中创建了一个程序来接收Android上客户端发送的数据。代码如下。

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Module Module1
    Public data As String = Nothing

    Private IPAddress As IPAddress = IPAddress.Parse("10.0.0.100")
    Private IPEndPoint As New IPEndPoint(IPAddress, 11000)
    Private Socket As Socket = Nothing

    Public Sub Main()
        Dim bytes() As Byte = New [Byte](1024) {}

        Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        If Not IsNothing(Socket) Then
            Socket.Bind(IPEndPoint)
            Socket.Listen(10)

            While (True)
                Dim handler As Socket = Socket.Accept()
                data = Nothing

                While True
                    bytes = New Byte(1024) {}
                    Dim bytesRec As Integer = handler.Receive(bytes)
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec)
                    If data.IndexOf("<EOF>") > -1 Then
                        Exit While
                    End If
                End While
                ' Show the data on the console.
                data = data.Replace("<EOF>", "")
                Console.WriteLine("Text received : {0}", data)
                ' Echo the data back to the client.
                Dim msg As Byte() = Encoding.ASCII.GetBytes(data)
                handler.Send(msg)
                handler.Shutdown(SocketShutdown.Both)
                handler.Close()
            End While
        End If
    End Sub
End Module

VB.NET中的程序运行顺畅。我用一个也在VB.NET中创建的客户端测试了它,但是我无法在Android上创建客户端,你能帮助我吗?

0 个答案:

没有答案