2

我正在尝试实现可以​​识别不同 URL 请求并对每个请求执行不同操作的代码,例如,通过访问拍照http://192.168.0.120/pic,并通过访问发送电子邮件http://192.168.0.120/email 我已经构建了用于拍照和发送电子邮件的代码,但不知道如何将它们分配给不同的 URL 请求?我找到了一个可以运行 Web 服务器以仅识别一个 IP 地址的代码,我想对其进行修改以识别多个 IP 地址并针对每个请求执行不同的操作:代码:

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class Server extends Thread {
    private ServerSocket listener = null;
    private static Handler mHandler;
    private boolean running = true;

    public static LinkedList<Socket> clientList = new LinkedList<Socket>();

    public Server(String ip, int port, Handler handler) throws IOException {
        super();
        mHandler = handler;
        InetAddress ipadr = InetAddress.getByName(ip);
        listener = new ServerSocket(port,0,ipadr);
    }

    private static void send(String s) {
        Message msg = new Message();
        Bundle b = new Bundle();
        b.putString("msg", s);
        msg.setData(b);
        mHandler.sendMessage(msg);
    }

    @Override
    public void run() {
        while( running ) {
            try {

                Socket client = listener.accept();


                new ServerHandler(client).start();
                LockStatus.getInstance().setMyVar(true);


                clientList.add(client);

            } catch (IOException e) {

            }
        }
    }

    public void stopServer() {
        running = false;
        LockStatus.getInstance().setMyVar(false);
        try {
            listener.close();
        } catch (IOException e) {

        }
    }

非常感谢

4

1 回答 1

0

这是代码的修改,但仍然无法识别IP地址:

public void run() {
        try {
            serverSocket = new ServerSocket(SERVERPORT);

            while (running) {
                // LISTEN FOR INCOMING CLIENTS
                Socket client = serverSocket.accept();

                try {
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(client.getInputStream()));
                    String line = null;
                    String IP = "192.168.0.111";
                    line = in.readLine();
                    if (line.equals(IP)) {
                        new ServerHandler(client).start();
                        LockStatus.getInstance().setMyVar(true);
                        Log.i(TAG, "IP Receive=" + line);
                        // Toast.makeText(getContext(), "Matches",
                        // Toast.LENGTH_SHORT).show();
                    } else {
                        Log.i(TAG, " IP not received :=" + line);
                        // Toast.makeText(getApplicationContext(), line +" != "+
                        // IP, Toast.LENGTH_SHORT).show();

                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } 
于 2013-08-18T10:27:58.977 回答