1

我遇到的问题是我能够正确连接,但是该死的连接不会关闭!我必须做什么?

服务器端 :

听.java

package com.example.ServerSide;


import java.net.ServerSocket;

public class Listen extends servVars {

    public static void main(String[] args) {
        try {
            System.out.println("HOSTED");
            hostAt = new ServerSocket(2051);
            conDev = hostAt.accept();
            System.out.println(conDev.getInetAddress()+":"+conDev.getPort());
            for(;;) {
                System.out.print(".");
                Thread.sleep(1000);
                if(conDev.isClosed())
                    break;
            }
        } catch(Exception E) {

        }
    }

}

servVars.java

package com.example.ServerSide;

import java.net.ServerSocket;
import java.net.Socket;

public class servVars {
    static ServerSocket hostAt;
    static Socket conDev;
}

客户端 :

*Connect_to_server.java*

package com.example.clientside;

import java.net.Socket;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Connect_to_server extends Activity implements View.OnClickListener {

    private EditText IP_field;
    private EditText port_field;
    private String IP;
    private String portStr;
    private int port;
    public static Socket conn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect_to_server);
        IP_field = (EditText) this.findViewById(R.id.IP_field);
        port_field = (EditText) this.findViewById(R.id.port_field);
        Button connect = (Button) this.findViewById(R.id.connect_button);
        connect.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.connect_to_server, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        IP = IP_field.getText().toString();
        portStr = port_field.getText().toString();
        Toast.makeText(getApplicationContext(),"Trying to connect to "+IP+":"+portStr,Toast.LENGTH_SHORT).show();
        try {
            port = Integer.parseInt(portStr);
            conn = new Socket(IP, port);
            Intent landing = new Intent(this, Landing.class);
            startActivity(landing);
        } catch(Exception E) {
            Intent failure = new Intent(this, ConnectFail.class);
            failure.putExtra("ctsException", E.toString());
            startActivity(failure);
        }
    }

}

登陆.java

package com.example.clientside;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Landing extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.landing);
        Button disconnect = (Button) this.findViewById(R.id.disconnect_button);
        disconnect.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.landing, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        try {
            Connect_to_server.conn.close();
            Toast.makeText(getApplicationContext(),"Disconnected!",Toast.LENGTH_SHORT).show();
        } catch(Exception E) {}
    }

}

我没有发布布局和 ConnectFail.java,因为我认为这不是必需的。

编辑:我猜这与 Socket 和 ServerSocket 是静态的有关。但是,我不知道如何使它们成为非静态的并且仍然使程序运行!

4

1 回答 1

0

isClosed()告诉你是否关闭套接字。它不会告诉您任何有关连接状态的信息。如果对等方关闭套接字,您将在读取时收到 EOF 指示:

  • read()返回 -1
  • readLine()返回空
  • readXXX()抛出EOFException任何其他 XXX。

由于您的服务器没有从客户端读取任何内容,因此它不可能发现客户端何时关闭连接。

于 2013-01-18T04:53:53.560 回答