/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serversynchro;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Lolek
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
byte[] ip ={Network.ip(127),Network.ip(0),Network.ip(0),Network.ip(1)};
InetAddress klient1 = InetAddress.getByAddress(ip);
byte[] ip2 ={Network.ip(127),Network.ip(0),Network.ip(0),Network.ip(1)};
InetAddress klient2 = InetAddress.getByAddress(ip2);
Port1 nPort1 = new Port1();
Port2 nPort2 = new Port2();
Preposilani1 nPrepos1 = new Preposilani1();
Preposilani2 nPrepos2 = new Preposilani2();
Vlakno1 serverSocket1 = new Vlakno1();
Vlakno2 serverSocket2 = new Vlakno2();
System.out.println(nPort1.getData());
serverSocket2.vlak2.start();
serverSocket1.vlak1.start();
Thread.sleep(60000);
while(true)
{
DatagramPacket odesilanyPaket2 = new DatagramPacket(nPrepos2.getData(), nPrepos2.getData().length, klient1, nPort1.getData());
serverSocket2.server2.send(odesilanyPaket2);
DatagramPacket odesilanyPaket1 = new DatagramPacket(nPrepos1.getData(), nPrepos1.getData().length, klient2, nPort2.getData());
serverSocket1.server1.send(odesilanyPaket1);
}
}
catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class Vlakno2 implements Runnable
{
private Port2 cisloPort2;
private Preposilani2 prepos2;
volatile DatagramSocket server2;
Thread vlak2;
Vlakno2()
{
vlak2 = new Thread(this,"Vlakno 2");
}
public void run ()
{
try
{
server2 = new DatagramSocket(4000);
byte[] prijata = new byte[256];
byte[] odeslana2 = new byte[256];
byte[] ip ={Network.ip(192),Network.ip(168),Network.ip(1),Network.ip(101)};
int port2=0;
while(true)
{
DatagramPacket prijatyPaket2 = new DatagramPacket(prijata,prijata.length);
server2.receive(prijatyPaket2);
port2 = prijatyPaket2.getPort();
System.out.println(port2);
cisloPort2.setData(port2);
String prijaty = new String(prijatyPaket2.getData());
System.out.println("Od 2. klienta:"+prijaty);
odeslana2 = prijaty.getBytes();
prepos2.setData(odeslana2);
}
}
catch (IOException ex)
{
Logger.getLogger(Vlakno2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class Vlakno1 implements Runnable
{
private Port1 cisloPort1;
private Preposilani1 prepos1;
volatile DatagramSocket server1;
Thread vlak1;
Vlakno1()
{
vlak1 = new Thread(this,"Vlakno 1");
}
public void run ()
{
try
{
server1 = new DatagramSocket(5000);
byte[] prijata = new byte[256];
byte[] odeslana1 = new byte[256];
byte[] ip ={Network.ip(192),Network.ip(168),Network.ip(1),Network.ip(101)};
int port1;
while(true)
{
DatagramPacket prijatyPaket2 = new DatagramPacket(prijata,prijata.length);
server1.receive(prijatyPaket2);
port1 = prijatyPaket2.getPort();
System.out.println(port1);
cisloPort1.setData(port1);
// ipAdresa1 = prijatyPaket2.getAddress();
String prijaty = new String(prijatyPaket2.getData());
System.out.println("Od 1. klienta:"+prijaty);
odeslana1 = prijaty.getBytes();
prepos1.setData(odeslana1);
}
}
catch (IOException ex)
{
Logger.getLogger(Vlakno1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class Port1 {
private AtomicInteger portt1 = new AtomicInteger();
public synchronized void setData(int portt1){
this.portt1.incrementAndGet();
}
public synchronized int getData(){
return this.portt1.get();
}
}
class Port2 {
private AtomicInteger portt2 = new AtomicInteger();
public synchronized void setData(int portt2){
this.portt2.incrementAndGet();
}
public synchronized int getData(){
return this.portt2.get();
}
}
class Preposilani1 {
private byte[] odeslat1 = new byte[256];
public synchronized void setData(byte odeslat1[]){
this.odeslat1=odeslat1;
}
public synchronized byte[] getData(){
return this.odeslat1;
}
}
class Preposilani2 {
private byte[] odeslat2 = new byte[256];
public synchronized void setData(byte odeslat2[]){
this.odeslat2=odeslat2;
}
public synchronized byte[] getData(){
return this.odeslat2;
}
}