Client/Server-Kommunikation über TCP/IP-Socket



  • Hallo zusammen,

    Das Programm Client soll eine Zeichenkette an das Programm Server senden, welches diese auf der Kommandozeile ausgibt.

    import java.net.*;
    import java.io.*;
    
    public class Client
    {
    	public static void main(String[] args) {
    		try {
    
    		Socket socket = new Socket("localhost", 5005);
    		DataOutputStream os =
    			new DataOutputStream(socket.getOutputStream());
    
    		os.writeChars("Wieder ein langweiliger Standardspruch\n");
    
    		os.close();
    		socket.close();
    
    		} catch (IOException e) { /* ... */ }
    	}
    }
    
    import java.net.*;
    import java.io.*;
    
    public class Server
    {
    	public static void main(String[] args) {
    		try {
    
    		ServerSocket server = new ServerSocket(5005);
    		Socket socket = server.accept();
    		BufferedReader is = new BufferedReader(
    			new InputStreamReader(socket.getInputStream()));
    
    		String req = is.readLine();
    		// Hier wird alles korrekt ausgegeben ...
    		System.out.println(req);
    		// aber folgendes funktioniert nicht:
    		String s = "Wieder ein langweiliger Standardspruch";
    		if (req.equals(s))
    			System.out.println('='); // wird nicht ausgegeben
    		System.out.println(req.compareTo(s)); // Ausgabe: -87
    
    		is.close();
    		socket.close();
    
    		} catch (IOException e) { /* ... */ }
    	}
    }
    

    Die Zeichenkette wird richtig auf der Konsole ausgegeben, nur der Vergleich mit equals bzw. compareTo funktioniert nicht. Weiß jemand woran das liegt?

    Grüße
    Martin



  • Ich bin in Java nicht unbedingt beste (lerne noch), aber ich denke mal:

    "Wieder ein langweiliger Standardspruch\n" != "Wieder ein langweiliger Standardspruch"
    


  • Das habe ich bereits ausgetestet. Leider liegt es nicht daran.

    Folgendes steht dazu in der API-Dokumentation:

    readLine

    public String readLine()
    throws IOException

    Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

    Returns:
    A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
    Throws:
    IOException - If an I/O error occurs


Anmelden zum Antworten