java socket

服务器启动的时候随机想到一个数字... 比如说是100;
当客户端连入了之后.定时发送一个数字过去...
客户端发送数字过去后服务器端计算...每个数字...
当累加后的值大于等于服务器想到的那个字后...告知所有客户端...
最后一个发送数字的客户端为胜利者

 

//客户端

package GameTest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Random;
import java.util.UUID;

public class GameClinet {
    private Socket socket;
    private DataInputStream in;
    private DataOutputStream out;

    public static void main(String[] args) {
        String clientName = "";
        clientName = UUID.randomUUID().toString();
        new GameClinet(clientName, "127.0.0.1", 8900);
    }

    public GameClinet(String clientName, String server, int port) {
        try {
            socket = new Socket(server, port);
            in = new DataInputStream(new BufferedInputStream(socket
                    .getInputStream()));
            out = new DataOutputStream(new BufferedOutputStream(socket
                    .getOutputStream()));
            out.writeUTF(clientName);
            out.flush();
            Random ran = new Random();
            while (true) {
                Thread.sleep(3000);
                String str = in.readUTF();
                if (str.equals("false")) {
                    System.exit(0);
                }
                System.out.println(str);
                out.writeUTF(ran.nextInt(10) + "");
                out.flush();

            }

        } catch (Exception e) {
            System.out.println("Server error");
            this.close();
            // System.exit(0);

        }
    }

    protected void send(String msg) {
        try {
            out.writeUTF(msg);
            out.flush();
        } catch (Exception e) {

        }
    }

    protected void close() {
        try {
            out.close();
            in.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

}

 

 

//服务器端

package GameTest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;

public class GameServer {
    int max = 0;
    boolean flag = true;
    int sum = 0;
    ServerSocket serverSocket;

    public void listen(int port) throws Exception {
        serverSocket = new ServerSocket(port);
        Random ran = new Random();
        if (max == 0) {
            max = ran.nextInt(100);
        }
        while (true) {
            System.out.println("等待客户端连接...");
            Socket socket = serverSocket.accept();
            DataInputStream in = new DataInputStream(socket.getInputStream());
            String clinetname = in.readUTF();
            System.out.println(clinetname + " is coming!");

            new ClientAgent(clinetname, socket, this).start();
            System.out.println("客户连接进来了.");
        }

    }

    public static void main(String[] args) throws Exception {
        new GameServer().listen(8900);
    }

}

class ClientAgent extends Thread {
    Socket socket;
    DataInputStream in;
    DataOutputStream out;
    GameServer server;
    String clinetname;
    protected static Vector clientlist = new Vector();

    public ClientAgent(String clinetname, Socket socket, GameServer server)
            throws IOException {
        this.socket = socket;
        this.server = server;
        this.clinetname = clinetname;
        in = new DataInputStream(new BufferedInputStream(socket
                .getInputStream()));
        out = new DataOutputStream(new BufferedOutputStream(socket
                .getOutputStream()));
    }

    @Override
    public void run() {
        clientlist.addElement(this);
        sendallclient("size:" + clientlist.size() + "");
        try {
            String clientStr;

            while (server.flag) {
                clientStr = in.readUTF();
                synchronized (in) {
                    server.sum += Integer.parseInt(clientStr);
                }
                sendallclient(clinetname + "/" + server.max + "/" + clientStr
                        + "/总和" + server.sum);
                if (server.sum > server.max && server.flag) {
                    sendallclient(clinetname + " is win!");
                    sendallclient("false");
                    server.max = 0;
                    server.flag = false;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("over");
        }
    }

    private void sendallclient(String msg) {
        synchronized (clientlist) {
            Enumeration all = clientlist.elements();
            while (all.hasMoreElements()) {
                ClientAgent clientAgent = (ClientAgent) all.nextElement();
                try {
                    clientAgent.out.writeUTF(msg);
                    clientAgent.out.flush();
                } catch (Exception e) {
                    clientAgent.interrupt();
                }
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值