2016-10-05 8 views
2

サーバ側で接続リセットエラーが発生することなく、ソケットを切断する方法がないようです。ソケットソケットのクローズ

私はcom.codename1.io.Socketcom.codename1.io.SocketConnectionの実装をテスターアプリ内で使用しています。次のように私のコードです:

private SpanLabel lblStatus; 
private SpanLabel lblIncoming; 
private CustomSocketConnection con; 
private Thread tIncoming; 

public ConnectForm() { 
    con = getSocketConnection(); 
    Button btnConnect = getConnectButton(); 
    Button btnDisconnect = getDisconnectButton(); 
    Button btnSendMessage = getSendMessageButton(); 
    lblStatus = getInfoLabel(); 
    lblIncoming = getIncomingLabel(); 

    setLayout(new BoxLayout(BoxLayout.Y_AXIS)); 
    addComponent(btnConnect); 
    addComponent(btnDisconnect); 
    addComponent(btnSendMessage); 
    addComponent(lblStatus); 
    addComponent(lblIncoming); 
} 

private Button getConnectButton() { 
    Button btn = new Button("Connect (localhost)"); 
    btn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      Socket.connect("localhost", 8687, con); 
     } 
    }); 
    return btn; 
} 

private Button getDisconnectButton() { 
    Button btn = new Button("Disconnect"); 
    btn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      //??? I don't know how to do this 
      try { 
       tIncoming.join(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       tIncoming.interrupt(); 
      } 
     } 
    }); 
    return btn; 
} 

private Button getSendMessageButton() { 
    Button btn = new Button("Send Message"); 
    btn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      try { 
       con.os.write("Hello".getBytes()); 
       con.os.write(Integer.parseInt("04", 16)); //end of transmit 
       con.os.flush(); 
       lblStatus.setText("Message Sent"); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    return btn; 
} 

private SpanLabel getInfoLabel() { 
    return new SpanLabel("Disconnected"); 
} 

private SpanLabel getIncomingLabel() { 
    return new SpanLabel("..."); 
} 

private CustomSocketConnection getSocketConnection() { 
    return new CustomSocketConnection(); 
} 

class CustomSocketConnection extends SocketConnection { 

    public OutputStream os; 
    public InputStream is; 

    @Override 
    public void connectionError(int errorCode, String message) { 
     lblStatus.setText("Error Connecting. ErrorCode: " + errorCode + " Message: " + message); 
    } 

    @Override 
    public void connectionEstablished(InputStream is, OutputStream os) { 
     lblStatus.setText("Connected :)"); 
     this.is = is; 
     this.os = os; 
     spawnIncomingMessageWatcher(); 
    } 
} 

private void spawnIncomingMessageWatcher() { 
    tIncoming = new Thread(new Runnable() { 
     public void run() { 
      String s = ""; 
      int eot = Integer.parseInt("04", 16); 
      while (con.isConnected()) { 
       try { 
        int temp; 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        while (((temp = con.is.read()) != -1) && (temp != eot)) { 
         baos.write(temp); 
        } 
        lblIncoming.setText(new String(baos.toByteArray())); 
        Thread.sleep(2000); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
    tIncoming.start(); 
} 

getDisconnectButton()方法でSocketConnectionオブジェクトは、このための適切な方法を持っていないようだと、私は、適切にサーバーから切断する方法がわかりません。

答えて

2

Input-またはOutputStreamのいずれかでclose()を呼び出すと、Socket.SocketInputStreamクラスlinkからSocket、コードを閉じます。

public void close() throws IOException { 
     closed = true; 
     if(Util.getImplementation().isSocketConnected(impl)) { 
      Util.getImplementation().disconnectSocket(impl); 
      con.setConnected(false); 
     } 
    } 

したがって、サーバーにclose命令を送信してから、ストリームを閉じます。

希望すると、

関連する問題