Java套接字编程示例(猜拳游戏)


package appendix_b;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Demo2 {
public static void main(String[] args) {
try {
System.out.println("正在连接服务器。。。");

//创建Socket对象,链接本机端口8001
Socket socket = new Socket("127.0.0.1",8001);

//由系统设备构造 BufferedReader
InputStreamReader is1 = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(is1);

//由Socket对象得到输出流,并构造PrintWriter对象
PrintWriter sout = new PrintWriter(socket.getOutputStream(),true);



//由Socket对象得到输入流,并构造BufferedReader
InputStreamReader is2 =new InputStreamReader(socket.getInputStream());
BufferedReader sin = new BufferedReader(is2);

System.out.println("正确的链接了服务器");
System.out.println();

String msg;

//接收服务器端的游戏说明文字
msg = sin.readLine();
System.out.println(msg);
msg = sin.readLine();
System.out.println(msg);
msg = sin.readLine();
System.out.println(msg);

//从标准流中接受用户的输入
msg = bin.readLine();
//将用户的输入传给服务器
sout.println(msg);

//等待服务器的响应,并将结果输出
//接收服务器的选择
msg = sin.readLine();
System.out.println(msg);

//接受客户端的选择
msg = sin.readLine();
System.out.println(msg);

//接受游戏结果
msg = sin.readLine();
System.out.println(msg);

sout.close();
sin.close();
socket.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}

//////////////////////////////////////////////////////////////////////
package appendix_b;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class Demo3 {
public static void main(String[] args) {
try {
ServerSocket server = null;

//监听本机的端口
server = new ServerSocket(8001);
//对应客户端的socket
Socket socket = null;
System.out.println("等待客户端的连接。。。");
//接收客户端对8001端口的连接
socket = server.accept();
System.out.println("客户端正在连接!");

//获取socket的输入流
InputStreamReader is1 = new InputStreamReader(socket.getInputStream());
BufferedReader sin = new BufferedReader(is1);

//获取socket的输出流
PrintWriter sout = new PrintWriter(socket.getOutputStream(),true);

//获取标准输入流
InputStreamReader is2 = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(is2);

String msg;

//输出游戏的提示
sout.println("开始新的游戏,请选择你的出拳:");
sout.println("1,剪刀,2,石头,3,布");
sout.println("");

//接受客户端的输入
msg = sin.readLine();

//构造服务器的选择
Random r = new Random();
String tempmsg = null;

tempmsg = String.valueOf((r.nextInt(3)+1));

//判断服务器的选择
if(tempmsg.equals("1"))
{
sout.println("服务器选的是:剪刀");
}
if(tempmsg.equals("2"))
{
sout.println("服务器选的是:石头");
}
if(tempmsg.equals("3"))
{
sout.println("服务器选的是:布");
}

//判断客户端的选择
if(msg.equals("1"))
{
sout.println("你选的是:剪刀");
}
if(msg.equals("2"))
{
sout.println("你选的是:石头");
}
if(msg.equals("3"))
{
sout.println("你选的是:布");
}

//判断是谁胜
if((tempmsg.equals("1") && msg.equals("2"))||
(tempmsg.equals("2") && msg.equals("3"))||
(tempmsg.equals("3") && msg.equals("1")))
{
sout.print("你赢了!");
}else if(tempmsg.equals(msg))
{
sout.println("平局");
}
else
{
sout.println("你输了!");
}

//关闭对策
sout.close();
sin.close();
socket.close();
server.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//////////////////////////////////////////////////////////////////////
package appendix_b;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class Demo4 extends Thread{

Socket socket;

public Demo4(Socket socket1)
{
this.socket = socket1;
}

public static void main(String[] args) {
try {
ServerSocket server = null;

//监听本机的端口
server = new ServerSocket(8001);

int clientCount = 1;

//循环接受客户端的链接
while(true)
{
Socket socket = null;
System.out.println("等待着"+clientCount+"客户端的链接。。。");

//接收客户端对8001端口的连接
socket = server.accept();
System.out.println("客户端正确连接");

//启动一个新的线程来处理客户端
Demo4 thread = new Demo4(socket);
thread.start();
clientCount++;
}
} catch (Exception e) {
e.printStackTrace();
}
}

//运行游戏的方法
public void run()
{
try {

//获取socket的输入流
InputStreamReader is1 = new InputStreamReader(socket.getInputStream());
BufferedReader sin = new BufferedReader(is1);

//获取socket的输出流
PrintWriter sout = new PrintWriter(socket.getOutputStream(),true);



String msg;

//输出游戏的提示
sout.println("开始新的游戏,请选择你的出拳:");
sout.println("1,剪刀,2,石头,3,布");
sout.println("");

//接受客户端的输入
msg = sin.readLine();

//构造服务器的选择
Random r = new Random();
String tempmsg = null;

tempmsg = String.valueOf((r.nextInt(3)+1));


//判断服务器的选择
if(tempmsg.equals("1"))
{
sout.println("服务器选的是:剪刀");
}
if(tempmsg.equals("2"))
{
sout.println("服务器选的是:石头");
}
if(tempmsg.equals("3"))
{
sout.println("服务器选的是:布");
}

//判断客户端的选择
if(msg.equals("1"))
{
sout.println("你选的是:剪刀");
}
if(msg.equals("2"))
{
sout.println("你选的是:石头");
}
if(msg.equals("3"))
{
sout.println("你选的是:布");
}

//判断是谁胜
if((tempmsg.equals("1") && msg.equals("2"))||
(tempmsg.equals("2") && msg.equals("3"))||
(tempmsg.equals("3") && msg.equals("1")))
{
sout.print("你赢了!");
}else if(tempmsg.equals(msg))
{
sout.println("平局");
}
else
{
sout.println("你输了!");
}

//关闭对策
sout.close();
sin.close();
socket.close();

} catch (Exception e) {
e.printStackTrace();
}

}
}
//////////////////////////////////////////////////////////////////////

   这是一个简单的猜拳游戏,通过socket实现客户端与服务器的交互。

你可能感兴趣的:(java,游戏,编程,.net,socket)