如何将数据发送到套接字服务器?

时间:2014-04-07 05:13:56

标签: android vb.net sockets

我在VB.NET中有一个代码来创建套接字服务器。我需要为Android创建一个向其发送数据的应用程序,它会在屏幕上显示收到的消息。

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

Public Module Module1
    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()
        Try
            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()
                    Dim data As String = ""

                    While (True)
                        Dim bytes(handler.ReceiveBufferSize) As Byte
                        Dim bytesRec As Integer = handler.Receive(bytes)
                        data = Encoding.ASCII.GetString(bytes, 0, bytesRec)
                        If data.Length > 0 Then
                            Exit While
                        End If
                    End While

                    Console.WriteLine("Mensagem recebida: " & data & "")

                    handler.Shutdown(SocketShutdown.Both)
                    handler.Close()
                End While
            End If
        Catch ex As Exception

        End Try
    End Sub
End Module

上面的代码是VB.NET中的一个代码,用于打开套接字服务器以接收来自客户端的消息。问题是我需要使用Android代码创建一个客户端。

我已尝试创建客户端,请检查以下代码。

package com.pcriot.maxsoft.testapp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
    private TextView LabelStatus = null;

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

        LabelStatus = (TextView)findViewById(R.id.LabelStatus);

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

                    DataInputStream DIStream = new DataInputStream(socket.getInputStream());
                    DataOutputStream DOStream = new DataOutputStream(socket.getOutputStream());

                    String msg = "teste";
                    DOStream.write(msg.getBytes(), 0, msg.getBytes().length);
                    DOStream.flush();

                    String text = DIStream.readLine();
                    LabelStatus.setText(text);

                    DOStream.close();
                    DIStream.close();

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

我发现没有错误,客户端和服务器似乎是正确的。但是,当我在计算机上打开服务器并访问智能手机上的应用程序时,没有收到任何消息。错误是什么?

Ps:我在AndroidManifest上设置了INTERNET权限。

1 个答案:

答案 0 :(得分:1)

您好添加了一个链接,为Android的套接字服务器编程提供了一个很好的教程,您可以在其中找到发送和接收代码please click here to see the code