Ho un client chat strutturato così:
codice:
// $Id$
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Client extends Panel implements Runnable
{
// Parti grafiche (per successiva implementaz. tramite Applet)
private TextField tf = new TextField();
private TextArea ta = new TextArea();
private Socket socket = null;
// Oggetti DataOutputStream con i quali comunichiamo col Server
private DataOutputStream dout;
private DataInputStream din;
// Costruttore
public Client( String host, int port ) {
// Set up the screen
setLayout( new BorderLayout() );
add( "North", tf );
add( "Center", ta );
// We want to receive messages when someone types a line
// and hits return, using an anonymous class as
// a callback
tf.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
processMessage( e.getActionCommand() );
}
} );
// Connessione al server
try {
// Initiate the connection
socket = new Socket( host, port );
// We got a connection! Tell the world
System.out.println( "connected to "+socket );
// Let's grab the streams and create DataInput/Output streams
// from them
din = new DataInputStream( socket.getInputStream() );
dout = new DataOutputStream( socket.getOutputStream() );
// Start a background thread for receiving messages
new Thread( this ).start();
} catch( IOException ie ) { System.out.println( ie ); }
}
//
private void processMessage( String message ) {
try {
//
dout.writeUTF( message );
//
tf.setText( "" );
} catch( IOException ie ) { System.out.println( ie ); }
}
//
public void run() {
try {
//
while (true) {
//
String message = din.readUTF();
//
ta.append( message+"\n" );
}
} catch( IOException ie ) { System.out.println( ie ); }
}
static public void main( String args[] ){
//
String host = args[0];
int port = Integer.parseInt( args[1] );
//
//
new Client( host, port);
}
}
E un Applet così strutturata:
codice:
// $Id$
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ClientApplet extends Applet{
public void init() {
String host = getParameter( "host" );
int port = Integer.parseInt( getParameter( "port" ) );
setLayout( new BorderLayout() );
add( "Center", new Client( host, port ) );
}
}
Il server funziona regolarmente, lo posso mettere in ascolto su una porta qualsiasi. Il client ho provato a modificarlo ma non funziona. Tramite applet come lo devo richiamare??Da una pagina web, come posso farlo??Quali parametri devo passare??
Non essendo necessaria una interfaccia grafica, come posso modificare il client, anche per farlo andare magari da riga di comando??
Ringrazio tutti in anticipo per le eventuali risposte...