Android客户端,Pc Server Socket d'ont工作超过3g

时间:2011-07-19 14:03:55

标签: android client 3g pc

我尝试创建一个客户端服务器套接字beetwen我的机器人(客户端)和我的PC(服务器),当我在本地(通过wifi)它完成工作,但当我尝试通过3G我得到此异常服务器时尝试获取clientsocket.getOutputStream()

at java.lang.Thread.run(Unknown Source)
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)

问题是什么,让人知道解决方案吗? 请帮忙: - (

服务器

public class Server {

ServerSocket serverSocket;
public LinkedBlockingQueue<CDRecCourseDisplay> recCours;
public LinkedList<ClientMail> clientMails;
static Server server;

public static Server getInstance(){
    if(server == null){
        server = new Server();
    }
    return server;
}

Server() {
    // TODO Auto-generated constructor stub
    try {
        serverSocket = new ServerSocket(54444);
        recCours = new LinkedBlockingQueue<CDRecCourseDisplay>(10);
        clientMails = new LinkedList<ClientMail>();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    this.start();
}

private void start(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true){
                try {
                    Socket socket = serverSocket.accept();
                    new Thread(new Client(socket)).start();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }).start();
}

class Client implements Runnable{

    Socket socket;
    DataInputStream in;
    DataOutputStream  out;

    public Client(Socket socket) {
        // TODO Auto-generated constructor stub
        this.socket = socket;
        if(socket == null) return;
        try {
            InputStream i = socket.getInputStream();
            OutputStream o = socket.getOutputStream();
            in = new DataInputStream(i);
            out = new DataOutputStream(o);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(true){
                    try {
                        out.writeUTF("Test Message");
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(true){
                    try {
                        String buf = in.readUTF();  
                        Log.d("MESSAGE", buf);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
            }
        }).start();
    }

}

}

和客户

class Client implements Runnable{

    Socket socket;
    DataInputStream in;
    DataOutputStream  out;

    public void run() {
        // TODO Auto-generated method stub
        boolean conected = false;
        while(!conected){
            try {
                Thread.sleep(500);
                socket = new Socket("213.233.216.25", 54444);
                in = new DataInputStream(socket.getInputStream());
                out = new DataOutputStream(socket.getOutputStream());
                conected = true;
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                Log.e("ERROR :", e.getMessage());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("ERROR :", e.getMessage());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Log.e("ERROR :", e.getMessage());
            }
        }

        new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                while(true){
                    try {
                        String buf = in.readUTF();
                        log.d("MESSAGE", buf);
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }).start();


        new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                while(true){
                    try {
                        out.writeUTF("Test message from the phone");
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

1 个答案:

答案 0 :(得分:2)

大多数网络(Wifi和3G)使用NAT。 NAT允许出站连接,但阻止入站(互联网到设备)连接。

如果您的服务器和设备在同一网络上(如您的情况),那么这可以在您不遍历NAT网关时起作用。

理由:您尝试做的事情(从互联网连接到设备)在大多数网络中无效。