Impressum · Kontakt · Hilfe
Besucher online · Mitglieder



Antwort
 
Themen-Optionen
Alt 02.03.2005, 18:29   Nach oben    #1
debian inside
Gast
 
Beiträge: n/a
Standard client problem

erstmal hallo
ich hab mich eben angemeldet weil ich mit java beginnen möchte


hab ein problem mit nem chat client:

Code:
 
... 
recvsock = new Socket(serverHost, recvport); 
is = new BufferedReader(new InputStreamReader(recvsock.getInputStream())); 
... 
new Thread(new Runnable() { 
	public void run() { 
		String line; 
		try { 
			while (loggedIn && ((line = is.readLine()) != null)) 
				ta.append(line + "\n"); 
		} catch(IOException e) { 
			showStatus("GAA! LOST THE LINK!!"); 
			return; 
		} 
	} 
}).start(); 
...

mit dem analyzer seh ich das über das netzwerk der richtige text kommt gefolgt von \r\n
aber im client kommt das nicht an

teilweise klappt es dann doch wieder ich kann aber keinen grund erkennen wieso
 
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Alt 08.03.2005, 16:36   Nach oben    #2
debian inside
Gast
 
Beiträge: n/a
Standard

nachdem mir mit dem codefragment scheinbar keiner helfen kann hier mal der gesamte code

es ist aus Darwins Java Kochbuch

ich hab es etwas umgeschrieben und in zeile 178 etwas zum debugen eingefügt

interessant finde ich das zwar der text aus readline() nicht ankommt aber der punkt aus zeile 178 erscheint :confused:

