Android - 消息已准备好阅读,但我无法获取它们

时间:2013-10-05 13:48:12

标签: java android client messages

简单步骤:

  • 客户端编写邮件并将其发送到服务器(正常工作)
  • 邮件已发送到服务器(正常工作)
  • 服务器抓取消息(正常工作)
  • 服务器处理邮件并做出适当的回复(工作正常)
  • 服务器将消息发送回客户端(正常工作)
  • 消息传递给客户端(工作正常)
  • 客户端抓取消息(nop ....问题)

正如您在代码中看到的那样;每次按下时,“oos”的大小实际上都在增加,但我无法以某种方式从oos中读取数据。

当我按下按钮时,Start会被调用,当我按下另一个按钮时,Get会被调用。

//客户端

package com.example.clienttest;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

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

public class MainActivity extends Activity {

    Thread m_objThreadClient;
    Socket clientSocket;
    TextView serverMessage;
    EditText clientMessage;
    String sIn = "Nothing", sOut;
    DataOutputStream oos;
    DataInputStream ois;
    int i = 0;

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

        serverMessage = (TextView) findViewById(R.id.textView1);
        clientMessage = (EditText) findViewById(R.id.editText1);


        m_objThreadClient = new Thread( new Runnable(){
            public void run()
            {
                try {
                    clientSocket = new Socket("10.0.2.2", 4000);
                    oos = new DataOutputStream (clientSocket.getOutputStream());
                    ois = new DataInputStream (clientSocket.getInputStream());


                } catch (Exception e) {
                    serverMessage.setText(e.getMessage());
                }

            }
        });
        m_objThreadClient.start();


    }

    public void Start(View view)throws IOException{
        sOut = clientMessage.getText().toString();
          try {
              oos.writeUTF(sOut);
              serverMessage.setText("Messages availible:" + ois.available() );

          }
          catch (Exception e) {
          }

    }
    public void Get(View view){
          try {

                sIn = ois.readUTF();
                serverMessage.setText("Message: " + sIn );
          }
          catch (Exception e) {
          }

    }

    public void onStop(){
        try {
            oos.close();
            ois.close();
            clientSocket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//服务器

import java.net.ServerSocket;
import java.net.Socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

import java.util.Hashtable;

public class Server2 {

    @SuppressWarnings("resource")
    public static void main (String[] args) throws IOException {

        ServerSocket server = null;  
        try {
            server = new ServerSocket(4000); 
        } catch (IOException e) {
            System.err.println("Could not start up on: " + "4000" + "Maby server is already open? Or a portforwording messup?");
            System.err.println(e);
            System.exit(1);
        }

        Socket client = null;
        while(true) {
            try {
                client = server.accept();
                String IP = client.getInetAddress().toString();
                System.out.print(IP + " Connected\n");
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.err.println(e);
            }

            Thread t = new Thread(new ClientConn(client));
            t.start();
        }
    }
}


class ClientConn implements Runnable {
    private Socket client;
    String Recv, Send;
    DataInputStream inFromClient;
    DataOutputStream outToClient;

    ClientConn(Socket client) {
        this.client = client;
        try {

            inFromClient = new DataInputStream(client.getInputStream());
            outToClient = new DataOutputStream(client.getOutputStream());
            } catch (IOException e) {
            e.printStackTrace();
            }
    }

    public void run() {
        try {
            while ((Recv = inFromClient.readUTF()) != null) {

                System.out.print("Msg: " + Recv + " \n");
                if( Recv.equals("Hi")){
                    Send = "Welcome";
                }
                else{
                    Send = "Wat?";
                }

                outToClient.writeUTF(Send);
                System.out.print("Replying:" + Send + "\n");
            }
        } catch (IOException e) {
            System.out.print("No input ");
            System.err.println(e);
        }
    }

}

这让我很困惑:( ....

1 个答案:

答案 0 :(得分:0)

尝试从服务器发送一些数据后尝试刷新数据并尝试

 outToClient.writeUTF(Send);
 outToClient.flush();//add this one

顺便说一句,当你点击调用get()方法时,客户端正在等待一些消息,服务器也在等待某些消息,也许你需要发送一些东西才能得到一些东西,比如这样。 / p>

public void Get(View view){
          try {
                oos.writeUTF("What's up?!");
                oos.flush();
                sIn = ois.readUTF();
                serverMessage.setText("Message: " + sIn );
          }
          catch (Exception e) {
          }

    }
<顺便说一下,请遵循Java命名规则,您的方法遵循m $ si-sharp命名规则Get() - > get()谢谢。 :)