- 论坛徽章:
- 0
|
//CommServer.java
import com.sun.image.codec.jpeg.*;
import java.io.*;
//import javax.imageio.*;
import java.awt.image.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.sql.ResultSet;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
class MyFrame extends JFrame
{
private MyPanel panel=null;
public MyFrame()
{
panel=new MyPanel();
add(panel);
}
public void drawScr(BufferedImage bi)
{
panel.draw(bi);
}
private class MyPanel extends JPanel
{
private BufferedImage bi=null;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(bi,0,0,null);
}
public void draw(BufferedImage bi)
{
this.bi=bi;
this.updateUI();
}
}
}
/**
* 通信机服务器端程序
* @author lishoudi
* @version 1.0
*
*/
public class CommServer extends Thread
{
private final static int headLen=37;
private static ServerSocket ss=null; //主Socket
private static Socket s=null;
private static ServerSocket ress=null;//返回信息socket
private static Socket res=null;
private InputStream is=null;
private OutputStream os=null;
public MyFrame frame=null;
private static int count =0;
public CommServer()
{
}
/**
* 把字节数组保存为一个文件
* @EditTime
*/
public static File getFileFromBytes(byte[] b, String outputFile)
{
BufferedOutputStream stream = null;
File file = null;
try
{
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
return file;
}
public void run()
{
//显示图片GUI
// frame = new MyFrame();
// frame.setSize(500,300);
// frame.setTitle("connection "+count);
// frame.setVisible(true);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
is=s.getInputStream();
os=s.getOutputStream();
//authentication
os.write("Pls enter your private key and press enter:".getBytes());
byte [] cskey=new byte[100];
int len=is.read(cskey);//读取客户端输入的私钥
String strKey=new String(cskey,0,len);
System.out.println("The private key of current connection is :"+
strKey);
if(strKey.startsWith("stephen123456"))//认证通过
{
System.out.println("legal connector,authenticate pass!");
os.write((byte)1);
//将输入流转换为字节输出流
ByteArrayOutputStream baos=new ByteArrayOutputStream();
transform(is,baos);//???????????????????
byte [] datapacket=baos.toByteArray();
// System.out.println(buf.length); //34946
// System.out.println(buf[0]);
parsePacket(datapacket);//解析数据包并处理
}
else
{
System.out.println("Illegal connection");
os.write((byte)0);
return;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
// try
// {
// is.close();
// os.close();
// }
// catch(Exception ex)
// {
// ex.printStackTrace();
// }
//
}
public void parsePacket(byte [] datapacket)
{
//公共数据包前缀解析
byte [] baseId=new byte[30];
byte [] servType=new byte[2];
byte [] packetLen=new byte[4];
byte flag;
System.arraycopy(datapacket, 0, baseId, 0, baseId.length);
System.arraycopy(datapacket, 30, servType, 0, servType.length);
System.arraycopy(datapacket, 32, packetLen, 0, packetLen.length);
flag=datapacket[37];
System.out.println("The connection is from basestation:"+
new String(baseId,0,baseId.length));
int servId=(int)ByteConvert.ByteToShort(servType);
byte [] bxml=null;
//处理不同服务请求
switch(servId)
{
case 1:
bxml=new byte[datapacket.length-37];
System.arraycopy(datapacket, 37, bxml, 0, bxml.length);
try
{
ByteArrayInputStream bais=new ByteArrayInputStream(bxml);
DataInputStream dis=new DataInputStream(bais);
// File f=new File("data_10k.xml");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
// FileInputStream fis=new FileInputStream(f);
// DataInputStream dis=new DataInputStream(fis);
Document doc = builder.parse(dis);
NodeList nl = doc.getElementsByTagName("VALUE");
for (int i=0;i"+
"col[0]col[1]col[2]>"+
"col[3]col[4]col[5]col[6]"+
"col[7]col[8]"+
"");
byte [] bqRest = strPacket.getBytes();
// String strPacket=new String(bqRest,0,bqRest.length);
//DataPacket(String baseID,short servType,int pacLen,byte flag,byte [] bData)
byte [] bqRestPacket=new DataPacket(strPacket,(short)15,bqRest.length+headLen,(byte)1,bqRest).getBytes();
//socket:返回查询信息
ress=new ServerSocket(2226);
res=ress.accept();
os=res.getOutputStream();
sendPacket(bqRestPacket,os);//将查询结果返回给客户端
// os.flush();
//关闭数据库连接
dbc.close();
rs.close();
// for (int i=0;i
}
/**
* function:capsulate the byte array and send it to the
* outputstream of socket
* @param bqRest
* @param os
*/
public void sendPacket(byte [] bqRestPacket,OutputStream os)
{
try
{
ByteArrayInputStream bas=new ByteArrayInputStream(bqRestPacket);//imagePacket-->b3
DataInputStream ds=new DataInputStream(bas);
//加密 cypheredPacket()
//组装 assemblyPacket()
//创建网络输出流并提供数据包装器
OutputStream ops=new DataOutputStream(new BufferedOutputStream(os));
//创建文件读取缓冲区
byte[] buf=new byte[1024];
int num=ds.read(buf);
//输出
System.out.println(new String(buf,0,num));
while(num!=(-1))
{//是否读完文件
ops.write(buf,0,num);//把文件数据写出网络缓冲区
ops.flush();//刷新缓冲区把数据写往客户端 ???????????
num=ds.read(buf);//继续从文件中读取数据
}
// ops.close(); //关闭socket?
ds.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 保存接收到的图片文件
* @param imageData
*/
public void saveCaptureImage(byte [] imageData)
{
byte [] filename=new byte[51];
byte [] imageInfo =new byte[imageData.length-51];//保存图像
// byte [] showInfo = new byte[imageData.length-51];//显示图像
System.arraycopy(imageData, 0, filename, 0, filename.length);
System.arraycopy(imageData, 51, imageInfo, 0, imageInfo.length);
// System.arraycopy(imageData, 51, showInfo, 0, showInfo.length);
ByteArrayInputStream bais=new ByteArrayInputStream(imageInfo);
DataInputStream dis=new DataInputStream(bais);
// ByteArrayInputStream bais_show=new ByteArrayInputStream(showInfo);
// DataInputStream dis_show=new DataInputStream(bais_show);
try
{
//使用本地文件系统接受网络数据并存为新文件
String filepath=new String(filename,0,filename.length);
String fileDir=new String("F:\\image\\");
File file=new File(fileDir+filepath);
file.createNewFile();
//RandomAccessFile raf=new RandomAccessFile(file,"rw");
FileOutputStream fos=new FileOutputStream(file);
// BufferedOutputStream bos=new BufferedOutputStream(fos);
//创建缓冲区缓冲网络数据
byte[] buf1=new byte[1024];
int num=dis.read(buf1);
for(int i=0;i
//显示图片
// JPEGImageDecoder decoder=JPEGCodec.createJPEGDecoder(dis_show);
// BufferedImage bi=decoder.decodeAsBufferedImage();
// System.out.println( bi.getData().toString());
//
// if(bi!=null)
// {
// frame.drawScr(bi);
// System.out.println("drawScr.");
// }
// dis_show.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
ss= new ServerSocket(2227);
while(true)
{
System.out.println("服务器阻塞监听.....");
s=ss.accept();
count++;
System.out.println("This is num "+count+" connection!");
new Thread(new CommServer()).start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void transform(InputStream in,OutputStream out)
{
int c=0;
try
{
while((c=in.read())!=-1)
{
out.write(c);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 将输入流读取到字节数组
* @param s
* @return
*/
public static byte[] getByte(java.io.InputStream s)
{
byte[] buffer = new byte[0];
byte[] chunk = new byte[1024];
int count;
try{
while((count = s.read(chunk))>=0)
{
byte[] t = new byte[buffer.length+count];
System.arraycopy(buffer,0,t,0,buffer.length);
System.arraycopy(chunk,0,t,buffer.length,count);
buffer=t;
}
s.close();
}
catch(Exception e)
{
}
return buffer;
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93928/showart_1976408.html |
|