/******************************************************/
******************** listenserve.java ************************************
import java.io.*;
import java.lang.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;// 主程序一直处于监听状态,有连接则启动一个线程进行处理,以实现多个客户端public class listenserve {
private ServerSocket ss;
private boolean listening = true;
public listenserve() {
Init(); // 初始化
lisn(); // 启动监听
} public void Init() {
try {
ss = new ServerSocket(10015, 10);
} catch (IOException ie) {
System.out.println("无法在10015端口监听");
ie.printStackTrace();
}
} public void lisn() {
try {
while (listening) {
new Thread(new dialogserve(ss.accept())).start();
}
} catch (IOException ie) {
ie.printStackTrace();
}
} public static void main(String args[]) {
new listenserve();
}
}
/******************************************************/
******************** dialogserve.java ************************************
import java.io.*;
import java.lang.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class dialogserve implements Runnable {
private Socket s;
private InputStream in;
private String rev, temp;
private byte b[];
private int len;
public dialogserve(Socket ss) {
s = ss;
b = new byte[1024];
try {
in = s.getInputStream();
} catch (IOException ie) {
ie.printStackTrace();
}
rev = "";
} public void run() {
try {
while (s.isConnected() == true) {
if ((len = in.read(b)) != -1) {
temp = new String(b, 0, len);
rev += temp;
System.out.println(rev);
temp = null;
//Thread.sleep(1000);
}
}
in.close();
System.out.println("会话socket已断开!");
s.close();
} catch (SocketException se) {
System.out.println("客户端已断开!");
System.exit(0);
} catch (IOException io) {
io.printStackTrace();
System.exit(0);
}
/*
catch (InterruptedException ire) {
ire.printStackTrace();
}
*/
}
}
/**********************************************************/
******************** TcpClient.java ************************************
import java.net.*;
import java.io.*;public class TcpClient implements Runnable
{
private Socket socket;
private OutputStream out;
private String send;
public void run()
{
try
{
socket = new Socket();
//socket.setSoTimeout(10000);
socket.connect(new InetSocketAddress("localhost", 10015),10000);
out = socket.getOutputStream();
send = Thread.currentThread().getName(); out.write(send.getBytes());
out.flush();
out.close();
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
for (int i=0; i< 10 ; i++ )
{
(new Thread(new TcpClient())).start();
}
}
}
/************************************************************************************/问题,TcpClient 运行完后,发现程序中所用的 10个连接并没有并关闭,
服务器端的 处于 close_wait状态 , 客户端的处于 fin_wait 2 状态。请教: 如何保证 TCP连接的关闭?
******************** listenserve.java ************************************
import java.io.*;
import java.lang.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;// 主程序一直处于监听状态,有连接则启动一个线程进行处理,以实现多个客户端public class listenserve {
private ServerSocket ss;
private boolean listening = true;
public listenserve() {
Init(); // 初始化
lisn(); // 启动监听
} public void Init() {
try {
ss = new ServerSocket(10015, 10);
} catch (IOException ie) {
System.out.println("无法在10015端口监听");
ie.printStackTrace();
}
} public void lisn() {
try {
while (listening) {
new Thread(new dialogserve(ss.accept())).start();
}
} catch (IOException ie) {
ie.printStackTrace();
}
} public static void main(String args[]) {
new listenserve();
}
}
/******************************************************/
******************** dialogserve.java ************************************
import java.io.*;
import java.lang.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class dialogserve implements Runnable {
private Socket s;
private InputStream in;
private String rev, temp;
private byte b[];
private int len;
public dialogserve(Socket ss) {
s = ss;
b = new byte[1024];
try {
in = s.getInputStream();
} catch (IOException ie) {
ie.printStackTrace();
}
rev = "";
} public void run() {
try {
while (s.isConnected() == true) {
if ((len = in.read(b)) != -1) {
temp = new String(b, 0, len);
rev += temp;
System.out.println(rev);
temp = null;
//Thread.sleep(1000);
}
}
in.close();
System.out.println("会话socket已断开!");
s.close();
} catch (SocketException se) {
System.out.println("客户端已断开!");
System.exit(0);
} catch (IOException io) {
io.printStackTrace();
System.exit(0);
}
/*
catch (InterruptedException ire) {
ire.printStackTrace();
}
*/
}
}
/**********************************************************/
******************** TcpClient.java ************************************
import java.net.*;
import java.io.*;public class TcpClient implements Runnable
{
private Socket socket;
private OutputStream out;
private String send;
public void run()
{
try
{
socket = new Socket();
//socket.setSoTimeout(10000);
socket.connect(new InetSocketAddress("localhost", 10015),10000);
out = socket.getOutputStream();
send = Thread.currentThread().getName(); out.write(send.getBytes());
out.flush();
out.close();
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
for (int i=0; i< 10 ; i++ )
{
(new Thread(new TcpClient())).start();
}
}
}
/************************************************************************************/问题,TcpClient 运行完后,发现程序中所用的 10个连接并没有并关闭,
服务器端的 处于 close_wait状态 , 客户端的处于 fin_wait 2 状态。请教: 如何保证 TCP连接的关闭?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货