假设socket设置字体传输到客户端
1,开始希望使用对象传递:创建3个类,Client,Server,SetFont,最后一个类是用来设置字体。在Client设置字体时产生一个对象,调用对象的方法设置字体;在Server类中接收Client类传过来的设置字体对象。这个思路的关键是使用socket传递对象。我使用的是函数返回对象,没有实现。
if (e.getSource() == ft) {
setfont = new SetFont(this, "字体设置", true);
setfont.setVisible(true);
ta.setFont(setfont.myFont);
tf.setFont(setfont.myFont);
//client端部分代码 if (e.getSource() == ft) { setfont = new SetFont(this, "字体设置", true); setfont.setVisible(true); ta.setFont(setfont.myFont); tf.setFont(setfont.myFont); //使用函数返回对象 public SetFont getSetFont() { return setfont; } //server端实现代码 client A = new client(); A.setVisible(false); SetFont BB=A.getSetFont(); ta.setFont(BB.myFont); ta.setText(ta.getText() + str + "\n");
2,可以使用标签,在传输的文字头部使用一个flag标记颜色,在接收端读取flag并根据flag的值设置相应的颜色
(转载代码)
String []s1={"黑色","红色","蓝色"}; cb1.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ if(cb1.getSelectedItem().equals("红色")){ flag1=1; }else{ if(cb1.getSelectedItem().equals("蓝色")){ flag1=2; }else{ flag1=0; } } } }); b3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ e.getSource(); if(flag2==1){ try{ LineNumberReader br=new LineNumberReader(new FileReader("123.txt")); String str2; String str3=""; for(str2=br.readLine();str2!=null;str2=br.readLine()){ str3=str3+"\n"+str2; } DatagramSocket dataSocket = new DatagramSocket(10000); byte [] sendDataByte = new byte[1024]; sendDataByte=(("r"+str3).getBytes()); DatagramPacket dataPacket = new DatagramPacket(sendDataByte, sendDataByte.length, InetAddress.getByName("localhost"), 10002); dataSocket.send(dataPacket); dataSocket.close(); //System.out.print(str3); }catch(IOException e2){ e2.printStackTrace(); } flag2=0; }else{ try { DatagramSocket dataSocket = new DatagramSocket(10000); byte [] sendDataByte = new byte[1024]; if(flag1==0){ sendDataByte =("abc"+tb.getText()).getBytes(); insertDocument("你:"+tb.getText()+"\n", Color.BLACK); } else if(flag1==1){ sendDataByte =("bcd"+tb.getText()).getBytes(); insertDocument("你:"+tb.getText()+"\n", Color.RED); } else{ sendDataByte =("def"+tb.getText()).getBytes(); insertDocument("你:"+tb.getText()+"\n", Color.BLUE); } DatagramPacket dataPacket = new DatagramPacket(sendDataByte, sendDataByte.length, InetAddress.getByName("localhost"), 10002); dataSocket.send(dataPacket); dataSocket.close(); } catch (Exception er) { er.printStackTrace(); } tb.setText(""); } } }); JP2.add(b3); c.add(JP2); app.setVisible(true); try { DatagramSocket dataSocket = new DatagramSocket(10001); byte []receiveByte = new byte[1024]; DatagramPacket dataPacket = new DatagramPacket(receiveByte, receiveByte.length); int i=0; while (i == 0) { dataSocket.receive(dataPacket); i = dataPacket.getLength(); if (i > 0) { String receiveStr = null; String s=null; String ss=null; receiveStr = new String(receiveByte, 3, dataPacket.getLength()-3); s= new String(receiveByte, 0, 3); ss=new String(receiveByte, 0, 1); if(ss.equals("r")){ try{ String h=new String(receiveByte, 1, dataPacket.getLength()-1); FileWriter f=new FileWriter("asd.txt"); f.write(h); f.close(); } catch(IOException e1){ e1.printStackTrace(); } }else{ if(s.equals("abc")) insertDocument("朋友:"+receiveStr+"\n",Color.BLACK); else if(s.equals("bcd")) insertDocument("朋友:"+receiveStr+"\n",Color.RED); else insertDocument("朋友:"+receiveStr+"\n",Color.BLUE); i = 0; } } if(i==-1) dataSocket.close(); } } catch (Exception e) { e.printStackTrace(); } }
3,将信息进行RTF编码进行读写