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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22904092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:18:57  来源:igfitidea点击:

How to send data to a socket server?

androidvb.netsockets

提问by Jhonas Kenne Boeno

I have a code in VB.NET to create a socket server. I need to create an application for Android that send data to it and it shows the received message on the screen.

我在 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

The above code is a code in VB.NET that opens the socket server to receive messages from clients. The problem is that I need to create a client with Android code.

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

I've tried to create a client, check the code below.

我试图创建一个客户端,请检查下面的代码。

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

I found no error, the client and the server appears to be correct. But when I open the server on my computer and access the app on my smartphone, no message is received. What is the error?

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

Ps: I set INTERNET permission on AndroidManifest.

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

回答by Arun Antoney

Hi added a link which provide a nice tutorial for socket server programming for android where you can find sending and receiving code please click here to see the code

您好添加了一个链接,它提供了一个很好的 android 套接字服务器编程教程,您可以在其中找到发送和接收代码,请单击此处查看代码