package sockettest;
import java.net.*;
import javax.net.*;
import java.util.*;
import java.io.*;
/**
* <p>Überschrift: </p>
*
* <p>Beschreibung: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Organisation: None</p>
*
* @author Tobias Viehweger
* @version 1.0
*/
public class Server {
public static final String UDP_PACKET_RECIEVED = "UPDRECV";
private ServerSocket tcpConnection = null;
private DatagramSocket udpConnection = null;
private Vector clients = new Vector();
private UDPListener udpThread;
private TCPListener tcpThread;
public Server ()
{
try
{
//Zuerst die UDP-Verbindung erstellen
udpConnection = new DatagramSocket( 11834 );
//Dann die TCP Socket Verbindung erstellen
tcpConnection = new ServerSocket( 11833 );
//Thread, der auf Nachrichten auf dem UDP-Port wartet
udpThread = new UDPListener( udpConnection );
udpThread.start();
//Thread, der auf neue Clients wartet
tcpThread = new TCPListener( tcpConnection, clients );
tcpThread.start();
while (true)
{
if (udpThread.hasNewMessages())
{
byte [] arData = udpThread.getNextMessage();
String sIndex = new String( arData, 0, arData.length );
int index = -1;
try
{
index = Integer.parseInt( sIndex );
}
catch ( NumberFormatException ex1 )
{}
if (index > -1 && index < clients.size())
{
//Bescheid sagen, dass das UDP Packet ankam (Sonst muss der Client es erneut senden)
Socket sock = (Socket)clients.elementAt( index );
sock.getOutputStream().write( UDP_PACKET_RECIEVED.getBytes() );
//Dann Nachricht abfragen
new MessageReciever(sock).start();
}
}
else
{
try
{
Thread.sleep( 100 );
}
catch ( InterruptedException ex )
{
}
}
}
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
public static void main ( String[] args )
{
new Server();
}
}
class MessageReciever extends Thread
{
private Socket socket;
public MessageReciever(Socket sock)
{
socket = sock;
}
public void run()
{
try
{
BufferedInputStream in = new BufferedInputStream( socket.getInputStream() );
OutputStream out = socket.getOutputStream();
byte[] buf = new byte[1024 ];
int len;
while ( ( len = in.read( buf ) ) != -1 )
{
System.out.write( buf, 0, len);
out.write( buf, 0, len );
}
}
catch ( IOException ex )
{
}
}
}
class TCPListener extends Thread
{
private boolean listen;
private Vector clients;
private ServerSocket tcp;
public TCPListener ( ServerSocket sock, Vector cl )
{
clients = cl;
tcp = sock;
listen = true;
}
public void setListening ( boolean b )
{
listen = b;
}
public void run()
{
while (listen)
{
try
{
//Auf neue TCP Verbindungen warten
Socket next = tcp.accept();
//Test
System.out.println( "Neuer Socket-Client: " + next.getInetAddress() );
//Den Client in die Liste ablegen
clients.addElement( next );
//Dem Client seinen Platz in der Liste mitteilen
byte[] arData = Integer.toString( clients.indexOf( next ) ).getBytes();
next.getOutputStream().write( arData, 0, arData.length );
}
catch ( IOException ex )
{
}
}
}
}
class UDPListener extends Thread
{
private boolean listen;
private DatagramSocket udp;
private DatagramPacket packet;
private List messages;
public UDPListener (DatagramSocket con)
{
udp = con;
messages = Collections.synchronizedList(new LinkedList());
listen = true;
}
public void setListening(boolean b)
{
listen = b;
}
public boolean hasNewMessages()
{
return !messages.isEmpty();
}
public byte[] getNextMessage()
{
if (!hasNewMessages())
return null;
synchronized(messages)
{
return (byte[])messages.remove(0);
}
}
public void run()
{
while (listen)
{
try
{
//Paket erzeugen
packet = new DatagramPacket( new byte[256 ], 256 ); //256 Bytes sollten locker reichen
//Auf Nachricht warten
udp.receive( packet );
System.out.println( "Paket!" );
//Nur uebertragene Bytes speichern
byte [] tmpBuf = new byte[ packet.getLength() ];
System.arraycopy( packet.getData(), 0, tmpBuf, 0, tmpBuf.length );
//Zur Liste hinzufuegen
messages.add( tmpBuf );
}
catch ( IOException ex )
{
}
}
}
}