ServerApp.java
package test;
import java.io.*;
import java.net.*;
/** *//**
* 端程序
*
* @author luxuan310,修正赵学庆。
*
*/
public class ServerApp ...{
static final int PORT = 9000;
private ServerSocket serverSocket;
private Socket socket;
private BufferedReader netIn;
private PrintWriter netOut;
public ServerApp() throws IOException ...{
serverSocket = new ServerSocket(PORT);
System.out.println("server start");
while (true) ...{
// 等待连接
socket = serverSocket.accept();
ServerThread st = new ServerThread(socket);
new Thread(st).start();
}
}
class ServerThread implements Runnable ...{
private Socket socket;
private int randomNumber;
private int clientGuessNumber;
public ServerThread(Socket s) throws IOException ...{
socket = s;
netIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
netOut = new PrintWriter(socket.getOutputStream());
}
public void run() ...{
System.out.println("client" + socket.getInetAddress() + " is connect");
randomNumber = (int) (Math.random() * 100);
System.out.println("random number is:" + randomNumber);
try ...{
clientGuessNumber = Integer.parseInt(netIn.readLine());
System.out.println("client guess number is:" + clientGuessNumber);
for (int i = 3; i > 0; i--) ...{
if (clientGuessNumber == randomNumber) ...{
netOut.println("OK,恭喜,猜对了");
ClientApp.finished = true;
} else if (clientGuessNumber < randomNumber) ...{
netOut.println("您猜的数小了,您还有" + (i - 1) + "次机会");
ClientApp.finished = false;
} else if (clientGuessNumber > randomNumber) ...{
netOut.println("您猜的数大了,您还有" + (i - 1) + "次机会");
ClientApp.finished = false;
}
netOut.flush();
if (!ClientApp.finished) ...{
clientGuessNumber = Integer.parseInt(netIn.readLine());
} else ...{
break;
}
}
if (!ClientApp.finished) ...{
netOut.println("OK,您没有机会了,游戏结束");
}
ClientApp.finished = true;
} catch (IOException e) ...{
} finally ...{
try ...{
netOut.close();
netIn.close();
socket.close();
} catch (IOException ee) ...{
}
}
}
}
public static void main(String[] args) throws IOException ...{
new ServerApp();
}
}
--------------------next---------------------
package test;
import java.io.*;
import java.net.*;
public class ClientApp ...{
private Socket socket;
private BufferedReader netIn;
private PrintWriter netOut;
private BufferedReader keyboardIn;
static Boolean finished = false;
public ClientApp() throws IOException ...{
System.out.println("请输入服务器地址,连接本地服务器请输入localhost");
keyboardIn = new BufferedReader(new InputStreamReader(System.in));
try ...{
if (keyboardIn.readLine().equalsIgnoreCase("localhost")) ...{
socket = new Socket(InetAddress.getLocalHost(), ServerApp.PORT);
} else ...{
socket = new Socket(InetAddress.getByName(keyboardIn.readLine()), ServerApp.PORT);
}
netIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
netOut = new PrintWriter(socket.getOutputStream());
} catch (UnknownHostException e) ...{
System.err.println("连接不到服务器");
System.exit(-1);
}
System.out.println("连接成功");
while (!finished) ...{
System.out.println("请输入0-100之间的数字");
netOut.println(keyboardIn.readLine());
netOut.flush();
String str = netIn.readLine();
if (str == null) ...{
finished = true;
break;
}
System.out.println(str);
if (str.startsWith("OK")) ...{
finished = true;
break;
}
}
netIn.close();
netOut.close();
keyboardIn.close();
socket.close();
}
public static void main(String[] args) throws IOException ...{
new ClientApp();
}
}
--------------------next---------------------
阅读(282) | 评论(0) | 转发(0) |