ally
07-11-2007, 09:17
...ciao...
...ho "rubato" un esempio di socket server da un noto programmatore italiano...ho adattato il codice alle mie esigenze e l'ho aggiunto ad un precedente programma in modo da poter monitorare tramite lan il funzionamento delle stesse...ho scelto come porta di comunicazione la 12000...la connessione verso il server avviene senza problemi ma le risposte che questo dovrebbe ritornare non vengono inoltrate...
public class SocketServer {
int port;
BufferedReader socketIn = null;
PrintWriter socketOut = null;
ServerSocket socketServer = null;
Socket sock = null;
SocketAddress socketAddress = null;
String line = ""; //$NON-NLS-1$
static Logger AppLogger = Logger.getLogger("AppLogger"); //$NON-NLS-1$
Player player;
public SocketServer(){
AppLogger.debug(""); //$NON-NLS-1$
}
public SocketServer(int port){
System.out.println("server start on port : " + port); //$NON-NLS-1$
this.port = port;
}
public void go(){
new Thread(){
public void run(){
try{
SocketServer.this.socketServer = new ServerSocket(SocketServer.this.port);
SocketServer.this.sock = SocketServer.this.socketServer.accept();
SocketServer.this.socketAddress = SocketServer.this.sock.getRemoteSocketAddress ();
System.out.println ("Received connection from " + SocketServer.this.socketAddress); //$NON-NLS-1$
try{
SocketServer.this.socketIn = new BufferedReader (new InputStreamReader (SocketServer.this.sock.getInputStream ()));
SocketServer.this.socketOut = new PrintWriter (SocketServer.this.sock.getOutputStream (), true);
while ((SocketServer.this.line = SocketServer.this.socketIn.readLine ()) != null)
{
if (SocketServer.this.line.equals ("quit")) //$NON-NLS-1$
break;
request(SocketServer.this.line);
}
}
finally
{
if (SocketServer.this.socketIn != null)
SocketServer.this.socketIn.close ();
if (SocketServer.this.socketOut != null)
SocketServer.this.socketOut.close ();
SocketServer.this.sock.close ();
}
}
catch (Exception e)
{
AppLogger.debug(e.getMessage ());
}
}
}.start();
}
public void setPlayer(Player player){
this.player = player;
}
public void request(String request){
if(request.equals("recent")) //$NON-NLS-1$
this.socketOut.println(this.player.getRecentBarcode());
else if(request.equals("cycle")) //$NON-NLS-1$
this.socketOut.println("global cycle : "+this.player.isPlayer()+" audio cycle : "+this.player.isAudio()+" video cycle : "+this.player.isVideo()+" idle cycle : "+this.player.isIdle()); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
else if(request.equals("timer")) //$NON-NLS-1$
this.socketOut.println("idle counter : "+this.player.getCounter()/10+" of "+this.player.getGlobalVariables().getIdleInterval()/10+" restart counter : "+this.player.getRestartTime()*3+" of "+this.player.getGlobalVariables().getClientManagerRestart()*3); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
else if(request.equals("error")) //$NON-NLS-1$
this.socketOut.println("file error : "+this.player.getFileError()+" db error : "+this.player.getDbError()+" freezed screen : "+this.player.getFreezedScreen() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
else if(request.equals("mplayer")) //$NON-NLS-1$
this.socketOut.println("mplayer running : "+Mplayer.getInstance().IsPlaying()+" playlist : "+Mplayer.getInstance().getPlaylist()); //$NON-NLS-1$//$NON-NLS-2$
else if(request.indexOf("execute")>-1) //$NON-NLS-1$
{
this.player.serverRequest(request.substring(6,request.length()));
this.socketOut.println(request+" sent"); //$NON-NLS-1$
}
this.socketOut.println(request);
}
}
...qualche idea?...anche su chi sia questo noto programmatore italiano?...eh eh...
...ciao...
...ho "rubato" un esempio di socket server da un noto programmatore italiano...ho adattato il codice alle mie esigenze e l'ho aggiunto ad un precedente programma in modo da poter monitorare tramite lan il funzionamento delle stesse...ho scelto come porta di comunicazione la 12000...la connessione verso il server avviene senza problemi ma le risposte che questo dovrebbe ritornare non vengono inoltrate...
public class SocketServer {
int port;
BufferedReader socketIn = null;
PrintWriter socketOut = null;
ServerSocket socketServer = null;
Socket sock = null;
SocketAddress socketAddress = null;
String line = ""; //$NON-NLS-1$
static Logger AppLogger = Logger.getLogger("AppLogger"); //$NON-NLS-1$
Player player;
public SocketServer(){
AppLogger.debug(""); //$NON-NLS-1$
}
public SocketServer(int port){
System.out.println("server start on port : " + port); //$NON-NLS-1$
this.port = port;
}
public void go(){
new Thread(){
public void run(){
try{
SocketServer.this.socketServer = new ServerSocket(SocketServer.this.port);
SocketServer.this.sock = SocketServer.this.socketServer.accept();
SocketServer.this.socketAddress = SocketServer.this.sock.getRemoteSocketAddress ();
System.out.println ("Received connection from " + SocketServer.this.socketAddress); //$NON-NLS-1$
try{
SocketServer.this.socketIn = new BufferedReader (new InputStreamReader (SocketServer.this.sock.getInputStream ()));
SocketServer.this.socketOut = new PrintWriter (SocketServer.this.sock.getOutputStream (), true);
while ((SocketServer.this.line = SocketServer.this.socketIn.readLine ()) != null)
{
if (SocketServer.this.line.equals ("quit")) //$NON-NLS-1$
break;
request(SocketServer.this.line);
}
}
finally
{
if (SocketServer.this.socketIn != null)
SocketServer.this.socketIn.close ();
if (SocketServer.this.socketOut != null)
SocketServer.this.socketOut.close ();
SocketServer.this.sock.close ();
}
}
catch (Exception e)
{
AppLogger.debug(e.getMessage ());
}
}
}.start();
}
public void setPlayer(Player player){
this.player = player;
}
public void request(String request){
if(request.equals("recent")) //$NON-NLS-1$
this.socketOut.println(this.player.getRecentBarcode());
else if(request.equals("cycle")) //$NON-NLS-1$
this.socketOut.println("global cycle : "+this.player.isPlayer()+" audio cycle : "+this.player.isAudio()+" video cycle : "+this.player.isVideo()+" idle cycle : "+this.player.isIdle()); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
else if(request.equals("timer")) //$NON-NLS-1$
this.socketOut.println("idle counter : "+this.player.getCounter()/10+" of "+this.player.getGlobalVariables().getIdleInterval()/10+" restart counter : "+this.player.getRestartTime()*3+" of "+this.player.getGlobalVariables().getClientManagerRestart()*3); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
else if(request.equals("error")) //$NON-NLS-1$
this.socketOut.println("file error : "+this.player.getFileError()+" db error : "+this.player.getDbError()+" freezed screen : "+this.player.getFreezedScreen() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
else if(request.equals("mplayer")) //$NON-NLS-1$
this.socketOut.println("mplayer running : "+Mplayer.getInstance().IsPlaying()+" playlist : "+Mplayer.getInstance().getPlaylist()); //$NON-NLS-1$//$NON-NLS-2$
else if(request.indexOf("execute")>-1) //$NON-NLS-1$
{
this.player.serverRequest(request.substring(6,request.length()));
this.socketOut.println(request+" sent"); //$NON-NLS-1$
}
this.socketOut.println(request);
}
}
...qualche idea?...anche su chi sia questo noto programmatore italiano?...eh eh...
...ciao...