gpt4 book ai didi

java - 当我非常简单的聊天服务器退出时,试图找出关闭套接字连接的最佳方法

转载 作者:行者123 更新时间:2023-12-02 00:36:08 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的聊天服务器。我有一个问题,程序将在一个线程上运行,结束无限循环,接受端口 80 上的套接字连接。现在,如果我重新运行该程序,它会给我一个地址错误,即端口 80 未释放。
现在,服务器在一个单独的线程上运行,主线程只是使用 system.in.read 等待某个人按下 Enter 键。似乎在java中,当程序退出时,它不会自动关闭所有连接。我正在考虑让主线程发送一条服务器线程会读取的消息。它会告诉服务器线程关闭。有更好的方法吗?

代码公共(public)类 cServerThread 实现 Runnable {

Thread runner;
public cServerThread() {
}

public cServerThread(String threadName) {
runner = new Thread(this, threadName); // (1) Create a new thread.
System.out.println(runner.getName());
runner.start(); // (2) Start the thread.
}

public void run() {
//Display info about this particular thread
System.out.println(Thread.currentThread());
cServer mServer = new cServer();
mServer.run();

}

}

公共(public)类cServer{

boolean StopFlag;
String header;
Socket connection;
ServerSocket server;
StringBuffer request;
OutputStream out;



public boolean ReadSize(InputStream in)
{
// read in one line
try{
request = new StringBuffer(1000);
boolean f=true;
int s=43;
while( s-->0 )
{
int c=in.read();
char a=(char)c;
request.append(a);
} // end while

} catch(IOException ec)
{
System.out.println(ec.getMessage());
}

System.out.println("last line");
System.out.println(request);
int p=request.lastIndexOf("stop");
if (p!=-1)
StopFlag=true;

return true;
}


public boolean ReadLine(InputStream in)
{
// read in one line
try{
request = new StringBuffer(1000);
boolean f=true;
while(true)
{
int c=in.read();
if (c=='\r')
{
// next should be a \n
c=in.read();
if (f==true)
return false;
break;
}
f=false;
request.append((char)c);
} // end while

} catch(IOException ec)
{
System.out.println(ec.getMessage());
}

System.out.println(request);
int p=request.lastIndexOf("stop");
if (p!=-1)
StopFlag=true;

return true;
}


public void ReadHeader()
{
// read in one line
try{
request = new StringBuffer(1000);
System.out.println("get connection reading in data \r");
InputStream in = new BufferedInputStream( connection.getInputStream() );
StopFlag=false;
for(int i=0;i<11;i++)
ReadLine(in);
ReadSize(in);


} catch(IOException ec)
{
System.out.println(ec.getMessage());
}

}

public void SendReply()
{
// set up header
String content="test html ted\r\n cat2";

String header="HTTP/1.0 200 Ok\r\n"+
"Server: OneFile 1.0\r\n"+
"Content-length: "+content.length() +"\r\n"+
"Content=type: "+"text/html " +"\r\n\r\n";
// header=header.getBytes("ASCII");
byte[] bHeader;

try{
bHeader=header.getBytes("ASCII");

out.write( header.getBytes("ASCII") );
out.write(content.getBytes("ASCII"));
out.flush();



} catch(IOException ec)
{
System.out.println(ec.getMessage());
}

}

public int GetPort()
{
return 90;
}


public void run()
{
String content="test html ted2 \r\n fred";

// set up header
header="HTTP/1.0 200 Ok\r\n"+
"Server: OneFile 1.0\r\n"+
"Content-length: "+content.length() +"\r\n"+
"Content=type: "+"text/html " +"\r\n\r\n";
// header=header.getBytes("ASCII");


try{
connection = null;
server = new ServerSocket(87); // port 62

// Loop forever
while(true)
{
///////////////////////////////////////////////////
// get a new connection
///////////////////////////////////////////////////
System.out.println("Aceepting connections on port 86 \r");

try{
// Get New Connection
connection=server.accept();

// Create a buffer stream to connection
out=new BufferedOutputStream( connection.getOutputStream() );

// Read in the header
ReadHeader();

// Read in the message id
// Send the reply back
SendReply();

if (StopFlag)
{
System.out.println("shutting down \r");
connection.close();
break;
}
/*
byte[] bHeader;
bHeader=header.getBytes("ASCII");

out.write( header.getBytes("ASCII") );
out.write(content.getBytes("ASCII"));
out.flush();

int p=request.lastIndexOf("stop");
if (p!=-1)
{
System.out.println("shutting down \r");
connection.close();
break;
}
*/
} catch(IOException ec)
{
System.out.println(ec.getMessage());
}
finally{
if (connection != null )
connection.close();
}

} // end while

} catch(IOException ec)
{
System.out.println(ec.getMessage());
}

} // end run


} // end class

最佳答案

ServerSocket.setReuseAddress(true)。由于技术原因,使端口保持打开状态并处于 TIME_WAIT 状态两分钟的不是 Java,而是操作系统,特别是 TCP/IP 堆栈。

关于java - 当我非常简单的聊天服务器退出时,试图找出关闭套接字连接的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7780330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com