PHP-Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class 
ChatRoom extends Applet {
 
/** Whether we are being run as an Applet or an Application */
 
protected boolean inAnApplet true;
 
/** The state of logged-in-ness */
 
protected boolean loggedIn;
 
/* The Frame, for a pop-up, durable Chat Room. */
 
protected Frame cp;
 
/** The default port number */
 
protected static int RECVPORTNUM 1550;
 protected static 
int SENDPORTNUM 1555;
 
/** The actual port number */
 
protected int recvport;
 protected 
int sendport;
 
/** The network socket */
 
protected Socket recvsock;
 protected 
Socket sendsock;
 
/** BufferedReader for reading from socket */
 
protected BufferedReader is;
 
/** PrintWriter for sending lines on socket */
 
protected PrintWriter pw;
 
/** TextField for input */
 
protected TextField tf;
 
/** TextArea to display conversations */
 
protected TextArea ta;
 
/** The Login button */
 
protected Button lib;
 
/** The LogOUT button */
 
protected Button lob;
 
/** The TitleBar title */
 
final static String TITLE "Chat: Ian Darwin's Toy Chat Room Client";
 
/** The message that we paint */
 
protected String paintMessage;
 
/** Init, inherited from Applet */
 
public void init() {
  
paintMessage "Creating Window for Chat";
  
repaint();
  
cp = new Frame(TITLE);
  
cp.setLayout(new BorderLayout());
  
String recvportNum null;
  
String sendportNum null;
  if (
inAnApplet) {
   
recvportNum getParameter("recvport");
   
sendportNum getParameter("sendport");
 }
  
recvport RECVPORTNUM;
  
sendport SENDPORTNUM;
  if (
recvportNum != null)
   
recvport Integer.parseInt(recvportNum);
  if (
sendportNum != null)
   
sendport Integer.parseInt(sendportNum);
  
// The GUI
  
ta = new TextArea(1480);
  
ta.setEditable(false);  // readonly
  
ta.setFont(new Font("Monospaced"Font.PLAIN11));
  
cp.add(BorderLayout.NORTHta);
  
Panel p = new Panel();
  
Button b;
  
// The login button
  
p.add(lib = new Button("Login"));
  
lib.setEnabled(true);
  
lib.requestFocus();
  
lib.addActionListener(new ActionListener() {
   public 
void actionPerformed(ActionEvent e) {
    
login();
    
lib.setEnabled(false);
    
lob.setEnabled(true);
    
tf.requestFocus(); // set keyboard focus in right place!
   
}
  });
  
// The logout button
  
p.add(lob = new Button("Logout"));
  
lob.setEnabled(false);
  
lob.addActionListener(new ActionListener() {
   public 
void actionPerformed(ActionEvent e) {
    
logout();
    
lib.setEnabled(true);
    
lob.setEnabled(false);
    
lib.requestFocus();
   }
  });
  
p.add(new Label("Message here:"));
  
tf = new TextField(40);
  
tf.addActionListener(new ActionListener() {
   public 
void actionPerformed(ActionEvent e) {
    if (
loggedIn) {
     
pw.println(tf.getText());
     
tf.setText("");
    }
   }
  });
  
p.add(tf);
  
cp.add(BorderLayout.SOUTHp);
        
cp.addWindowListener(new WindowAdapter() {
   public 
void windowClosing(WindowEvent e) {
    
// If we do setVisible and dispose, then the Close completes
    
ChatRoom.this.cp.setVisible(false);
    
ChatRoom.this.cp.dispose();
    
logout();
   }
  });
  
cp.pack();
  
// After packing the Frame, centre it on the screen.
  
Dimension us cp.getSize(),
   
them Toolkit.getDefaultToolkit().getScreenSize();
  
int newX = (them.width us.width) / 2;
  
int newY = (them.heightus.height)/ 2;
  
cp.setLocation(newXnewY);
  
cp.setVisible(true);
  
paintMessage "Window should now be visible";
  
repaint();
 }
 protected 
String serverHost "192.168.2.2";
 
/** LOG ME IN TO THE CHAT */
 
public void login() {
  
showStatus("In login!");
  if (
loggedIn)
   return;
  if (
inAnApplet)
   
serverHost getCodeBase().getHost();
  try {
   
recvsock = new Socket(serverHostrecvport);
   
is = new BufferedReader(new InputStreamReader(recvsock.getInputStream()));
  } catch(
UnknownHostException e) {
   
showStatus("Unknown host " serverHost "/" recvport ": " e);
  } catch(
NoRouteToHostException e) {
   
showStatus("No rout to host " serverHost "/" recvport ": " e);
  } catch(
ConnectException e) {
   
showStatus("Connect failed " serverHost "/" recvport ": " e); 
  } catch(
IOException e) {
   
showStatus("Can't get recvsocket to " serverHost "/" recvport ": " e);
   
cp.add(new Label("Can't get recvsocket: " e));
   return;
  }
  
showStatus("Got recvsocket");
  
  try {
   
sendsock = new Socket(serverHostsendport);
   
pw = new PrintWriter(sendsock.getOutputStream(), true);
  } catch(
UnknownHostException e) {
   
showStatus("Unknown host " serverHost "/" sendport ": " e);
  } catch(
NoRouteToHostException e) {
   
showStatus("No rout to host " serverHost "/" sendport ": " e);
  } catch(
ConnectException e) {
   
showStatus("Connect failed " serverHost "/" sendport ": " e);
  } catch(
IOException e) {
   
showStatus("Can't get sendsocket to " serverHost "/" sendport ": " e);
   
cp.add(new Label("Can't get sendsocket: " e));
   return;
  }
  
showStatus("Got sendsocket");
   
  
// Construct and start the reader: from server to textarea.
  // Make a Thread to avoid lockups.
  
new Thread(new Runnable() {
   public 
void run() {
    
String line;
    try {
     while (
loggedIn && ((line is.readLine()) != null) && (sendsock != null) && (recvsock != null)) {
      
ta.append(line "\n");
      
ta.append(".");
     }
    } catch(
IOException e) {
     
showStatus("GAA! LOST THE LINK!!");
     return;
    }
   }
  }).
start();
  
// FAKE LOGIN FOR NOW
  //pw.println("AppletUser");//Chat.CMD_LOGIN + 
  
loggedIn true;
 }
 
/** Log me out, Scotty, there's no intelligent life here! */
 
public void logout() {
  if (!
loggedIn)
   return;
  
loggedIn false;
  try {
   if (
recvsock != null)
    
recvsock.close();
  } catch (
IOException ign) {
   
// so what?
  
}
  
  try {
   if (
sendsock != null)
    
sendsock.close();
  } catch (
IOException ign) {
   
// so what?
  
}
 }

 
/** Paint paints the small window that appears in the HTML,
  * telling the user to look elsewhere!
  */
 
public void paint(Graphics g) {
  
Dimension d getSize();
  
int h d.height;
  
int w d.width;
  
g.fillRect(00w0);
  
g.setColor(Color.black);
  
g.drawString(paintMessage10, (h/2)-5);
 }

 
/** a showStatus that works for Applets or non-Applets alike */
 
public void showStatus(String mesg) {
  if (
inAnApplet)
   
super.showStatus(mesg);
  
System.out.println(mesg);
 }
 
/** A main method to allow the client to be run as an Application */
 
public static void main(String[] args) {
  
ChatRoom room101 = new ChatRoom();
  
room101.inAnApplet false;
  
room101.init();
  
room101.start();
 }

 
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Alt 10.03.2005, 20:52   Nach oben    #3
debian inside
Gast
 
Beiträge: n/a
Standard

ist mir ja fast schon peinlich...

der server hat immer ne lange leere zeichenkette gesendet um zu prüfen ob der client noch antwortet und somit verbunden ist

hab das jetzt geändert so das nur mehr ein zeichen gesendet wird und im client ignoriere ich so kurze strings einfach

sorry ist mein erstes projekt mit client und server
 
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Antwort

Lesezeichen


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 
Themen-Optionen

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist An.
Smileys sind An.
[IMG] Code ist An.
HTML-Code ist Aus.
Trackbacks are An
Pingbacks are An
Refbacks are Aus

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Problem E-Mails zu senden Jann Hendrik Plauderecke 7 25.05.2007 21:37
Logisches Problem beim einsatz von Ereignissen Prophet Allgemeine Java-Programmierung 19 05.06.2006 22:08
Problem mit Cookie und Reloads... Bookworm PHP-Programmierung 10 13.04.2006 12:09
Chat *Logik?* Problem relax Allgemeine Java-Programmierung 8 15.12.2005 08:25
OSX + Eclipse 3.1 Problem bacarni Eclipse 3 29.07.2005 21:19


Alle Zeitangaben in WEZ +2. Es ist jetzt 09:18 Uhr.

Nach oben
Wir nutzen das Zend Framework, vBulletin (vBulletin v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0) und vBSEO.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44