ExServer.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. package exframework;
  2. /**
  3.  * <p>Title: ExFramework</p>
  4.  *
  5.  * <p>Description: lizhenpeng</p>
  6.  *
  7.  * <p>Copyright: Copyright (c) 2005</p>
  8.  *
  9.  * <p>Company: LP&P</p>
  10.  *
  11.  * @author lipeng
  12.  * @version 1.0
  13.  */
  14. import javax.microedition.io.*;
  15. import java.io.*;
  16. import javax.microedition.lcdui.*;
  17. public class ExServer implements AllAction
  18. {
  19.   ServerSocketConnection server;
  20.   SocketConnection client;
  21.   byte []readBuffer;
  22.   byte []writeBuffer;
  23.   public ExServer()
  24.   {
  25.     readBuffer = new byte[4];
  26.   }
  27.   public void allAction(MainForm form)
  28.   {
  29.     try
  30.     {
  31.       server=(ServerSocketConnection)Connector.open("socket://:8070");
  32.       client=(SocketConnection)server.acceptAndOpen();
  33.       client.setSocketOption(SocketConnection.DELAY,0); //是否开启Nagle算法
  34.       client.setSocketOption(SocketConnection.LINGER,5); //设置服务器的悬挂等待时间
  35.       client.setSocketOption(SocketConnection.KEEPALIVE,0);
  36.       client.setSocketOption(SocketConnection.RCVBUF,1024); //设置接收缓冲
  37.       client.setSocketOption(SocketConnection.SNDBUF,1024); //设置发送缓冲
  38.       InputStream is=client.openInputStream();
  39.       OutputStream os=client.openOutputStream();
  40.       is.read(readBuffer,0,readBuffer.length);
  41.       InputStream imageIn=getClass().getResourceAsStream("/bg.png");
  42.       int imageSize = imageIn.available();
  43.       byte[]imageBuffer = new byte[imageSize];
  44.       imageIn.read(imageBuffer,0,imageSize);
  45.       ByteArrayOutputStream bos=new ByteArrayOutputStream();
  46.       DataOutputStream dos = new DataOutputStream(bos);
  47.       dos.writeInt(imageSize);
  48.       dos.flush();
  49.       writeBuffer = bos.toByteArray();
  50.       os.write(writeBuffer,0,writeBuffer.length);
  51.       os.write(imageBuffer,0,imageSize);
  52.       os.flush();
  53.     }
  54.     catch(Exception e)
  55.     {
  56.       System.out.print(e);
  57.     }
  58.   }
  59. }