java socket小游戏_[ Java ] Socket 实现猜数字小游戏!!!

前言记一次作业

运行截图Server

Client

完整代码Server

import java.io.IOException;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Random;

import java.util.Scanner;

public class ServerDemo {

private int flag;

public void server() throws IOException {

//创建一个服务器

System.out.println("服务端启动,等待客户端连接。。。");

PrintWriter out = null;

Scanner getClient = null;

ServerSocket server = null;

Socket client = null;

this.setFlag();

try {

server = new ServerSocket(6666);

//创建一个接收连接客户端的对象

client = server.accept();

System.out.println(client.getInetAddress() + " 已成功连接到此台服务器上。");

//字符输出流

out = new PrintWriter(client.getOutputStream()); //向客户端发送数据

out.println("欢迎来到猜数字小游戏(1-100)!!!");

out.flush();//将缓冲流中的数据全部输出

getClient = new Scanner(client.getInputStream()); //接收客户端发送的数据

//阻塞等待客户端发送消息过来

while (getClient.hasNextLine()) {

String returnMsg = null;

String tmp = getClient.nextLine();

if ("e".equals(tmp)) {

break;

} else if ("c".equals(tmp)) {

returnMsg = "欢迎来到新一轮的猜数字小游戏(1-100)!!!";

this.setFlag();

} else {

int clientInput = Integer.parseInt(tmp);

if (clientInput > flag) {

returnMsg = "你输入的数字大了!!!";

} else if (clientInput < flag) {

returnMsg = "你输入的数字小了!!!";

} else {

returnMsg = "恭喜你,猜中了!!!继续(输入c),退出(输入e)";

}

}

out.println(returnMsg); //向客户端发送数据

out.flush();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

out.close();

getClient.close();

server.close();

}

}

// 生成随机数字

public void setFlag() {

Random r = new Random();

flag = r.nextInt(100);

System.out.println("猜数字小游戏答案:flag = " + flag);

}

public static void main(String[] args) throws IOException {

new ServerDemo().server();

}

}Client

import java.io.IOException;

import java.io.PrintWriter;

import java.net.Socket;

import java.util.Scanner;

public class ClientDemo {

public void client() throws IOException {

System.out.println("正在向服务器请求连接。。。");

Socket client = null;

Scanner getKey = null;

Scanner getServer = null;

PrintWriter out = null;

try {

client = new Socket("127.0.0.1", 6666);

getServer = new Scanner(client.getInputStream());

System.out.println(getServer.nextLine());

out = new PrintWriter(client.getOutputStream());

System.out.print("请输入数字:");

//先读取键盘录入方可向服务端发送消息

getKey = new Scanner(System.in);

while (getKey.hasNextLine()) {

//写到服务端的的控制台

out.println(getKey.nextLine());

out.flush();

try {

System.out.println("提示消息:" + getServer.nextLine());

System.out.print("请输入数字:");

} catch (Exception e) {

System.out.print("游戏结束!!!");

break;

}

//阻塞等待接收服务端的消息

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

getKey.close();

getServer.close();

out.close();

client.close();

}

}

public static void main(String[] args) throws IOException {

new ClientDemo().client();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值