我遇到的问题是我能够正确连接,但该死的连接不会关闭!我该怎么办?
服务器端:
Listen.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);
}
}
}
Landing.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是静态的有关。但是,我不知道如何使它们非静态并仍然使程序运行!
答案 0 :(得分:0)
isClosed()
告诉您您是否关闭了套接字。它没有告诉您有关连接状态的任何信息。如果对等方关闭套接字,您将在阅读时获得EOF指示:
read()
返回-1 readLine()
返回null readXXX()
会针对任何其他XXX投放EOFException
。由于您的服务器没有从客户端读取任何内容,因此无法发现客户端何时关闭连接。