2014-04-28 179 views
1

我写了一个runnable网络类,它在套接字上侦听并取消编组输入。它也可以使用编组对象写入套接字。出现问题是因为套接字保持打开状态(以便稍后允许客户端和主机之间的通信) - 这会导致输入流的解组停止。我已经尝试从发件人一方编写XMLStreamConstants.END_DOCUMENT,但这会导致错误解组而不是挂起。下面是一些对网络类代码:Unmarshalling挂在打开套接字上

@Override 
public void update(Observable o, Object arg) { 
    try { 
     if(!this.updatedByNetwork){ 
      OutputStream os = socket.getOutputStream(); 
      mh.marshal(this.gm.getBoard(), os); 
      os.flush(); 
     } 
    }catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JAXBException e) { 
     e.printStackTrace(); 
    } 
} 
@Override 
public void run() { 
    try { 
     if (this.ss != null){ 
      this.socket = this.ss.accept(); 
      this.update(this.gm, null); 
     } 
     while (true){ 
      try { 
       InputStream is = socket.getInputStream(); 
       Board b = mh.unmarshal(is); 
       this.updatedByNetwork = true; 
       this.gm.updateBoard(b); 
      } catch (SocketTimeoutException e){ 
       e.printStackTrace(); 
      } catch (JAXBException e) { 
       e.printStackTrace(); 
      } 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

下面的代码为我的马歇尔处理程序:

public Board unmarshal(InputStream in) throws JAXBException{ 
     Unmarshaller um = this.jc.createUnmarshaller(); 
     Board b = (Board) um.unmarshal(in); 
     return b; 
} 
public void marshal(Board b, OutputStream os) throws JAXBException { 
     Marshaller m = this.jc.createMarshaller(); 
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     m.marshal(b, os); 
} 

那么,有没有一种方法来表示文件的解组结束了吗?或者,有没有更好的方法来做到这一点?

回答

1

即使有一种方法可以向解组器发送“文件结束”信号,但当两个或更多个消息彼此直接发送时,解组器仍可能会读取下一条消息。为了防止这种情况的发生,网络协议层需要到位,从逻辑上将发送/接收的字节分隔成不同的消息。在下面的示例中,该“协议”在writeMsgreadMsg方法中实施。请注意,这是一个简单的例子,假设所有消息都可以在内存中完全处理。

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class NetworkMarshall { 

private static final int NumberOfMsgs = 2; 

public static void main(String[] args) { 

    Socket s = null; 
    try { 
     JAXBContext jc = JAXBContext.newInstance(NetworkMarshall.class); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 

     new Thread(new Receiver(unmarshaller)).start(); 
     // Wait for socket server to start 
     Thread.sleep(500); 
     s = new Socket(InetAddress.getLocalHost(), 54321); 
     DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 

     for (int i = 0; i < NumberOfMsgs; i++) { 
      NetworkMarshall msg = new NetworkMarshall(); 
      msg.setName("vanOekel" + i); 
      writeMsg(msg, marshaller, dos); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { s.close(); } catch (Exception ignored) {} 
    } 
} 

private static void writeMsg(NetworkMarshall msg, Marshaller marshaller, DataOutputStream dos) throws Exception { 

    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
    marshaller.marshal(msg, bout); 
    byte[] msgBytes = bout.toByteArray(); 
    System.out.println("Sending msg: " + new String(msgBytes)); 
    dos.writeInt(msgBytes.length); 
    dos.write(msgBytes); 
    dos.flush(); 
} 

private String name; 

public void setName(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

public String toString() { 
    return this.getClass().getName() + ": " + getName(); 
} 

static class Receiver implements Runnable { 

    final Unmarshaller unmarshaller; 

    public Receiver(Unmarshaller unmarshaller) { 
     this.unmarshaller = unmarshaller; 
    } 

    public void run() { 

     ServerSocket ss = null; 
     Socket s = null; 
     try { 
      s = (ss = new ServerSocket(54321)).accept(); 
      DataInputStream dis = new DataInputStream(s.getInputStream()); 
      for (int i = 0; i < NumberOfMsgs; i++) { 
       Object o = unmarshaller.unmarshal(readMsg(dis)); 
       System.out.println("Received message " + i + ": " + o); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { ss.close(); } catch (Exception ignored) {} 
      try { s.close(); } catch (Exception ignored) {} 
     } 
    } 

    private ByteArrayInputStream readMsg(DataInputStream dis) throws Exception { 

     int size = dis.readInt(); 
     byte[] ba = new byte[size]; 
     dis.readFully(ba); 
     return new ByteArrayInputStream(ba); 
    } 
} 
}