如何把服务端监听端口事件放在线程不断地去监听是否有客户端连接呢?
写了一些,出现了一些错误,请指教~下面是我【服务端】代码:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class GuessServer implements Runnable {
// =服务器连接
public static ServerSocket serverSocket;
// =连接
public static Socket socket;
// =端口
public static final int PORT = 8888; public static void main(String[] args) {
// TODO Auto-generated method stub
DataInputStream dis = null;
DataOutputStream dos = null; try {
serverSocket = new ServerSocket(PORT);
System.out.println("正在等待客户端连接....");
// =这里处于等待状态,如果没有客户端连接,不向下执行
socket = serverSocket.accept();
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
// =读取数据
String clientString = dis.readUTF();
// =写出数据
dos.writeUTF(clientString);
System.out.println("===客户端已经成功连接====");
// =得到客户端的IP
System.out.println("客户端的IP:" + socket.getInetAddress());
// =得到客户端的端口号
System.out.println("客户端的端口号:" + socket.getPort());
// ==得到本地端口号
System.out.println("本地服务器(localhost)端口号:" + socket.getLocalPort());
System.out.println("---------------------------------");
System.out.println("客户端:" + clientString);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
} @Override
public void run() {
// TODO Auto-generated method stub

}
}AndroidActivity【客户端】代码:
public class GuessClientActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button btnOk;
private EditText edit;
private TextView tv;
// =Socket用于连接服务器获取输入输出流
private Socket socket;
// =服务器server IP地址
private final String ADDRESS = "10.0.2.2";
// =服务器端口
private final int PORT = 8888; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// =设置全屏、去除状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// =实例化组件
btnOk = (Button) findViewById(R.id.btn1);
tv = (TextView) findViewById(R.id.tv1);
edit = (EditText) findViewById(R.id.et1);
// =绑定按钮
btnOk.setOnClickListener(this);
} public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btnOk) {
DataInputStream dis = null;
DataOutputStream dos = null; try {
// =阻塞函数,正常连接后才会向下执行
socket = new Socket(ADDRESS, PORT);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
// ==向服务器写数据
dos.writeUTF(edit.getText().toString());
String temp = "client:";
temp += edit.getText().toString();
temp += "\n";
temp += "Server:";
// ==读取服务器发来的数据
temp += dis.readUTF();
tv.setText(temp);
} catch (IOException e) {
// TODO: handle exception
Log.e("tee", "Stream error");
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
}