Android可以向服务器发送消息但无法从服务器接收消息

时间:2013-10-01 13:49:17

标签: java android client-server

我可以从客户端接收服务器上的消息,但我无法从服务器检索到客户端的回复。知道为什么吗?这是客户端和服务器的代码。

客户代码

package com.example.clienttest;

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

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;

    @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("192.168.1.55", 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) {
        sOut = clientMessage.getText().toString();
          try {
              oos.writeUTF(sOut);

              ServerConn sc = new ServerConn(clientSocket);
              Thread t = new Thread(sc);
              t.start();

              serverMessage.setText("Reply: " + sc.msg + "\n");

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

    }

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

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

}

class ServerConn implements Runnable {
    private DataInputStream ois = null;
    public String msg;

    public ServerConn(Socket server) throws IOException {
         ois = new DataInputStream (server.getInputStream());
    }

    public void run() {
        try {

            while ((msg = ois.readUTF()) != null) {
            }
        } catch (IOException e) {
            msg = e.toString();
        }
    }
}

服务器代码

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();
                System.out.print("Connected ");
            } 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 = "Wa alaikum";
                }
                else{
                    Send = "Wat?";
                }

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

}

1 个答案:

答案 0 :(得分:0)

ServerConn中的msg变量在从服务器读取时被赋予一个值,该值在运行ServerConn对象的线程启动之后不是紧接的。需要有一些代码(Handler ??)在GUI线程上运行,当它获取msg时从ServerConn类调用。 在run()方法中添加一些调试代码,在读取时打印出msg的值,以便您可以看到msg中的值。打印输出可在LogCat中查看