argh, as i probably shouldnt just use yours, i continued making my own (although yours was useful

)
but the guy on the sun forum helped me right up to the point of my original question and refuses to help me with that ¬_¬.. i hate that forum
heres my non-working code so far, i cant find my last working code but im just a little stuck with making it so that if any user types the kill server command (/kill) it shuts down the server (synchronised variables or something) and how to loop it so that more than one user can connect at once
i put //--------------- around the broken code and he wouldnt tell me what to put where // ... is out of my original code
on a sidenote: for some reason if someone sets up a socket to the server on the client side without doing anything else, it crashes the server, but id like to get multiple users able to connect before i look into this
Code:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.SocketException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String args[])
{
Server server = new Server(9001);
server.execute();
}
//----------------------
private boolean blnKeepAlive = true;
protected synchornized boolean isKeepAlive()
{
return blnKeepAlive;
}
protected synchronized void setKeepAlive(boolean blnKeepAlive)
{
this.blnKeepAlive = blnKeepAlive;
}
public void execute()
{
// ...
// Inside your method, where you loop the server accepting
while( isKeepAlive() )
{
// ...
// Store the created threads, so you can stop them afterwards
list.add(echoThread);
}
// Before you close your server, you must allow every thread to stop graciously
for (Iterator iter = list.iterator(); iter.hasNext();)
{
Thread echoerThread = (Thread) iter.next();
try
{
echoerThread.interrupt();
echoerThread.join(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
//----------------------
public Server(int intPort)
{
//int intPort = 9001;
ServerSocket objServerSocket;
Socket objSocket;
try
{
//---Start listening on port---
objServerSocket = new ServerSocket(intPort);
objSocket = objServerSocket.accept();
//---Make a new thread for each connection---
Thread echoThread = new Thread(new Echoer(objSocket));
echoThread.start();
//---Clean Up---
objServerSocket.close();
}
catch(BindException be)
{
System.out.println("BindException: \n\t" + "Port " + intPort + " already in use");
}
catch(SocketException se)
{
System.out.println("SocketException: Client has disconnected");
}
catch(Exception e)
{
System.out.println("Exception: \n\t" + e);
}
}
private class Echoer implements Runnable
{
String data;
BufferedReader objInBuffer;
PrintWriter objOutStream;
public Echoer(Socket objSocket)
{
try
{
//---Set up streams---
this.objInBuffer = new BufferedReader(new InputStreamReader(objSocket.getInputStream()));
this.objOutStream = new PrintWriter(objSocket.getOutputStream(), true);
}
catch(IOException ioe)
{
System.out.println("IOException:\n\t" + ioe);
}
}
public void run()
{
System.out.println("Client has connected to server!");
//---Receive Data---
while(true)
{
try
{
data = objInBuffer.readLine();
}
catch(IOException ioe)
{
System.out.println("IOException:\n\t" + ioe);
System.exit(0);
}
if(data.startsWith("/"))
{
//---Respond to Command---
if(data.toLowerCase().equals("/kill"))
{
objOutStream.println("*KILLING SERVER*");
objOutStream.flush();
setKeepAlive(false)
break;
}
}
else
{
//---Echo clients data back---
System.out.println("Echoing string: '" + data + "'");
objOutStream.println(data);
objOutStream.flush();
}
}
//---Clean Up---
try
{
objOutStream.close();
objInBuffer.close();
}
catch(IOException ioe)
{
System.out.println("IOException:\n\t" + ioe);
}
}
}
}