Skip to content

Instantly share code, notes, and snippets.

@komang4130
Created December 14, 2017 16:46
Show Gist options
  • Save komang4130/db79bd1940545b47317eb6afca78fd2b to your computer and use it in GitHub Desktop.
Save komang4130/db79bd1940545b47317eb6afca78fd2b to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package UDP_Cau3;
import UDP_MulThread_guess.UDP_thread_server;
import static UDP_MulThread_guess.UDP_thread_server.check;
import static UDP_MulThread_guess.UDP_thread_server.receivePacket;
import static UDP_MulThread_guess.UDP_thread_server.sendPacket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author koman
*/
public class server {
public static void sendPacket(String mess , DatagramSocket ds , InetAddress addr , int port) throws IOException
{
byte m[] = mess.getBytes();
DatagramPacket dp = new DatagramPacket(m,m.length,addr,port);
ds.send(dp);
}
public static DatagramPacket receivePacket(DatagramSocket ds) throws IOException
{
byte m[] = new byte[256];
DatagramPacket dp = new DatagramPacket(m,m.length);
ds.receive(dp);
return dp;
}
public static int checkPort(int port[] , DatagramPacket dp)
{
for( int i = 0 ; i < port.length ; i++ )
if ( port[i] == dp.getPort() )
return 1;
return 0;
}
static int port[] = new int[100];
static int time_win[] = new int[100];
static int winner_id = 0;
static int magic[] = new int[100];
static int stt;
public static class Handler extends Thread
{
DatagramSocket ds;
DatagramPacket dp;
int number;
int stt;
public Handler(int number,DatagramSocket ds , DatagramPacket dp)
{
this.ds = ds;
this.dp = dp;
this.number = number;
}
@Override
public void run()
{
if ( winner_id != 0 )
{
try {
sendPacket("Sorry but there is a winner, his id " + winner_id,ds,dp.getAddress(),dp.getPort());
} catch (IOException ex) {
Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
}
}
else
{
try {
int id = 0;
for ( int i = 0 ; i < port.length ; i++ )
if( port[i] == dp.getPort() )
id = i;
if ( number > magic[id] )
sendPacket("Bigger than my magic ",ds,dp.getAddress(),dp.getPort());
else if ( number < magic[id] )
sendPacket("Smaller than my magic ",ds,dp.getAddress(),dp.getPort());
else
{
try {
time_win[id]++;
if ( time_win[id] == 5)
{
try {
sendPacket("Congratz, You are the winner!",ds,dp.getAddress(),dp.getPort());
winner_id = id;
for ( int i = 0 ; i < port.length ; i++)
{
if ( port[i] != dp.getPort() )
{
sendPacket("The winner is id " + winner_id,ds,dp.getAddress(),port[i]);
}
}
System.exit(0);
} catch (IOException ex) {
Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
}
}
sendPacket("Congratz, you need to win " + (5-time_win[id]) + " round more.",ds,dp.getAddress(),dp.getPort());
magic[id] = (int)(Math.random()*100 + 1);
System.out.println("Client " + id + " magic is " + magic[id]);
} catch (IOException ex) {
Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (IOException ex) {
Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void main(String[] args) throws SocketException, IOException
{
DatagramSocket ds = new DatagramSocket(1338);
stt = 0;
System.out.println("I'm running, Master");
while(true)
{
DatagramPacket dp = receivePacket(ds);
if (check(port,dp) == 0)
{
System.out.println("Client " + stt + " just connected");
magic[stt] = (int)(Math.random()*100 + 1);
System.out.println("Client " + stt + " magic is " + magic[stt]);
time_win[stt] = 0;
port[stt] = dp.getPort();
stt++;
}
else
{
int number = Integer.parseInt(new String(dp.getData()).trim());
Handler hd = new Handler(number,ds,dp);
hd.start();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment