Hiho,
I just started writing my first Teaming Portlet and got some Problems with UDP Sockets...
Imho is Teaming a Great Collaboration Platform for Gamers/Clans/Guilds and so I wanna build some Usefull Gamer Portlets.
I wrote a Portlet to scan Quake3 Engine based Gameservers... The Code itself works fine if i run the Code as Java Application.
But when i use the same code in the Portlet view.jsp, than i only receive a "disconnect".
But i should receive a String with information about the Serversettings...
Is there something to keep in mind when you write UDP Socket Portlets?
Here my Code:
|
Code:
|
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects />
<%@ page import="java.io.*,java.net.*" %>
<%
InetAddress address;
DatagramSocket socket = null;
DatagramPacket packet;
byte[] buf = new byte[256];
try
{
socket = new DatagramSocket();
} catch (SocketException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
String initString = "˙˙˙˙getstatus";
buf=initString.getBytes();
address = InetAddress.getByName("85.214.40.44");
packet = new DatagramPacket(buf,buf.length,address,27960);//default port
try
{
socket.send(packet);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
byte[] buffer = new byte[40000];
DatagramPacket dPacket = new DatagramPacket(buffer,buffer.length);
String received = new String(dPacket.getData(), 0, dPacket.getLength());
//Save the Settings + playerlist in an Array. spit the received string at the "\";
String[] splitArray = received.split("\\\\");
for (int i = 1; i < splitArray.length; i++)
{
out.println("DEBUG: splitArray["+i+"]=" +splitArray[i]+"<br>");
if(i+1<splitArray.length-1)
{
if(splitArray[i].compareTo("sv_hostname")==0)
out.println("sv_hostname: "+splitArray[i+1]);
}
}
//in the last Key string is our playerList... now lest grep it out. we split the string at the "\n" and receive an array of player
//We see the Score, ping and Name, seperated by " ";
String[] playerList=splitArray[splitArray.length-1].split("\n");
//we start at 1, because at 0 is still an information about the server settings...
for (int i = 1; i < playerList.length; i++)
{
System.out.println(playerList[i]);
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
%> |
Thank you in advance for your time and help
Apologet