Advertisement
aroblu94

udp_server

Mar 1st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class server {
  5.     public static void main(String[] args) {
  6.         int MAX = 1;
  7.         char[] moves = {'s','c','f'};
  8.         DatagramSocket s;
  9.         byte[] buffer;
  10.         DatagramPacket pkt;
  11.         DatagramPacket tosend;
  12.         char clientMove;
  13.         char move;
  14.         int cw = 0;
  15.         int cp = 0;
  16.         int exit = 0;
  17.  
  18.         try {
  19.             s = new DatagramSocket(12345);
  20.             buffer = new byte[MAX];
  21.             pkt = new DatagramPacket(buffer, MAX);
  22.  
  23.             System.out.println("Server: " + s.getLocalAddress() + ":" + s.getLocalPort());
  24.             while(true) {
  25.                 s.receive(pkt);
  26.                 clientMove = (new String(buffer,0,MAX)).charAt(0);
  27.                 if(clientMove == 'p') {
  28.                     InetAddress ia = pkt.getAddress();
  29.                     int port = pkt.getPort();
  30.                     System.out.println("** Now playing with " + ia.getHostAddress() + ":" + port + " **");
  31.                     cp++;
  32.                     // "k"
  33.                     tosend = new DatagramPacket("k".getBytes(), 1, ia, port);
  34.                     s.send(tosend);
  35.                     while(exit == 0) {
  36.                         s.receive(pkt);
  37.                         clientMove = (new String(buffer,0,MAX)).charAt(0);
  38.  
  39.                         if(clientMove == 'y') {
  40.                             cw++;
  41.                             move = 'b';
  42.                             exit = 1;
  43.                         }
  44.                         else if(clientMove == 'i') {
  45.                             move = 'b';
  46.                             exit = 1;
  47.                         }
  48.                         else {
  49.                             move = doMove(moves);
  50.                             System.out.println("S: " + move + " --- C: " + clientMove);
  51.                         }
  52.  
  53.                         tosend = new DatagramPacket((move + "").getBytes(), 1, ia, port);
  54.                         s.send(tosend);
  55.                     }
  56.                     System.out.println("** Won " + cw + " matches out of " + cp + " **")
  57.                     exit = 0;
  58.                 }
  59.             }
  60.         }
  61.         catch(Exception e) {
  62.             e.printStackTrace();
  63.         }
  64.  
  65.     }
  66.  
  67.     public static char doMove(char[] arr) {
  68.         return arr[(int)(System.currentTimeMillis() % arr.length)];
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement