1

我有一个有效的消息传递应用程序,可以将消息从客户端发送到服务器。

  • 我希望客户端不断收听来自服务器的消息。
  • 我希望客户端仅在调用“开始”时将数据包发送到服务器,但问题是我必须按两次才能将消息实际传递到服务器(Idk 为什么)
  • 稍后我会将程序更改为始终将消息传递到服务器(他的 GPS 位置)的程序,因此我还想要一些关于如何在 Android 应用程序中创建循环的提示。

//客户

package com.example.clienttest;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
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, sOut;
    BufferedReader brOut, brIn;
    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.102", 4000);
                    oos = new DataOutputStream (clientSocket.getOutputStream());
                    ois = new DataInputStream (clientSocket.getInputStream());
                    brIn = new BufferedReader (new InputStreamReader(ois));


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

            }
        });
        m_objThreadClient.start();



    }

    public void Listener(){
        try {

            while ((sIn = brIn.readLine()) != null){
            sIn = brIn.readLine();
            }

            serverMessage.setText(sIn);

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

    public void Start(View view) {
        sOut = clientMessage.getText().toString();

                try {

                    oos.writeUTF(sOut);

                    oos.flush();
                    oos.flush();


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

    }

    public void onStop(){
        try {
            oos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            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();
                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;
    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() {
        String response;
        try {
            while ((response = inFromClient.readUTF()) != null) {

                Recv = inFromClient.readUTF();
                System.out.print("Msg: " + Recv + " \n");
                if( Recv.equals("Hi")){
                    outToClient.writeUTF("Wa alaikum");
                    outToClient.flush();
                }
                else{
                    outToClient.writeUTF("..what?");
                    outToClient.flush();
                }
            }
        } catch (IOException e) {
            System.out.print("No input ");
            System.err.println(e);
        }
    }

}
4

0 回答 0