本文介绍了VB.Net服务器和放大器; Android客户端(插座)发送和放大器;接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid程序我写了一个简单的服务器(VB.NET)/客户端(的Java / Android的)计划。在Android / Java的文字是成功发送到VB.Net但VB.Net反应不是Android中/ Java的(收到的 buffer.readLine() 返回

我缺少的东西吗?

下面是我的codeS

VB.NET(服务器)

 导入的System.Net.Sockets
进口System.Text公共类Form1的昏暗的服务器作为新的TcpListener(9999)
昏暗的客户端作为新的TcpClient
昏暗的流作为的NetworkStream私人小组Form1_Load的(发送者为对象,E作为EventArgs的)把手MyBase.Load结束小组私人小组Start_Click(发送者为对象,E作为EventArgs的)把手Start.Click
    Me.Text =等待......
    昏暗str作为字符串
    server.Start()
    客户端= server.AcceptTcpClient
    流= client.GetStream()
    昏暗的r_byt(client.ReceiveBufferSize)为字节
    stream.Read(r_byt,0,client.ReceiveBufferSize)
    海峡= Encoding.ASCII.GetString(r_byt)
    Label1.Text = STR
结束小组私人小组Responce_Click(发送者为对象,E作为EventArgs的)把手Responce.Click
    昏暗的s_byt()作为字节= Encoding.ASCII.GetBytes(知道了&安培; VBCR)
    stream.Write(s_byt,0,s_byt.Length)
    stream.Flush()
    stream.Close()
    client.Close()
    server.Stop()
结束小组

的Andr​​oid / Java的(客户端)

 进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.io.PrintStream中;
进口的java.net.Socket;
进口的java.net.UnknownHostException;进口android.os.Bundle;
进口android.app.Activity;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;公共类主要活动扩展{按钮buttonSend,buttonReceive;
私有静态Socket套接字= NULL;
PrintStream的流= NULL;
的BufferedReader缓冲= NULL;
串串=A;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    buttonSend =(按钮)findViewById(R.id.buttonSend);
    buttonSend.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            尝试{
                插座=新的Socket(192.168.0.104,9999);
                流=新的PrintStream(socket.getOutputStream());
                stream.println(嗨服务器...);                缓冲区=新的BufferedReader(新的InputStreamReader(socket.getInputStream()));
                串= buffer.readLine();
                Log.d(ServerActivity, - +串);
                buffer.close();
                socket.close();
            }赶上(UnknownHostException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
        }
    });    }
}


解决方案

看起来你有几个问题:


  • 在服务器端,你正试图从套接字读的东西
    r_byt ,而你不写任何东西给它cliente的一面。
    当您在服务器端preSS发送按钮, r_byt 还是空和
    这就是你收到cliente侧的东西。


  • 在客户端调用套接字读取阻止和后数
    在几秒钟内将导致ANR错误(应用程序无响应)
    该cliente。你应该将读不同的线程插座
    从UI。较新的Andr​​oid版本不甚至让你从中读取
    在UI线程的插座。


问候。

I am new to Android programming I wrote a simple Server(VB.NET) / Client(Java/Android) program. Text from Android/Java is send successfully to VB.Net but Response from VB.Net is not received in Android/Java (buffer.readLine() returns null)

Am I missing something?

Here are my Codes

VB.NET (Server)

Imports System.Net.Sockets
Imports System.Text

Public Class Form1

Dim server As New TcpListener(9999)
Dim client As New TcpClient
Dim stream As NetworkStream

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
    Me.Text = "Waiting...."
    Dim str As String
    server.Start()
    client = server.AcceptTcpClient
    stream = client.GetStream()
    Dim r_byt(client.ReceiveBufferSize) As Byte
    stream.Read(r_byt, 0, client.ReceiveBufferSize)
    Str = Encoding.ASCII.GetString(r_byt)
    Label1.Text = str
End Sub

Private Sub Responce_Click(sender As Object, e As EventArgs) Handles Responce.Click
    Dim s_byt() As Byte = Encoding.ASCII.GetBytes("Got it" & vbCr)
    stream.Write(s_byt, 0, s_byt.Length)
    stream.Flush()
    stream.Close()
    client.Close()
    server.Stop()
End Sub

Android/Java(Client)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {

Button buttonSend, buttonReceive;
private static Socket socket = null;
PrintStream stream = null;
BufferedReader buffer = null;
String string = "a";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonSend = (Button) findViewById(R.id.buttonSend);
    buttonSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try {
                socket = new Socket("192.168.0.104", 9999);
                stream = new PrintStream(socket.getOutputStream());
                stream.println("Hi Server...");

                buffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                string = buffer.readLine();
                Log.d("ServerActivity", " - " + string);
                buffer.close();
                socket.close();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    }
}
解决方案

It looks like you have a couple of issues:

  • In the server side, you are trying to read something from the socketinto r_byt, and you are not writing anything to it on cliente side.When you press the send button on server side, r_byt still null andthat's what you receive on cliente side.

  • On client side the call to socket read is blocking and after a fewseconds will result in a ANR error (Application not Responding) inthe cliente. You should move the socket read to a different threadfrom the UI. The newer Android versions don't even let you read froma socket in the UI thread.

Regards.

这篇关于VB.Net服务器和放大器; Android客户端(插座)发送和放大器;接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:19