freedb.org Query

Status
Nicht offen für weitere Antworten.
T

TheSunToucher

Gast
Hallo,

ich hab ein kleines Problem beim absetzen von Queries auf die freedb.

Wer's nicht kennt:
über die freedb kann man informationen über ein album abfragen, also Albumname, Tracks, Erscheinungsjahr usw. dafür benutzt man das cddb-Protokoll. Infos unter: freedb.org

Jetzt zu meinem Problem:

Erstmal habe ich eine Simple anfrage über telnet ausprobiert, lief einwandfrei.
Code:
-> telnet freedb.freedb.org 8880
< 201 zaphod CDDBP server v1.5.1PL0 ready at Sat Nov 13 14:01:28 2004
-> cddb hello username hostname clientname version
< 200 Hello and welcome username@hostname running clientname version.
->cddb lscat
<210 OK, category list follows (until terminating `.')
<data
<folk
<jazz
<misc
<rock
<country
<blues
<newage
<reggae
<classical
<soundtrack
<.

Wenn ich das ganze aber in einer Java-Klasse über einen Socket
versuche funktionierts nicht!

Code:
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Freedb {

	/**
	 * @throws IOException
	 * 
	 */
	public Freedb() throws IOException {
		
		InetAddress addr = InetAddress.getByName("freedb.freedb.org");
		int port = 8880;
		Socket socket = new Socket(addr, port);
		
		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
	    
	        out.write("cddb hello username hostname clientname version");
	        out.write("cddb lscat");
	        out.flush();
	        
	        [COLOR=red]for (String line; (line = in.readLine()) != null; ) {
	            System.out.println(line);
	        }[/COLOR]
	        
	        in.close();
	        out.close();
	        socket.close();
	}

	/**
	 * For Tests only
	 * @param args 
	 */
	public static void main(String[] args) {
		try {
			new Freedb();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Das Problem ist, das er nie aus der for-Schleife kommt, also sie läuft nicht endlos, sondern einmal, gibt dann die erste Antwort aus (Welcome... siehe telnet) versucht die nächste Line zu lesen und bleibt da stehen.

Ich verstehe nicht warum. Ich hatte auch versucht den output nach jedem input abzuholen, gleiches Ergebnis! Als "termination"-Symbol ist '.' angegeben, ist das das Problem? Darif ich immer nur bis zum ersten Punkt lesen?

Ich bin für jede Hilfe dankbar,
gruß
TheSunToucher
 
T

TheSunToucher

Gast
Fehler gefunden!
Beim senden der Anfragen muss man ein "\n" mitschicken, also:

Code:
...
out.write("cddb hello username hostname clientname version\n");
out.write("cddb lscat\n");
...

Danke an die Jungs aus dem IRC Channel irc.phat-net.de/java-forum.net
 
T

TheSunToucher

Gast
Also ich hab weiter an der Klasse geschrieben, der Fehler taucht wieder auf... :-\
Ich stell mal die komplette Klasse hier rein, bin für jeden Tipp dankbar.

Hab Kommentare und referenzen auf andere Klassen (org.apache.log4j usw.) mal rausgeschmissen, hoffe die läuft so... :-]

Code:
package de.thesuntoucher.freedb;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

public class FreedbConnection {
	
	/** <code>defaultHost</code> is used if no host is specified */
	public final static String defaultHost = "freedb.freedb.org";
	
	/** <code>defaultPort</code> is used if no port is specified */
	public final static int defaultPort = 8880; 
	
	private String host;
	private int port;
	
	private Socket socket = null;
	private BufferedReader in = null;
	private BufferedWriter out = null;
	
	/**
	 * 
	 * 
	 */
	public FreedbConnection() {
		this(defaultHost, defaultPort);
	}
	
	/**
	 * @param host
	 * 
	 */
	public FreedbConnection(String host) {
		this(host, defaultPort);
	}
	
	/**
	 * @param host
	 * @param port
	 */
	public FreedbConnection(String host, int port){
		this.host = host;
		this.port = port;
	}
	
	/**
	 * @throws IOException
	 */
	public void connect() throws IOException{
				
		InetAddress addr = InetAddress.getByName(this.host);
	    socket = new Socket(addr, this.port);
	    
	    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	    out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
	    
	    // welcomemessage
	    System.out.println(in.readLine());
        
	    // sending handshake and print the response
        String[] result = exec("cddb hello [email]thesuntoucher@sourceforge.net[/email] sourceforge.net jTagEditor 0.1");
		for (int i = 0; i < result.length; i++) {
			System.out.println(result[i]);
		}
	}
	
	/**
	 * @param query
	 * @return
	 * @throws Exception
	 */
	public String query(String query) throws Exception{
		
		out.write(query);
		out.newLine();
        out.flush();
		
        return in.readLine();
	}
	
	/**
	 * @param cmd
	 * @return
	 * @throws IOException
	 */
	public String[] exec(String cmd) throws IOException{
		
		ArrayList result = new ArrayList();
		
		// send command
		System.out.println("sending query: " + cmd);
		out.write(cmd);
		out.newLine();
        out.flush();
        
        // response
        char[] buf = new char[1024];
        while(in.read(buf) >= 0){
        	String s = new String(buf).trim();
        	result.add(s);
        	if(s.endsWith(".")) break;
        }
        
        /*
		for (String line; (line = in.readLine()) != null; ) {
			result.add(line);
			if(line.endsWith(".")) break;
        }
        */
		
		return (String[]) result.toArray(new String[result.size()]);
	}
	
	/**
	 * 
	 */
	public void close(){
		
		System.out.println("closing connection...");
		
		try{
			out.write("cddb quit");
			out.newLine();
			out.flush();
			in.close();
			out.close();
			socket.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		System.out.println("connection closed successfully");
	}
	
	/**
	 * @return a String[] containing the supported genres
	 * @throws IOException
	 */
	public String[] getGenreList() throws IOException{
		return exec("cddb lscat");
	}

	/**
	 * For Tests only
	 * @param args 
	 */
	public static void main(String[] args) {
		try {
			
			// getConnection
			FreedbConnection fdb = new FreedbConnection();
			
			// connect
			fdb.connect();
			
			// list genres
			String[] genres = fdb.getGenreList();
			for (int i = 0; i < genres.length; i++) {
				System.out.println(genres[i]);
			}
			
			// cddb query c10f420f 15 150 11285 21702 37942 71487 86592 109332 124057 151442 173425 192332 212430 242155 258807 271272 3908
			System.out.println(fdb.query("cddb query c10f420f 15 150, 11285, 21702, 37942, 71487, 86592, 109332, 124057, 151442, 173425, 192332, 212430, 242155, 258807, 271272 3908"));
			
			// cddb read
			String[] result = fdb.exec("cddb read soundtrack c10f420f");
			for (int i = 0; i < result.length; i++) {
				System.out.println(result[i]);
			}
			
			// close
			fdb.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 

gest01

Mitglied
Hi

Bin gerade dabei ein grafisches Frontend für cdparanoia und lame zu schreiben. Ich möchte ebenfalls eine CDDB Abfrage starten. Dazu benötige ich aber die CDID. Wie kann ich die auslesen?? bzw. kennt jemand eine lib die das tut??


thx
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
S send riesige "Query" to servlet Netzwerkprogrammierung 7

Ähnliche Java Themen

Neue Themen


Oben