我正在尝试使用 Java 中的套接字将 RDF/XML 从客户端发送到服务器。当我发送信息时,服务器程序挂起并且不接收信息,除非我关闭客户端的 Socket 或 OutputStream。即使我在客户端刷新 OutputStream,除非我关闭 Socket/Stream,否则服务器也不会接收数据。我想在不关闭套接字的情况下发送信息。这是客户端的一些示例代码(使用 Sesame):
import java.io.*;
import java.net.*;
import org.openrdf.rio.*;
import org.openrdf.rio.helpers.*;
import org.openrdf.model.URI;
import org.openrdf.model.Model;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.Statement;
import org.openrdf.model.impl.*;
import org.openrdf.model.vocabulary.*;
public class SimpleRDFClient {
private Socket socket = null;
public static void main(String[] args) {
new SimpleRDFClient(args[0],Integer.parseInt(args[1])).launch();
}
public SimpleRDFClient(String host, int port) {
try {
socket = new Socket(host,port);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void launch() {
try {
OutputStream out = socket.getOutputStream();
BufferedOutputStream dos = new BufferedOutputStream(out);
Model model = new LinkedHashModel();
ValueFactory factory = new ValueFactoryImpl();
URI clive = factory.createURI("http://www.site.org/cliveAnderson");
Statement st = factory.createStatement(clive, RDF.TYPE, FOAF.PERSON);
model.add(st);
Rio.write(model,dos,RDFFormat.RDFXML);
dos.flush();
//Some other stuff
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
和服务器处理程序:
import java.io.*;
import java.net.*;
import org.openrdf.rio.*;
import org.openrdf.rio.helpers.*;
import org.openrdf.model.*;
import org.openrdf.model.impl.*;
public class SimpleRDFSHandler implements Handler {
public void handleConnection(Socket socket) {
Model model = null;
try {
InputStream in = socket.getInputStream();
model = Rio.parse(in,"www.blah.com",RDFFormat.RDFXML);
for (Statement st: model) {
System.out.println(st);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
问题似乎来自 Rio.parse() 方法挂起(我认为是因为它不知道输入何时结束)。当我以类似的方式使用 Jena api 时,我遇到了类似的问题,即使用 Model.write(outputstream,format) 和 Model.read(inputstream,format) 而不是 Rio。我已经查看了源代码和 javadoc 多年但无法解决问题。我想这一定是我误解了一些简单的事情。有任何想法吗?