2

我正在做一个安卓项目。我在哪里使用 Socket 编程。

但我越来越 java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused。从模拟器。

我已将本地主机更改为

"10.0.2.2" ("http://10.0.2.2/abc.php").

我还关注了java.net.ConnectException: localhost/127.0.0.1:8080 - Connection denied。但我没有得到任何解决方案。

另请注意模拟器IP地址与本地主机IP地址不匹配。所以我不知道如何解决这个问题。

这是请求的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;

import android.util.Log;

import com.mekya.interfaces.IAppManager;
import com.mekya.interfaces.ISocketOperator;

public class SocketOperator implements ISocketOperator
{
    private static final String AUTHENTICATION_SERVER_ADDRESS = "http://billbahadur.com/demo/androidchat/android_im/";

    private int listeningPort = 5550;

    private static final String HTTP_REQUEST_FAILED = null;

    private HashMap<InetAddress, Socket> sockets = new HashMap<InetAddress, Socket>();

    private ServerSocket serverSocket = null;

    private boolean listening;

    private IAppManager appManager;

    private class ReceiveConnection extends Thread {
        Socket clientSocket = null;
        public ReceiveConnection(Socket socket) 
        {
            this.clientSocket = socket;
            SocketOperator.this.sockets.put(socket.getInetAddress(), socket);
        }

        @Override
        public void run() {
             try {
    //          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                                    clientSocket.getInputStream()));
                String inputLine;

                 while ((inputLine = in.readLine()) != null) 
                 {
                     if (inputLine.equals("exit") == false)
                     {
                         appManager.messageReceived(inputLine);                      
                     }
                     else
                     {
                         clientSocket.shutdownInput();
                         clientSocket.shutdownOutput();
                         clientSocket.close();
                         SocketOperator.this.sockets.remove(clientSocket.getInetAddress());
                     }                       
                 }      

            } catch (IOException e) {
                Log.e("ReceiveConnection.run: when receiving connection ","");
            }           
        }   
    }

    public SocketOperator(IAppManager appManager) {
        this.appManager = appManager;   
    }


    public String sendHttpRequest(String params)
    {       
        URL url;
        String result = new String();
        try 
        {
            url = new URL(AUTHENTICATION_SERVER_ADDRESS);
            HttpURLConnection connection;
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);

            PrintWriter out = new PrintWriter(connection.getOutputStream());

            out.println(params);
            out.close();

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            connection.getInputStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                result = result.concat(inputLine);              
            }
            in.close();         
        } 
        catch (MalformedURLException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }           

        if (result.length() == 0) {
            result = HTTP_REQUEST_FAILED;
        }

        return result;


    }



    public boolean sendMessage(String message, String ip, int port) 
    {
        try {


            String[] str = ip.split("\\.");

            byte[] IP = new byte[str.length];

            for (int i = 0; i < str.length; i++) {

                IP[i] = (byte) Integer.parseInt(str[i]);                
            }
            Socket socket = getSocket(InetAddress.getByAddress(IP), port);
            if (socket == null) {
                return false;
            }

            PrintWriter out = null;
            out = new PrintWriter(socket.getOutputStream(), true);

            out.println(message);
        } catch (UnknownHostException e) {          
            return false;
            //e.printStackTrace();
        } catch (IOException e) {
            return false;           
            //e.printStackTrace();
        }

        return true;        
    }



    public int startListening(int portNo) 
    {
        listening = true;

        try {
            serverSocket = new ServerSocket(portNo);
            this.listeningPort = portNo;
        } catch (IOException e) {           

            //e.printStackTrace();
            this.listeningPort = 0;
            return 0;
        }

        while (listening) {
            try {
                new ReceiveConnection(serverSocket.accept()).start();

            } catch (IOException e) {
                //e.printStackTrace();              
                return 2;
            }
        }

        try {
            serverSocket.close();
        } catch (IOException e) {           
            Log.e("Exception server socket", "Exception when closing server socket");
            return 3;
        }


        return 1;
    }


    public void stopListening() 
    {
        this.listening = false;
    }

    private Socket getSocket(InetAddress addr, int portNo) 
    {
        Socket socket = null;
        if (sockets.containsKey(addr) == true) 
        {
            socket = sockets.get(addr);
            // check the status of the socket
            if  ( socket.isConnected() == false ||
                  socket.isInputShutdown() == true ||
                  socket.isOutputShutdown() == true ||
                  socket.getPort() != portNo 
                 )  
            {           
                // if socket is not suitable,  then create a new socket
                sockets.remove(addr);               
                try {
                    socket.shutdownInput();
                    socket.shutdownOutput();
                    socket.close();
                    socket = new Socket(addr, portNo);
                    sockets.put(addr, socket);
                } 
                catch (IOException e) {                 
                    Log.e("getSocket: when closing and removing", "");
                }               
            }
        }
        else  
        {
            try {
                socket = new Socket(addr, portNo);
                sockets.put(addr, socket);
            } catch (IOException e) {
                Log.e("getSocket: when creating", "");              
            }                   
        }
        return socket;      
    }


    public void exit() 
    {           
        for (Iterator<Socket> iterator = sockets.values().iterator(); iterator.hasNext();) 
        {
            Socket socket = (Socket) iterator.next();
            try {
                socket.shutdownInput();
                socket.shutdownOutput();
                socket.close();
            } catch (IOException e) 
            {               
            }       
        }

        sockets.clear();
        this.stopListening();
        appManager = null;
    //      timer.cancel();     
        }


        public int getListeningPort() {

            return this.listeningPort;
        }   

    }
4

1 回答 1

0

将 android_im 更改为 android-im,因为这是您的文件夹名称将保存在服务器的 Web 服务器目录中。还要在身份验证地址中提及服务器的端口号。

于 2015-04-28T06:00:14.130 回答