ServerApp
package test;
import java
import java
/** *//**
* 服務器端程序
*
* @author luxuan
*
*/
public class ServerApp
static final int PORT =
private ServerSocket serverSocket;
private Socket socket;
private BufferedReader netIn;
private PrintWriter netOut;
public ServerApp() throws IOException
serverSocket = new ServerSocket(PORT); xxxxcom
System
while (true)
// 等待連接
socket = serverSocket
ServerThread st = new ServerThread(socket);
new Thread(st)
}
}
class ServerThread implements Runnable
private Socket socket;
private int randomNumber;
private int clientGuessNumber;
public ServerThread(Socket s) throws IOException
socket = s;
xxxcom
netIn = new BufferedReader(new InputStreamReader(socket
netOut = new PrintWriter(socket
}
public void run()
System
randomNumber = (int) (Math
System
try
clientGuessNumber = Integer
System
xxxx
for (int i =
if (clientGuessNumber == randomNumber)
netOut
ClientApp
} else if (clientGuessNumber < randomNumber)
netOut
ClientApp
} else if (clientGuessNumber > randomNumber)
netOut
ClientApp
}
netOut
if (!ClientApp
clientGuessNumber = Integer
} else
break;
}
} xxxx
if (!ClientApp
netOut
}
ClientApp
} catch (IOException e)
} finally
try
netOut
netIn
socket
} catch (IOException ee)
}
}
}
}
public static void main(String[] args) throws IOException
new ServerApp();
}
}
[
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27731.html