Java - ¡¡¡¡¡¡¡¡¡¡sobre sockets ayuda de favor!!!!!!!!!!!!

 
Vista:

¡¡¡¡¡¡¡¡¡¡sobre sockets ayuda de favor!!!!!!!!!!!!

Publicado por luis (5 intervenciones) el 25/01/2006 00:24:35
necesito un programa que mande cadenas del std de un cliente a un servidor y que solamante se detenga el cliente por ejemplo si mandas exit y que se detenga tos el proceso si le mandas por ejemplo shutdown ya tengo reslovido la primera parte pero en la segunda si detengo el cliente se detiene el servidor tambien como hago para que escuchen tanto cliente como servidor de manera independiente cuando les mandas un mensaje de salida? gracias aqui estan los fuentes hasta donde tengo:
CLIENTE:
import java.net.ConnectException;
public class ClientEx
{

/*_____________________________________________________________________________
*
*
* MAIN
*
*___________________________________________________________________________*/

public static void main ( String argv [ ] ) throws IOException
{
Socket socketClient = null;
PrintWriter output = null;
String host = null;
BufferedReader in = null;
int port = 0,
i = 0,
j = 0;
/*---command-line arguments---*/
if ( argv.length != 4)
{
System.err.println ("usage: java ClientEx -ip_address < > " +
"-port <#> ");

System.exit ( 0 );
}
else
for ( i = 0; i < argv.length; i++ )
{
if( argv [ i ].compareTo ( "-ip_address" ) == 0 )
{
host = ( argv [ i+1 ]);
}
else if( argv [ i ].compareTo ( "-port" ) == 0 )
{
try
{
port = Integer.parseInt ( argv [ i+1 ] );
}
catch ( NumberFormatException e )
{
System.err.println ("\n\nport must be in number: " +
e.toString ( ));
System.err.println ("usage: java ClientEx -ip_address < > " +
"-port <#> ");
System.exit ( 0 );
}
}
}
if ( port < 0 )
{
System.err.println ("\n port can't be negative \n\n" +
"usage: java ClientEx -ip_address < > " +
"-port <#> ");
System.exit ( 0 );
}


for (i = 0; i < 4; i += 2)

{
if ( argv[ i ].compareTo ( "-ip_address" ) != 0 &&
argv[ i ].compareTo ( "-port" ) != 0 )
{
System.err.println ("\n\ninvalid option: '" +argv [ i ]+"'\n" +
"usage: java ClientEx -ip_address < > " +
"-port <#> ");

System.exit ( 0 );
}
}
for (i = 2; i >= 0; i -=2)
{
for (j = 0; j <= i-1; j +=2)
if( argv [ i ].compareTo (argv [ j ]) == 0)
{
System.err.println ("\n\nduplicated option: '" +
argv [ i ]+"'\n" +
"usage: java ClientEx -ip_address < > " +
"-port <#> ");

System.exit ( 0 );
}
}


try
{

socketClient = new Socket( host ,port );

System.out.println(" client connected");

output = new PrintWriter (socketClient.getOutputStream ( ) ,true);



in =
new BufferedReader(new InputStreamReader(socketClient.getInputStream( )));

}
catch ( UnknownHostException e)

{
System.err.println("IP of host couldn't be determined" +
e.toString ( ));
System.exit ( 0 );
}
catch ( ConnectException e )
{
System.err.println ("\n\nBe sure that the port be the " +
"same as the server is running otherwise " +
"if is a server running\n " +
e.toString ( ));
System.exit ( 0 );
}

String server = null;
BufferedReader stdIn =
new BufferedReader(new InputStreamReader( System.in ));
while((server = stdIn.readLine( )).compareTo("exit") != 0)
{
output.println ( server );
}



}/*____________________________END OF MAIN_________________________________*/

}/*_________________________END OF ClientEx Class___________________________*/

SERVIDOR:
/*_____________________________________________________________________________
*
* Clase : ServerEx
*
* Archivo : ServerEx.java
*
* Fecha : 18/Enero/2006 -> Creacion
*
* Autor : Luis Arturo
*
* Proposito : Funcionamiento de un servidor.
*
* v 1.0 : Inicial.
*___________________________________________________________________________*/

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketException;
public class ServerEx
{

/*_____________________________________________________________________________
*
*
* MAIN
*
*___________________________________________________________________________*/


public static void main ( String argv [ ] ) throws IOException
{
int Port = 0 ;
Socket clientSocket = null;
ServerSocket socketServer = null;
BufferedReader in = null;
PrintWriter output = null;
/*---command line arguments---*/

if( argv.length != 2 )
{
System.err.println( "usage: java ServerEx -port <#>" );
System.exit ( 0 );
}
try
{
Port = Integer.parseInt ( argv [ 1 ] );
}
catch( NumberFormatException e)
{
System.err.println ("\n The port must be a number: "+
e.toString ( ));

System.err.println( "usage: java ServerEx -port <#>" );
System.exit ( 0 );

}

if ( Port < 0 )
{

System.err.println( "\n port must be positive \n\n" +
"usage: java ServerEx -port <#>");
System.exit ( 0 );

}

else if (argv [ 0 ].compareTo ("-port") !=0 )
{

System.err.println( "\n invalid option '"+ argv [ 0 ] + "'\n" +
"usage: java ServerEx -port <#>");
System.exit ( 0 );

}



try
{
socketServer = new ServerSocket( Port );
System.out.println("Server created. Waiting client connections...");
}
catch (IOException e)

{
System.err.println("could not listen on that port " + e.toString( ));
}


try

{
clientSocket = socketServer.accept( );
System.out.println( "client accepted" );

}
catch ( IOException e )
{
System.err.println ( "accept failed" + e.toString( ));
}






in =
new BufferedReader
(new InputStreamReader(clientSocket.getInputStream( )));

while (true)
{
System.out.println("client sends: " + ">" + in.readLine() + "<");
if ((in.readLine( )).compareTo("shutdown")==0)
{
socketServer.close ( );
}

}


}/*___________________________ END MAIN ___________________________________*/

}/*_________________________ END ServerEx class______________________________*/
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder