Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- public class server {
- public static void main(String[] args) {
- int MAX = 1;
- char[] moves = {'s','c','f'};
- DatagramSocket s;
- byte[] buffer;
- DatagramPacket pkt;
- DatagramPacket tosend;
- char clientMove;
- char move;
- int cw = 0;
- int cp = 0;
- int exit = 0;
- try {
- s = new DatagramSocket(12345);
- buffer = new byte[MAX];
- pkt = new DatagramPacket(buffer, MAX);
- System.out.println("Server: " + s.getLocalAddress() + ":" + s.getLocalPort());
- while(true) {
- s.receive(pkt);
- clientMove = (new String(buffer,0,MAX)).charAt(0);
- if(clientMove == 'p') {
- InetAddress ia = pkt.getAddress();
- int port = pkt.getPort();
- System.out.println("** Now playing with " + ia.getHostAddress() + ":" + port + " **");
- cp++;
- // "k"
- tosend = new DatagramPacket("k".getBytes(), 1, ia, port);
- s.send(tosend);
- while(exit == 0) {
- s.receive(pkt);
- clientMove = (new String(buffer,0,MAX)).charAt(0);
- if(clientMove == 'y') {
- cw++;
- move = 'b';
- exit = 1;
- }
- else if(clientMove == 'i') {
- move = 'b';
- exit = 1;
- }
- else {
- move = doMove(moves);
- System.out.println("S: " + move + " --- C: " + clientMove);
- }
- tosend = new DatagramPacket((move + "").getBytes(), 1, ia, port);
- s.send(tosend);
- }
- System.out.println("** Won " + cw + " matches out of " + cp + " **")
- exit = 0;
- }
- }
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static char doMove(char[] arr) {
- return arr[(int)(System.currentTimeMillis() % arr.length)];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement