当前位置:首页 > android - OPhone实现J2ME API之GCF通信
{ {
}
return datagramSocket.getLocalAddress().toString();
@Override
public int getLocalPort() throws IOException { }
@Override
public int getMaximumLength() throws IOException { }
@Override
public int getNominalLength() throws IOException { }
@Override
public Datagram newDatagram(int size) throws IOException { }
@Override
public Datagram newDatagram(int size, String addr) throws IOException }
@Override
public Datagram newDatagram(byte[] buf, int size) throws IOException }
@Override
public Datagram newDatagram(byte[] buf, int size, String addr) }
@Override
throws IOException { maxSize = buf.length; normalSize = size;
return new DatagramImpl(buf, size, addr); maxSize = buf.length; normalSize = size;
return new DatagramImpl(buf, size, isServer ? null : maxSize = normalSize = size;
return new DatagramImpl(size, addr); maxSize = normalSize = size;
return new DatagramImpl(size, isServer ? null : this.address); return normalSize; return maxSize;
return datagramSocket.getPort();
this.address);
}
public void receive(Datagram dgram) throws IOException { }
@Override
public void send(Datagram dgram) throws IOException { }
@Override
public void close() throws IOException { }
datagramSocket.close();
datagramSocket.send(((DatagramImpl) dgram).dgPacket); datagramSocket.receive(((DatagramImpl) dgram).dgPacket);
DatagramImpl实现Datagram接口拥有封装UDP数据包,这里你也仅需使用标准的J2SE API DatagramPacket类即可。
package javax.microedition.io; import java.net.DatagramPacket; //import…
public class DatagramImpl implements Datagram {
private String host; private int port; private String address;
public DatagramImpl(int size, String addr) throws IOException { }
public DatagramImpl(byte[] buf, int size, String addr) throws
if (size < 0 || size > buf.length) { }
this.maxSize = buf.length;
throw new IllegalArgumentException();
dgPacket = new DatagramPacket(new byte[size], size); this.maxSize = size; this.setAddress(addr);
baos = new ByteArrayOutputStream(size); dos = new DataOutputStream(baos); DatagramPacket dgPacket; private DataInputStream dis; private DataOutputStream dos; private ByteArrayOutputStream baos;
private final String DG_HEAD = \private int maxSize;
IOException {
}
dgPacket = new DatagramPacket(buf, size); this.setAddress(addr);
baos = new ByteArrayOutputStream(buf.length); dos = new DataOutputStream(baos); dos.write(buf, 0, size);
@Override
public String getAddress() { }
@Override
public byte[] getData() { }
@Override
public int getLength() { }
@Override
public int getOffset() { }
@Override
public void reset() { }
@Override
public void setAddress(String addr) throws IOException {
if (addr == null)
return; baos.reset(); try {
dis.reset();
return dgPacket.getOffset(); return dgPacket.getLength(); return dgPacket.getData(); return this.address;
} catch (IOException e) { }
address = addr; String[] strs = null;
if (addr.startsWith(DG_HEAD)) {
}
int headLen = DG_HEAD.length();
strs = addr.substring(headLen).split(\strs = addr.split(\
} else {
if (strs[0].equals(\|| strs[0].equals(\
strs[0] = \
}
host = strs[0];
port = Integer.parseInt(strs[1]);
InetAddress inetAddr = InetAddress.getByName(host); this.dgPacket.setAddress(inetAddr); dgPacket.setPort(port);
}
@Override
public void setAddress(Datagram reference) { try { setAddress(reference.getAddress()); } catch (IOException e) {
e.printStackTrace(); }
}
@Override
public void setData(byte[] buffer, int offset, int len) { baos.reset();
baos.write(buffer, offset, len);
this.dgPacket.setData(buffer, offset, len); }
@Override
public void setLength(int len) { if (len > this.maxSize) { throw new IllegalArgumentException(); }
byte[] buffer = baos.toByteArray(); baos.reset();
baos.write(buffer, 0, len); this.dgPacket.setLength(len);
}
private void checkDis() throws IOException {
if (dis == null) {
{
共分享92篇相关文档