ExServer.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:
J2ME
开发平台:
Java
- package exframework;
- /**
- * <p>Title: ExFramework</p>
- *
- * <p>Description: lizhenpeng</p>
- *
- * <p>Copyright: Copyright (c) 2005</p>
- *
- * <p>Company: LP&P</p>
- *
- * @author lipeng
- * @version 1.0
- */
- import javax.microedition.io.*;
- import java.io.*;
- import javax.microedition.lcdui.*;
- public class ExServer implements AllAction
- {
- ServerSocketConnection server;
- SocketConnection client;
- byte []readBuffer;
- byte []writeBuffer;
- public ExServer()
- {
- readBuffer = new byte[4];
- }
- public void allAction(MainForm form)
- {
- try
- {
- server=(ServerSocketConnection)Connector.open("socket://:8070");
- client=(SocketConnection)server.acceptAndOpen();
- client.setSocketOption(SocketConnection.DELAY,0); //是否开启Nagle算法
- client.setSocketOption(SocketConnection.LINGER,5); //设置服务器的悬挂等待时间
- client.setSocketOption(SocketConnection.KEEPALIVE,0);
- client.setSocketOption(SocketConnection.RCVBUF,1024); //设置接收缓冲
- client.setSocketOption(SocketConnection.SNDBUF,1024); //设置发送缓冲
- InputStream is=client.openInputStream();
- OutputStream os=client.openOutputStream();
- is.read(readBuffer,0,readBuffer.length);
- InputStream imageIn=getClass().getResourceAsStream("/bg.png");
- int imageSize = imageIn.available();
- byte[]imageBuffer = new byte[imageSize];
- imageIn.read(imageBuffer,0,imageSize);
- ByteArrayOutputStream bos=new ByteArrayOutputStream();
- DataOutputStream dos = new DataOutputStream(bos);
- dos.writeInt(imageSize);
- dos.flush();
- writeBuffer = bos.toByteArray();
- os.write(writeBuffer,0,writeBuffer.length);
- os.write(imageBuffer,0,imageSize);
- os.flush();
- }
- catch(Exception e)
- {
- System.out.print(e);
- }
- }
- }