PDA

View Full Version : ...Java SocketServer...


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...

ally
21-11-2007, 14:46
...ancora problemi con questo socket server...ho ripulito un po' il codice...il mio problema maggiore è di mantenere il server su dopo la disconnessione del client...al log out del client infatti il server termina...


public class ServerEvo {

int port = 12000;
BufferedReader socketIn = null;
PrintWriter socketOut = null;
ServerSocket socketServer = null;
Socket sock = null;
SocketAddress socketAddress = null;
String line = ""; //$NON-NLS-1$

public ServerEvo(int port){

System.out.println("server start on port : " + port); //$NON-NLS-1$
try{
this.port = port;
this.socketServer = new ServerSocket (this.port);
this.sock = this.socketServer.accept ();
}catch(Exception e){
System.out.println(e);
}

go();
}

public void go(){

new Thread(){
public void run(){
try{
socketAddress = sock.getRemoteSocketAddress ();
System.out.println ("Received connection from " + socketAddress);
try{
socketIn = new BufferedReader (new InputStreamReader (sock.getInputStream ()));
socketOut = new PrintWriter (sock.getOutputStream (), true);
while ((line = socketIn.readLine ()) != null)
{
System.out.println ("Received from " + socketAddress + " string \"" + line + "\"");
socketOut.println(line);
if (line.equals ("quit"))
break;
}
}
finally
{
System.out.println ("Disconnection from " + socketAddress);
if (socketIn != null)
socketIn.close ();
if (socketOut != null)
socketOut.close ();
sock.close ();
}
}
catch (Exception e)
{
System.out.println (e.getMessage ());
}
}

}.start();

}

public static void main(String args[]){

new ServerEvo(12000);

}
}


...il codice di Andbin da cui ho preso spunto è composto di due parti principali...il socketSrverRunnable che ho replicato nel metodo go annegando il thread...

public class SocketServerRunnable implements Runnable{

private Socket sock;

public SocketServerRunnable (Socket sock){

this.sock = sock;
}

public void run(){

try{
BufferedReader socketIn = null;
PrintWriter socketOut = null;
SocketAddress socketAddress = sock.getRemoteSocketAddress ();
System.out.println ("Received connection from " + socketAddress);
try{
socketIn = new BufferedReader (new InputStreamReader (sock.getInputStream ()));
socketOut = new PrintWriter (sock.getOutputStream (), true);
String line;
while ((line = socketIn.readLine ()) != null)
{
System.out.println ("Received from " + socketAddress + " string \"" + line + "\"");
socketOut.println(line);
if (line.equals ("quit"))
break;
}
}
finally
{
System.out.println ("Disconnection from " + socketAddress);
if (socketIn != null)
socketIn.close ();
if (socketOut != null)
socketOut.close ();
sock.close ();
}
}
catch (Exception e)
{
System.out.println (e.getMessage ());
}
}
}


...e il SocketServer...il ciclo while di quest'ultimo è responsabile di mantenere up il server anche dopo le disconessioni dei client...la mia idea era pero' di mantenere il server slegato dal resto del programma...dovrei quindi eliminare il ciclo while o isolarlo dal funzionamento del programma principale...


public class SocketServer{

private ServerSocket socketServer;

public SocketServer(int port)throws IOException{

socketServer = new ServerSocket (port);
start();

}

public void start() throws IOException {

Thread thread;
SocketServerRunnable socketServerRunnable;

System.out.println ("SocketSample1 Server started");

try {
while (true)
{
Socket socket = socketServer.accept ();
socketServerRunnable = new SocketServerRunnable(socket);
thread = new Thread (socketServerRunnable);
thread.start ();
}
}
finally
{
System.out.println ("SocketSample1 Server terminated");
socketServer.close();
}
}
}

...grazie...ciao...