Java Socket中的连接被拒绝错误

时间:2017-12-31 08:06:48

标签: java sockets

我正在尝试构建一个允许远程PC控制的软件。到目前为止,我的代码能够在客户端和服务器位于同一台PC上时将服务器屏幕共享给客户端,但它显示“连接”当我试图将客户端和服务器保存在不同的笔记本电脑中时,我拒绝了“错误。

 public static void main(String args[]) throws IOException, 
 ClassNotFoundException {

    DrawGui();
     ServerSocket ss = new ServerSocket(3389);
    //Socket server = ss.accept();
    while(true)
    {
    Socket server = ss.accept();
    new ReceiveScreenshots(server,frame,desktop,inter,panel,label);
    }

}

我被建议将ss.accept()保留在while循环之外。但是,如果我将我的代码更改为以下内容并将ss.accept()保留在while循环之外,那么我的代码会显示许多错误。

 public static void main(String args[]) throws IOException, ClassNotFoundException {

    DrawGui();
     ServerSocket ss = new ServerSocket(3389);
    Socket server = ss.accept();
    while(true)
    {
    //Socket server = ss.accept();
    new ReceiveScreenshots(server,frame,desktop,inter,panel,label);
    }

}

有人能帮帮我吗?以下是客户端的代码

  public class Test {
 public static void main(String args[]) throws AWTException, IOException
 {
    Socket s = new Socket("localhost",3389);
    Robot robot = null;
    GraphicsEnvironment gen = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gdev = gen.getDefaultScreenDevice();
    robot = new Robot(gdev);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle rectangle = new Rectangle(dim);
    new Client(s,robot,rectangle);

}
}

public class Client extends Thread {

Socket s;
Rectangle rectangle = null;

Robot robot = null;
ImageIcon previous = new ImageIcon();
public Client(Socket s, Robot robot, Rectangle rectangle) {

    this.s = s;
    this.rectangle = rectangle;
    this.robot = robot;
    start();
}

public void run() {
    ObjectOutputStream ous = null;
    try {
        ous = new ObjectOutputStream(s.getOutputStream());
    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
    while (true) {
        BufferedImage bImage = robot.createScreenCapture(rectangle);
        ImageIcon image = new ImageIcon(bImage);
        if(previous.equals(image))
            continue;
        else
        {
        try {
            System.out.println("before sending image");
            ous.writeObject(image);
            System.out.println(" image sent");
            ous.reset();
            System.out.println(" stream reset");
            previous = image;
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }}

}

1 个答案:

答案 0 :(得分:0)

此处没有可以抛出 data=[{name:'HP',id:1}......] search = (text$: Observable<string>) => text$ .debounceTime(200) .distinctUntilChanged() .map(term => { const matchRegExp = new RegExp(term, 'gi'); return term.length === 0 ? [] : this.ar_items_manfs.filter(v => { return this.selectedItems.indexOf(v) === -1 && matchRegExp.test(v); }); }); selected($e) { $e.preventDefault(); this.selectedItems.push($e.item); console.log($e); console.log('--------'); this.inputEl.nativeElement.value = ''; } close(item) { this.selectedItems.splice(this.selectedItems.indexOf(item), 1); this.inputEl.nativeElement.focus(); } 的代码,但这意味着没有任何内容正在侦听目标IP:端口。

我们建议您将ConnectException: connection refused保持在ss.accept()循环之外?基于什么理由?

while

此代码仅在服务器位于本地主机中时才有效。如果不是,则必须提供IP地址或主机名,而不是Socket s = new Socket("localhost",3389); 。当然这很明显?