📄 receiver_01.java
字号:
package ece544.p1.zbwu;import java.io.IOException;import java.io.OutputStream;import ece544.p1.IReceiver;import edu.rutgers.winlab.labloma.net.*;/** * Jan 28, 2003 * This is for implementation of IReceiver, the * receiver is going to recieved fix-length packets of 1024 * while the last 4 bytes is an length indicator. * Thus, 1020 data bytes + 4 length indicator * @author Zhibin Wu */public class Receiver implements IReceiver { /** * the parameter addr is not used, * define private member varibles here * out is the buffer to store the packet , then the data in * the packet will be send to the outputstream after length indicator * is evaluated */ private INetworkPort defaultport; private byte[] out; public Receiver( INetworkPort port, IAddress addr ) { defaultport = port ; out = new byte[1024]; } /** * @see ece544.p1.IReceiver#receive(OutputStream) * * The method is simple, reading a packet of 1024, see last 4 bytes to * parse the length as an integer "len_rcvd", * then sending the number of bytes as data to outputstream * */ public void receive( OutputStream stream ) throws IOException { int i; int len_rcvd; do { defaultport.recv(out); String s = new String(out,1020, 4); len_rcvd = Integer.parseInt(s); stream.write (out,0,len_rcvd); } while (defaultport.hasMore()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -