使用AsyncTask的Android套接字客户端

时间:2018-06-21 00:56:41

标签: android sockets android-asynctask client inputstream

我想让应用程序可以使用套接字客户端

但是从服务器接收数据时有问题

此处MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button send;
    EditText et;
    SocketAsync sa;
    TextView tv;

    String ip = "192.168.7.26";
    int port = 7080;

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

        send = (Button)findViewById(R.id.button);
        et = (EditText)findViewById(R.id.input);
        tv = (TextView)findViewById(R.id.output);

        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.button:
                sa = new SocketAsync();
                sa.execute(ip, port, et.getText(), this);
            break;
        }
    }

    public void setRecvText(String s)
    {
        tv.append(s);
    }
}

这是SocketAsync代码

@Override
protected Object doInBackground(Object... objects) {
    this.ip = objects[0].toString();
    this.port = (int)objects[1];
    this.send_msg = objects[2].toString();
    this.context = (Context)objects[3];
    try {
        socket = new Socket(ip, port);
        networkWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        PrintWriter out = new PrintWriter(networkWriter, true);
        out.println(send_msg);

        ReadNet rn = new ReadNet();
        String s = rn.ReadValue(socket);

        publishProgress(s);
        socket.close();    
    }
    catch(Exception ex) {
        publishProgress(ex.getMessage());
    }
    return null;
}

@Override
protected void onProgressUpdate(Object... values) {
    super.onProgressUpdate(values);
    ((MainActivity)context).setRecvText(values[0].toString());
}

和ReadNet中的ReadValue

public String ReadValue(Socket socket)
{

    try {
        StringBuilder sb = new StringBuilder();
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        char buff[] = new char[1024];
        int nbytes = 0;
        while(true){
            Log.e("nbytes1", nbytes+"");
           nbytes = in.read(buff); //here...breakpoint is missing when server dosen't send data
           if (nbytes == -1)
                break;
           sb.append(new String(buff).substring(0, nbytes));
           Log.e("nbytes2", nbytes+"");
        }
        return  sb.toString();
    }
    catch (IOException e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

我正在运行此应用程序,当服务器发送一些数据时,它会从服务器接收数据。

但是服务器没有发送数据,它有问题。

所以我调试它,当nbytes可能为-1时缺少断点,没有错误消息并且应用程序没有死,但是应用程序无法工作...

我也使用

byte[] buff = new byte[1024];
InputStream in = socket.getInputStream();
nbytes = in.read(buff);

while ((nbytes = in.read(buff)) != -1) {
       sb.append(new String(buff).substring(0, nbytes));
}

服务器从客户端接收数据时发送数据

我使用C#制作服务器,并且该代码在线程上运行

NetworkStream stream = null;
try
{
    byte[] buffer = new byte[1024];
    string msg = string.Empty;
    int bytes = 0;
    int MessageCount = 0;

    while (true)
    {
        MessageCount++;
        stream = clientSocket.GetStream();
        bytes = stream.Read(buffer, 0, buffer.Length);
        msg = Encoding.ASCII.GetString(buffer, 0, bytes);
        msg = "Data Received : " + msg;

        if (OnReceived != null)
            OnReceived(msg);

        stream.Write(buffer, 0, buffer.Length);
        stream.Flush();

        msg = " >> " + msg;
    }
}

catch (SocketException se)
{
    if (clientSocket != null)
    {
        clientSocket.Close();
        stream.Close();
    }

    if (OnCalculated != null)
        OnCalculated();
}

catch (Exception ex)
{
    if (clientSocket != null)
    {
        clientSocket.Close();
        stream.Close();
    }

    if (OnCalculated != null)
        OnCalculated();
}

有同样的问题

我该如何解决?

0 个答案:

没有答案