java 跟 xml 在使用socket通信的时候,突然有个想法,通过socket服务器向客户端输出的数据,以xml对象向外面输入,小第试了好多次,但在客户端就是接受不到数据,不知道有哪位前辈能帮帮我 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 其他的信息,我是说不用xml ,能收到吗,我觉得不是xml的问题 这个不就是最简单的Web Service的概念么 我贴出服务器的代码,客户断是使用flash的XMLSocket的onXML方法//TestServer.javapackage test;import java.net.*;import java.io.*;public class TestServer extends Thread // implements Runnable{ static final int DEFAULT_PORT =7000; static ServerSocket serverSocket; static PrintWriter pw=null; public static void main(String arg[]) { int port = DEFAULT_PORT; try { serverSocket = new ServerSocket(port); System.out.println("test服务器已经启动,正在监听......."); } catch (IOException e){ System.out.println(e.toString()); System.exit(1); } while (true){ try { Socket cs = serverSocket.accept(); System.out.println("监听成功......."); TestClientProc cp=new TestClientProc(cs); Thread ct = new Thread(cp); ct.start(); } catch (IOException e){ System.out.println(e.toString()); } } }}///TestClientProc.javapackage test;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.HttpURLConnection;import java.net.ProtocolException;import java.net.Socket;import java.net.URL;import org.jdom.Attribute;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;public class TestClientProc implements Runnable { Socket socket; BufferedReader in; PrintWriter out; String line=null; public TestClientProc(Socket socket) throws IOException { try { this.socket = socket; in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); } catch (IOException dd) { System.out.print(dd.toString()); } } public void run() { Document cml=this.XMLOutput(); this.ServerSenderDocument(cml); line = null; } public Document XMLOutput() { FileOutputStream fo = null; Document doc = null; Element DocElement = new Element("gamelist");// 设置建立文档所需要的一条元素 doc = new Document(DocElement); Element Root = null;// 定义第一层元素 boolean expandEntities = true; String saxDriverClass = null; Root= new Element("player"); Root.setAttribute(new Attribute("name","faimry")); DocElement.addContent(Root); try { SAXBuilder builder = null; if (saxDriverClass == null) { builder = new SAXBuilder(); } else { builder = new SAXBuilder(saxDriverClass); } builder.setExpandEntities(expandEntities); Format format = Format.getPrettyFormat(); format.setIndent(" "); format.setEncoding("utf-8"); XMLOutputter outp = new XMLOutputter(format); fo = new FileOutputStream("aa.xml"); outp.output(doc, fo); } catch(Exception ds){ System.out.println(ds.toString()); } return doc; } public void ServerSender(String msg) { String oum=msg+"\0"; System.out.println("自己:"+oum); out.print(oum); out.flush(); } public void ServerSenderDocument(Document msg) { Format format = Format.getPrettyFormat(); format.setIndent(""); format.setEncoding("UTF-8");// UTF-8编码输出 XMLOutputter outp = new XMLOutputter(format); OutputStream sa=null; try { sa = socket.getOutputStream(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { outp.output(msg, sa); out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 关于汽车启动的问题 求教checkbox和button关联的问题 请教一个基本问题 请问高手以下的初始化是否正确:int a = new int[10];float b = new float[10];Object c = new float[10]; 一个小程序,帮忙看看 如何在一个类里引用另一个类里的已经存在的实例? 如何获取IP包?高手请入! 请大家帮忙 用applet怎么实现显示html页面? 求救:SAXParserFactory saxFact = SAXParserFactory.newInstance();错误 java读取机器的IP地址 JAVA中的sql语句
我觉得不是xml的问题
//TestServer.java
package test;
import java.net.*;
import java.io.*;
public class TestServer extends Thread // implements Runnable
{
static final int DEFAULT_PORT =7000; static ServerSocket serverSocket;
static PrintWriter pw=null; public static void main(String arg[])
{
int port = DEFAULT_PORT;
try {
serverSocket = new ServerSocket(port);
System.out.println("test服务器已经启动,正在监听.......");
}
catch (IOException e){
System.out.println(e.toString());
System.exit(1);
}
while (true){
try {
Socket cs = serverSocket.accept();
System.out.println("监听成功.......");
TestClientProc cp=new TestClientProc(cs);
Thread ct = new Thread(cp);
ct.start();
}
catch (IOException e){
System.out.println(e.toString());
}
}
}
}///TestClientProc.javapackage test;import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.Socket;
import java.net.URL;import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;public class TestClientProc implements Runnable { Socket socket;
BufferedReader in;
PrintWriter out;
String line=null; public TestClientProc(Socket socket) throws IOException {
try {
this.socket = socket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
} catch (IOException dd) {
System.out.print(dd.toString());
}
} public void run()
{
Document cml=this.XMLOutput();
this.ServerSenderDocument(cml);
line = null;
} public Document XMLOutput()
{
FileOutputStream fo = null;
Document doc = null;
Element DocElement = new Element("gamelist");// 设置建立文档所需要的一条元素
doc = new Document(DocElement);
Element Root = null;// 定义第一层元素
boolean expandEntities = true;
String saxDriverClass = null;
Root= new Element("player");
Root.setAttribute(new Attribute("name","faimry"));
DocElement.addContent(Root);
try {
SAXBuilder builder = null;
if (saxDriverClass == null) {
builder = new SAXBuilder();
} else {
builder = new SAXBuilder(saxDriverClass);
}
builder.setExpandEntities(expandEntities);
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("utf-8");
XMLOutputter outp = new XMLOutputter(format);
fo = new FileOutputStream("aa.xml");
outp.output(doc, fo);
}
catch(Exception ds){
System.out.println(ds.toString());
}
return doc;
}
public void ServerSender(String msg)
{
String oum=msg+"\0";
System.out.println("自己:"+oum);
out.print(oum);
out.flush();
}
public void ServerSenderDocument(Document msg)
{
Format format = Format.getPrettyFormat();
format.setIndent("");
format.setEncoding("UTF-8");// UTF-8编码输出
XMLOutputter outp = new XMLOutputter(format);
OutputStream sa=null;
try {
sa = socket.getOutputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
outp.output(msg, sa);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}