public class SocketServer {
public SocketServer(){
Socket incoming;
ServerSocket so;
try{
so= new ServerSocket(9731);
System.out.println("等待客户端连接");
while(true){
try{
incoming = so.accept( );
System.out.println("已连接客户端");
GetInfo gi=new GetInfo(incoming);
} catch (IOException e){
so.close();
e.printStackTrace();
}
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){ PropertyConfigurator.configure("E:\\Myeclipse\\workspace\\oufeitest\\src\\oufei\\test\\log4j.properties");
new SocketServer();
}
private static class GetInfo implements Runnable{ //�߳���
private Socket incoming;
private String s=null;
private BufferedReader b;
Thread t; public GetInfo(Socket incoming){
try{
this.incoming=incoming;
b=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
t=new Thread(this);
t.start();
}catch(IOException e){
e.printStackTrace();
}
}
public void run(){
try{
Logger logger=Logger.getLogger(SocketServer.class);
String lines;
String line="";
while ((lines = b.readLine()) != null){
line+=lines;
}
System.out.println("line----"+line);
System.out.println(line.length());
List<String> list1 = new ArrayList<String>();
for(int i=0;i<line.length();i+=63){
list1.add(line.substring(i,i+63));
}
Iterator list=list1.iterator();
while(list.hasNext()){
String co="";
co=(String) list.next();
logger.info("后台socket发送给我的内容:"+co);
String post_url="http://esales1.ofcard.com:8088/onlineorder.do";
String content=ConcatPackage.getPackage(co);
//用post方式发送http请求
String xmlcontent="";////
try {
xmlcontent=Sender.readContentFromPost(post_url, content);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("不好啦,发送http请求出问题啦,哦也!");
e.printStackTrace();
}
System.out.println("查看取得的xml字符串是否为空:"+xmlcontent);
// String xmlc=xmlcontent.substring(0, 40);
// System.out.println(xmlc);
System.out.println("发送欧非任务已完成");
//处理接收xml,并以socket发送到交换平台
// Getxml getxml = new Getxml();
// String xmlcontent = getxml.getxmlcontent(); Decodexml decoderxml = new Decodexml();
String packagesend = decoderxml.xmlElements(xmlcontent); //xml字符串被解析并被拼装为package
try {
System.out.println("组装的发送后台的SOCKET为:"+"["+packagesend+"]");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("发送给后台的socket失败!");
e.printStackTrace();
}
}
}catch(IOException e){
e.printStackTrace();
} }
}
}
以上代码我测试过可以接收到客服端的请求,但是我如何同步返回经过我处理后的信息呢????求高手指点,小弟在线等
public SocketServer(){
Socket incoming;
ServerSocket so;
try{
so= new ServerSocket(9731);
System.out.println("等待客户端连接");
while(true){
try{
incoming = so.accept( );
System.out.println("已连接客户端");
GetInfo gi=new GetInfo(incoming);
} catch (IOException e){
so.close();
e.printStackTrace();
}
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){ PropertyConfigurator.configure("E:\\Myeclipse\\workspace\\oufeitest\\src\\oufei\\test\\log4j.properties");
new SocketServer();
}
private static class GetInfo implements Runnable{ //�߳���
private Socket incoming;
private String s=null;
private BufferedReader b;
Thread t; public GetInfo(Socket incoming){
try{
this.incoming=incoming;
b=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
t=new Thread(this);
t.start();
}catch(IOException e){
e.printStackTrace();
}
}
public void run(){
try{
Logger logger=Logger.getLogger(SocketServer.class);
String lines;
String line="";
while ((lines = b.readLine()) != null){
line+=lines;
}
System.out.println("line----"+line);
System.out.println(line.length());
List<String> list1 = new ArrayList<String>();
for(int i=0;i<line.length();i+=63){
list1.add(line.substring(i,i+63));
}
Iterator list=list1.iterator();
while(list.hasNext()){
String co="";
co=(String) list.next();
logger.info("后台socket发送给我的内容:"+co);
String post_url="http://esales1.ofcard.com:8088/onlineorder.do";
String content=ConcatPackage.getPackage(co);
//用post方式发送http请求
String xmlcontent="";////
try {
xmlcontent=Sender.readContentFromPost(post_url, content);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("不好啦,发送http请求出问题啦,哦也!");
e.printStackTrace();
}
System.out.println("查看取得的xml字符串是否为空:"+xmlcontent);
// String xmlc=xmlcontent.substring(0, 40);
// System.out.println(xmlc);
System.out.println("发送欧非任务已完成");
//处理接收xml,并以socket发送到交换平台
// Getxml getxml = new Getxml();
// String xmlcontent = getxml.getxmlcontent(); Decodexml decoderxml = new Decodexml();
String packagesend = decoderxml.xmlElements(xmlcontent); //xml字符串被解析并被拼装为package
try {
System.out.println("组装的发送后台的SOCKET为:"+"["+packagesend+"]");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("发送给后台的socket失败!");
e.printStackTrace();
}
}
}catch(IOException e){
e.printStackTrace();
} }
}
}
以上代码我测试过可以接收到客服端的请求,但是我如何同步返回经过我处理后的信息呢????求高手指点,小弟在线等
定义:
private BufferedWriter w=null;
初始化:
w=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
输出:
w.write(packagesend);
w.flush();