2

最近我不得不更换我的路由器,它是一个 D-Link 的 Belking,我的程序在我的 Belkin 路由器上工作,但现在不能在 D-Link 路由器上工作。

这是我的程序:

客户端:

package brainset.socket;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

/**
 *
 * @author Valter
 */
public class Client {  

    public static void main(String[] args) {  

        Socket s = null;  

        PrintStream ps = null;  

        try{  

            s = new Socket("valterhenrique.dyndns.info", 40000);  

            ps = new PrintStream(s.getOutputStream());  

            ps.println("lamp");  

        }catch(IOException e){  

            System.out.println("Some problem happens.");  
            e.printStackTrace();

        }finally{  

            try{  
                s.close();  
            }catch(IOException e){}  
        }  
    }  
}

这是我的服务器:

package brainset.socket;

// imports

public class Server {
    private Supervisory supervisory;

    public Server(Supervisory supervisory) {
        this.supervisory = supervisory;
    }

    public void start() {
        ThreadServer ts = new ThreadServer();
        Thread t = new Thread(ts);
        t.start();
    }

    class ThreadServer extends Thread {

        public void run() {
            ServerSocket ss = null;

            Socket socket = null;

            BufferedReader br = null;

            try {

                ss = new ServerSocket(40000);
                socket = ss.accept();
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("message:" + message);
                    try {
                        if (message.equals("lamp")) {
                            supervisory.active();
                            supervisory.switchLamp();
                        } else if (message.contains("airConditioning")) {
                            String airConditioning[] = message.split(":");
                            // temperature[0] = 'temperature'
                            // temperature[1] = temperature value
                            supervisory.active();
                            supervisory.changeTemperature(Float.parseFloat(airConditioning[1]));
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    socket = ss.accept();
                    br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                }

            } catch (IOException ioe) {

                ioe.printStackTrace();
            } finally {

                try {
                    socket.close();
                    ss.close();

                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }

    public static void main (String args[]){
        Server s = new Server(new Supervisory("192.168.1.149", "192.168.1.255", 101));
        s.start();
    }

}

我已经在我的新路由器中打开了一个端口并更新了 DynDns.org 中的主机名,但仍然不断启动异常:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at brainset.socket.Client.main(Client.java:28)

如果我更改主机名“valterhenrique.dyndns.info”,它可以工作,但这不是我想要的,我想使用主机名,因为我在动态 ip 网络中。

任何想法 ?

4

1 回答 1

0

我认为您需要允许外部访问您的网络。在Port Forwarding页面中(在路由器的配置页面中)添加一个条目,将外部请求转发到 LAN 中的特定地址。

于 2012-05-21T20:26:14.150 回答