私が得ている問題は、正しく接続できることですが、いまいましい接続は閉じられません!私は何をしなければなりませんか?
サーバ側 :
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が静的であることに関係していると思います。しかし、それらを非静的にし、それでもプログラムを実行させる方法がわかりません。