I’m trying to make a simple chat server. I had a siisue where the program would run on one thread end a endless loop accepting a socket connection on port 80. Now if I re ran the program, it would give me a address error, ie port 80 was not freed.
Now the server runs on a separate thread, and the main thread just waits for some one to press the enter key by using a system.in.read. It seems like in java when a program exits, it does not automcly close all the connections.
I was thinking about having the main thread send a message that the server thread would read. It would tell the server thread to shut down.
Is there a better way ro do this?
code
public class cServerThread implements 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 class 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). It isn’t Java but the operating system, specifically the TCP/IP stack, that keeps the port open, for two minutes in TIME_WAIT state, for technical reasons.