ricardolecheta 27 de dez. de 2007
algumas streams como a DataInputStream bloqueiam o readXXX se nao tiver nada para ler…
ou vc usa algo mais profi como :
http://xsocket.sourceforge.net/
ou
http://mina.apache.org/
Granella 28 de dez. de 2007
Olá Ricardo!
Gostei muito desse xsocket, uma pena que não de para usar pois o host que irá prover o serviço usa java 1.3 e tanto esse quanto o mina requerem versões mais recentes do java.
Bom na luta de resolver e garimpando o google encontrei algumas coisas como esta abaixo que estou usando:
package fef ;
import java.io.DataInputStream ;
import java.io.IOException ;
import java.io.PrintStream ;
import java.net.ServerSocket ;
import java.net.Socket ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Vector ;
public class Server extends Thread {
protected int port ;
protected ServerSocket server_port ;
protected ThreadGroup CurrentConnections ;
protected Vector connections ;
ServerWriter writer ;
public Server ( int port ) {
super ( & quot ; Server Socket & quot ;);
this . port = port ;
try {
server_port = new ServerSocket ( this . port );
} catch ( IOException e ) {
System . out . println ( e . getMessage ());
System . exit ( 0 );
}
CurrentConnections = new ThreadGroup ( & quot ; Server Connections & quot ;);
System . out . println ( & quot ; Server configurado para receber conexoes na porta : & quot ; + this . port );
System . out . println ( & quot ; ### Conexoes Realizadas ### & quot ;);
connections = new Vector ();
writer = new ServerWriter ( this );
this . start ();
}
public void run () {
try {
while ( true ) {
String dataAtual = new SimpleDateFormat ( & quot ; dd / MM / yyyy HH : mm : ss & quot ;). format ( new Date ());
Socket cliente_socket = server_port . accept ();
Connection c = new Connection ( cliente_socket , CurrentConnections , 3 , writer );
System . out . println ( & quot ; IP : & quot ; + cliente_socket . getInetAddress () + & quot ; Porta : & quot ; + cliente_socket . getPort () + & quot ; - & quot ; + dataAtual );
synchronized ( connections ) {
connections . addElement ( c );
}
}
} catch ( IOException e ) {
System . out . println ( e + & quot ; Exception & quot ;);
}
}
public static void main ( String [] args ) {
if ( args . length == 1 ) {
try {
new Server ( Integer . parseInt ( args [ 0 ] ));
} catch ( NumberFormatException ex ) {
System . out . println ( & quot ; Digite somente numeros . & quot ;);
}
}
}
}
class Connection extends Thread {
static int numberOfConnections = 0 ;
protected Socket client ;
protected DataInputStream in ;
protected PrintStream out ;
protected ServerWriter writer ;
public Connection ( Socket cliente_socket , ThreadGroup CurrentConnections , int priority , ServerWriter writer ) {
super ( CurrentConnections , & quot ; Conexao # & quot ; + numberOfConnections ++ );
this . setPriority ( priority );
client = cliente_socket ;
this . writer = writer ;
try {
in = new DataInputStream ( client . getInputStream ());
out = new PrintStream ( client . getOutputStream ());
writer . OutputStreams . addElement ( out );
} catch ( IOException e ) {
try {
client . close ();
System . out . println ( & quot ; Exception while getting socket streams : & quot ; + e );
} catch ( IOException e2 ) {
System . out . println ( & quot ; Exception while close socket client : & quot ; + e2 );
return ;
}
}
this . start ();
}
public void run () {
String inline ;
out . println ( & quot ; Conectado ... & quot ;);
try {
while ( true ) {
inline = in . readLine ();
if ( inline == null ) {
break ;
}
writer . setOutdata ( inline );
synchronized ( writer ) {
writer . notify ();
}
}
} catch ( IOException e ) {
} finally {
try {
client . close ();
writer . setOutdata ( & quot ; Host : & quot ; + client . getInetAddress () + & quot ; Porta : & quot ; + client . getPort () + & quot ; desconectou . & quot ;);
} catch ( IOException e2 ) {
}
}
}
}
class ServerWriter extends Thread {
protected Server server ;
public Vector OutputStreams ;
public String outdata ;
private String outputline ;
public ServerWriter ( Server s ) {
super ( s . CurrentConnections , & quot ; Server Writer & quot ;);
server = s ;
OutputStreams = new Vector ();
this . start ();
}
public void setOutdata ( String mensagem ) {
outdata = mensagem ;
}
public synchronized void run () {
while ( true ) {
try {
this . wait ();
} catch ( InterruptedException e ) {
System . out . println ( & quot ; Interrupted Exception & quot ; + e . getMessage ());
}
outputline = outdata ;
synchronized ( server . connections ) {
for ( int i = 0 ; i & lt ; OutputStreams . size (); i ++ ) {
PrintStream out ;
out = ( PrintStream ) OutputStreams . elementAt ( i );
out . println ( outputline );
}
}
System . out . println ( server . writer . outputline + & quot ; \ n \ r & quot ;);
}
}
}
falta agora fazer os clientes do nosso departamento "escutarem" todas as chamadas vinda de outros departamentos e integrar com o Forms...