Codenameone关闭套接字

时间:2016-10-05 14:10:43

标签: codenameone

似乎没有办法断开套接字而不会在服务器端造成连接重置错误。

我在测试版应用中使用com.codename1.io.SocketConnectionprivate 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方法,我不知道如何正确地断开与服务器的连接,因为{{1}}对象似乎没有适当的方法。

1 个答案:

答案 0 :(得分:2)

如果在Input-或OutputStream上调用close(),则关闭Socket,代码来自Socket.SocketInputStream类link

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

首先向服务器发送关闭指令,然后关闭流。

希望这有帮助,