简体   繁体   English

android / java套接字帮助

[英]android/java sockets help

Here is my goal. 这是我的目标。 I am designing a server that will spit out data every second. 我正在设计一个服务器,它将每秒吐出数据。 I need to also design a client (on android) that will connect to that server and gather the data that is being sent by the server. 我还需要设计一个客户端(在android上),该客户端将连接到该服务器并收集服务器发送的数据。 I have already wrote up some code for this in java and for the android. 我已经在Java和Android中为此编写了一些代码。 However, my experience with java and android is very minimal. 但是,我在java和android方面的经验很少。 I have been having a lot of difficulty with this but have started making progress. 我遇到了很多困难,但已经开始取得进展。 I was wondering if everyone could take a look at my code and give some tips on how to best perform this design. 我想知道是否每个人都可以看一下我的代码并提供一些有关如何最好地执行此设计的提示。

The server will just be constantly spitting out data and the client will connect to the server and then start gathering the data that is being sent. 服务器将不断吐出数据,而客户端将连接到服务器,然后开始收集要发送的数据。

The data that is being sent is not important. 正在发送的数据并不重要。 Right now it will just be something random for testing. 现在这只是随机测试。

Thanks in advance! 提前致谢!

THE JAVA SERVER

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;





public class SmartServerSocket {

    private ServerSocket server;

    private int port = 7777;



    public SmartServerSocket() {

        try {

            server = new ServerSocket(port);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    public static void main(String[] args) {

        SmartServerSocket smart = new SmartServerSocket();

        smart.handleConnection();

    }

    public void handleConnection() {

        // TODO Auto-generated method stub

        System.out.println("Waiting for client message...");

        while (true) {

            try {

                Socket socket = server.accept();

                new ConnectionHandler(socket);

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

}

class ConnectionHandler implements Runnable {

    private Socket socket;

    public ConnectionHandler(Socket socket) {

        this.socket = socket;

        Thread t = new Thread(this);

        t.start();

    }

    public void run() {

        ObjectOutputStream oos = null;

        int test = 0;

        try {

            while(true)

            {

                oos = new ObjectOutputStream(socket.getOutputStream());

                oos.writeObject(Integer.toString(test));

                test++;

                System.out.println("Waiting for client message...");

            } 

        } catch (IOException e) {

                e.printStackTrace();

        } finally {

            try {

                    oos.close();

                    socket.close();

                } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }



            }

        }

    }




THE ANDROID CLIENT
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//import android.app.Activity;

public class ConnectDevice implements Runnable {
    public void run(){
        try {
            System.out.println("test1");
            //InetAddress host = InetAddress.getLocalHost();
            InetAddress host = InetAddress.getByName("THEIPADDRESS");
            Socket socket = new Socket(host.getHostName(), 7777);
            System.out.println("test2");
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            System.out.println("test3");
            String message = (String) ois.readObject();
            System.out.println("test4");    
            System.out.println("Message: " + message);

            ois.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

I'm using Apache Mina to handle my game's connection. 我正在使用Apache Mina处理游戏的连接。 You should take a look on it. 您应该看一下。 It have really made my life easier. 这确实使我的生活更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM