Salve a tutti ragazzi..ho bisogno di un aiutino...in pratica ho scaricato una chat in java composto tra 3 classi: chatServer, chatClient e FinestraChat. Ora il server ha il main e non ho problemi a lanciarlo (lo lancio con Jcreator o da dos). Lanciato il server il mio problema è lanciare il client con l'interfaccia grafica... lanciadolo da telnet con dos va...pero senza interfaccia naturalmente...con Jcreator mi dice invece che manca il main... come si puo fare...ne ho scaricato 2 chat con la stessa strategia...il client è senza main...mi dite una soluzione...inserisco qui il codice: ( non so se e il giusto modo di inserirlo in caso avvisatemi che modifico):
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class ChatClient extends Applet {
boolean isStandalone = false;
Button bConferma = new Button();
Label lMessaggio = new Label();
Label label2 = new Label();
TextField tUserid = new TextField();
FinestraChat chat;
Socket t;
static DataInputStream is;
static DataOutputStream os;
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setBackground(Color.blue);
this.setSize(new Dimension(445, 197));
bConferma.setActionCommand("");
bConferma.setFont(new Font("TimesRoman", 1, 14));
bConferma.setLabel("Conferma");
bConferma.setBounds(new Rectangle(168, 106, 108, 38));
bConferma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
bConferma_actionPerformed(e);
}
});
lMessaggio.setBackground(Color.blue);
lMessaggio.setForeground(Color.red);
lMessaggio.setFont(new Font("Dialog", 3, 12));
lMessaggio.setBounds(new Rectangle(6, 165, 428, 29));
label2.setText("Userid :");
tUserid.setBackground(Color.white);
tUserid.setBounds(new Rectangle(100, 34, 302, 28));
label2.setBounds(new Rectangle(34, 33, 56, 29));
label2.setFont(new Font("Dialog", 1, 14));
label2.setForeground(Color.black);
label2.setBackground(Color.blue);
this.setLayout(null);
this.add(bConferma, null);
this.add(label2, null);
this.add(lMessaggio, null);
this.add(tUserid, null);
tUserid.requestFocus();
}
void bConferma_actionPerformed(ActionEvent e) {
if (tUserid.getText().trim().equalsIgnoreCase("")) {
lMessaggio.setText("Digitare una userid");
tUserid.requestFocus();
return;
}
lMessaggio.setText("");
try
{
t = new Socket("127.0.0.1",1111);
is = new DataInputStream(t.getInputStream());
os = new DataOutputStream(t.getOutputStream());
os.writeBytes(tUserid.getText()+"\n");
boolean more = true;
chat = new FinestraChat("Finestra di discussione di "+tUserid.getText());
chat.setVisible(true);
chat.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
chat_windowClosing(e);
}
});
bConferma.setEnabled(false);
}
catch (ConnectException ce)
{lMessaggio.setText("Non c'e' alcun server in attesa");}
catch(IOException ioe)
{ System.out.println("Error " + ioe); }
}
void chat_windowClosing(WindowEvent e) {
chat.setVisible(false);
bConferma.setEnabled(true);
try {
t.close();
} catch(IOException ioe)
{ System.out.println("Error " + ioe); }
tUserid.requestFocus();
}
}
import java.io.*;
import java.net.*;
import java.util.Vector;
public class ChatServer {
static Vector out = new Vector();
static Vector userid = new Vector();
public static void main(String[] args )
{
try
{
ServerSocket s = new ServerSocket(1111);
System.out.println( "Server in attesa di amici sulla porta 1111" );
boolean done = false;
while (!done)
{
try
{
Socket incoming = s.accept();
DataInputStream chatIn = new DataInputStream(incoming.getInputStream());
DataOutputStream chatOut = new DataOutputStream(incoming.getOutputStream());
out.addElement(chatOut);
String str = chatIn.readLine();
if (str == null)
done = false;
else {
System.out.println( "E' entrato il nuovo amico "+str);
userid.addElement(str);
(new ThreadServer(chatIn,chatOut,str)).start();
}
} catch (Exception e)
{ System.out.println(e);
}
}
s.close();
}
catch (Exception e)
{ System.out.println(e);
}
}
}
class ThreadServer extends Thread {
DataInputStream chatIn;
DataOutputStream chatOut;
String chatUser;
ThreadServer(DataInputStream chatIn, DataOutputStream chatOut, String chatUser) {
this.chatIn = chatIn;
this.chatOut = chatOut;
this.chatUser = chatUser;
}
public void run() {
try {
// avvisa tutti della nuova userid
for (int i = 0 ; i < ChatServer.out.size() ; i++) {
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("U");
for (int j = 0 ; j < ChatServer.userid.size() ; j++)
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("<"+(String)ChatServer.userid.elementAt (j)+">");
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("\n");
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("T***** Entra "+chatUser+"\n");
}
while (true) {
String str = chatIn.readLine();
if (str == null) break;
// avvisa tutti del nuovo messaggio
for (int i = 0 ; i < ChatServer.out.size() ; i++) {
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("T<"+chatUser+">"
+str+"\n");
}
}
} catch (IOException ioe) {System.out.println("errore "+ioe);}
for (int i = 0 ; i < ChatServer.out.size() ; i++) {
if (((String)(ChatServer.userid.elementAt(i))).equals IgnoreCase(chatUser)) {
ChatServer.userid.removeElementAt(i);
ChatServer.out.removeElementAt(i);
break;
}
}
try {
// avvisa tutti del nuovo elenco di userid
for (int i = 0 ; i < ChatServer.out.size() ; i++) {
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("U");
for (int j = 0 ; j < ChatServer.userid.size() ; j++)
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("<"+(String)ChatServer.userid.elementAt (j)+">");
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("\n");
}
for (int i = 0 ; i < ChatServer.out.size() ; i++) {
((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("T***** Esce "+chatUser+"\n");
}
} catch (IOException ioe) {System.out.println("errore "+ioe);}
super.run();
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.StringTokenizer;
public class FinestraChat extends Frame {
TextArea taNomi = new TextArea();
TextField tMessaggio = new TextField();
TextArea taChat = new TextArea();
Button button1 = new Button();
public FinestraChat(String titolo) {
this();
setTitle(titolo);
}
public FinestraChat() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setBackground(new Color(0, 192, 0));
this.setSize(new Dimension(571, 535));
this.setTitle("Finestra di discussione");
this.setLayout(null);
taNomi.setBackground(Color.white);
taNomi.setFont(new Font("TimesRoman", 0, 14));
taNomi.setBounds(new Rectangle(453, 29, 107, 400));
tMessaggio.setBackground(Color.white);
tMessaggio.setBounds(new Rectangle(18, 438, 533, 33));
taChat.setBackground(Color.white);
taChat.setFont(new Font("TimesRoman", 1, 14));
taChat.setBounds(new Rectangle(11, 28, 437, 401));
button1.setFont(new Font("TimesRoman", 1, 14));
button1.setBounds(new Rectangle(211, 473, 115, 43));
button1.setLabel("Invia");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.add(taNomi, null);
this.add(tMessaggio, null);
this.add(taChat, null);
this.add(button1, null);
(new ThreadClient(this)).start();
tMessaggio.requestFocus();
}
void button1_actionPerformed(ActionEvent e) {
try {
if (!tMessaggio.getText().trim().equalsIgnoreCase("") ) {
ChatClient.os.writeBytes(tMessaggio.getText()+"\n" );
tMessaggio.setText("");
}
} catch (IOException ioe) {System.out.println("Errore = "+ioe);}
tMessaggio.requestFocus();
}
}
class ThreadClient extends Thread {
FinestraChat fc;
ThreadClient(FinestraChat fc) {
this.fc = fc;
}
public void run() {
try {
while (true) {
String str = ChatClient.is.readLine();
if (str == null) break;
if (str.substring(0,1).equalsIgnoreCase("U")) {
fc.taNomi.setText("");
StringTokenizer toke = new StringTokenizer(str.substring(1),">");
while (toke.hasMoreElements()) {
fc.taNomi.setText(fc.taNomi.getText()+toke.nextTok en()+">"+"\n");
}
}
else {
fc.taChat.setText(fc.taChat.getText()+str.substrin g(1)+"\n");
}
}
} catch (IOException ioe) {}
super.run();
}
}

Rispondi quotando

