Java多线程Socket操作猜数游戏样例

1 服务器端程序,接受连接并处理客户端的请求

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();
}
}
2 客户端程序,发送每次猜的数字

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();
}
}
3 运行效果

TAG: java多线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值