欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java UDP打印

程序员文章站 2022-04-10 08:42:00
...
采用UDP的通讯方式,调用java自带的awt打印接口

服务器端代码

Server:
# import java.io.IOException;  
# import java.net.DatagramPacket;  
# import java.net.DatagramSocket;  
# import java.net.SocketException;  
#   
# public class Server  
# {  
#     private DatagramSocket server;  
#       
#     private DatagramPacket packet;  
#       
#     private byte[] b;  
#       
#     public Server()  
#     {  
#         startServer();  
#     }  
#       
#     private void startServer()  
#     {  
#         try  
#         {  
#             server = new DatagramSocket(12345);  
#   
#             while(true)  
#             {  
#                 b = new byte[1024];  
#   
#                 packet = new DatagramPacket(b, b.length);  
#   
#                 server.receive(packet);  
#                   
#                 new Thread(new PrintThread(packet, b)).start();  
#             }  
#               
#         }catch(SocketException e)  
#         {  
#             e.printStackTrace();  
#         }catch(IOException e)  
#         {  
#             e.printStackTrace();  
#         }finally  
#         {  
#             server.close();  
#         }  
#     }  
#   
#     public static void main(String[] args)  
#     {  
#         new Server();  
#     }  
# }


打印线程代码

PrintThread
# import java.net.DatagramPacket;  
#   
# public class PrintThread implements Runnable  
# {  
#     private DatagramPacket packet;  
#   
#     private byte[] b;  
#   
#     public PrintThread(DatagramPacket packet, byte[] b)  
#     {  
#         this.packet = packet;  
#         this.b = b;  
#     }  
#   
#     public void run()  
#     {  
#         int i = 0;  
#   
#         i = packet.getLength();  
#   
#         if(i>0)  
#         {  
#             String str = new String(b, 0, i);  
#             new Print().doPrint(str);  
#         }  
#     }  
# }


打印的实际代码

Print:
# import java.awt.Color;  
# import java.awt.Font;  
# import java.awt.Graphics;  
# import java.awt.Graphics2D;  
# import java.awt.Paint;  
# import java.awt.print.PageFormat;  
# import java.awt.print.Printable;  
# import java.awt.print.PrinterException;  
#   
# import javax.print.Doc;  
# import javax.print.DocFlavor;  
# import javax.print.DocPrintJob;  
# import javax.print.PrintException;  
# import javax.print.PrintService;  
# import javax.print.PrintServiceLookup;  
# import javax.print.SimpleDoc;  
# import javax.print.attribute.DocAttributeSet;  
# import javax.print.attribute.HashDocAttributeSet;  
# import javax.print.attribute.standard.MediaSizeName;  
# import javax.swing.JOptionPane;  
#   
# public class Print implements Printable {  
#       
#     private String content = null;  
#       
#     public void setContent(String content) {  
#         this.content = content;  
#     }  
#   
#     public void doPrint(String content) {  
#         if (null == content || content.equals("")) {  
#             Object message = new String("Sorry, Printer Job is Empty, Print Cancelled!");  
#             String title = "Empty";  
#             JOptionPane.showConfirmDialog(null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);  
#         } else {  
#             setContent(content);  
#             PrintService printService = PrintServiceLookup.lookupDefaultPrintService();  
#             DocPrintJob docPrintJob = printService.createPrintJob();  
#             DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;  
#             DocAttributeSet attributes = new HashDocAttributeSet();  
#             attributes.add(MediaSizeName.A);  
#             Doc doc = new SimpleDoc(this, flavor, attributes);  
#             try {  
#                 docPrintJob.print(doc, null);  
#             } catch (PrintException e) {  
#                 // TODO Auto-generated catch block  
#                 e.printStackTrace();  
#             }  
#         }  
#     }  
#   
#     public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)  
#             throws PrinterException {  
#         // TODO Auto-generated method stub  
#         if (pageIndex >= 1) {  
#             return Printable.NO_SUCH_PAGE;  
#         } else {  
#             Graphics2D graphics2D = (Graphics2D) graphics;  
#             graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());  
#             Paint paint = Color.black;  
#             graphics2D.setPaint(paint);  
#             Font font = new Font("宋体", Font.PLAIN, 10);  
#             graphics2D.setFont(font);  
#             float x = 0.0f;  
#             float y = font.getSize2D();  
#   
#             String[] array = content.split("@");  
#             String room_idx = array[0];  
#             String content = array[1];  
#             String price = array[2];  
#             String unit = array[3];  
#             String time = array[4];  
#   
#             price = price + "元/" + unit;  
#   
#             graphics2D.drawString("网络视频酒店信息服务项目", x, 1*y);  
#             graphics2D.drawString("--------------------------------", x, 2*y);  
#             graphics2D.drawString("房间号:    " + room_idx, x, 3*y);  
#             graphics2D.drawString("消费内容:  " + content, x, 4*y);  
#             graphics2D.drawString("消费金额:  " + price, x, 5*y);  
#             graphics2D.drawString("消费时间:  " + time, x, 6*y);  
#         }  
#         return Printable.PAGE_EXISTS;  
#     }  
#   
# }


客户端代码

Client:
# import java.io.IOException;  
# import java.net.DatagramPacket;  
# import java.net.DatagramSocket;  
# import java.net.InetSocketAddress;  
# import java.net.SocketException;  
# import java.text.SimpleDateFormat;  
# import java.util.Date;  
#   
# public class Client  
# {  
#     private DatagramSocket client;  
#       
#     private DatagramPacket packet;  
#       
#     private InetSocketAddress address;  
#       
#     private byte[] b;  
#   
#     public void doPrint(String str)  
#     {  
#         try  
#         {  
#             client = new DatagramSocket();  
#               
#             address = new InetSocketAddress("127.0.0.1", 12345);  
#   
#             SimpleDateFormat spl = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
#   
#             str = str + "@" + spl.format(new Date());  
#   
#             b = str.getBytes();  
#   
#             packet = new DatagramPacket(b, b.length, address);  
#   
#             client.send(packet);  
#         } catch (SocketException e)  
#         {  
#             e.printStackTrace();  
#         } catch (IOException e)  
#         {  
#             e.printStackTrace();  
#         } finally  
#         {  
#             client.close();  
#         }  
#     }  
#   
#     public static void main(String[] args)  
#     {  
#         for(int i = 0; i<5; i++)  
#         {  
#             int a = i;  
#             String str = a + "@" + (a+1) + "@" + (a+2) + "@天";  
#             new Client().doPrint(str);  
#         }  
#     }  
# }