1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| package cn.com.lihuan.oicq; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.net.ServerSocket; import java.net.Socket; public class Test { public static void main(String[] args) throws IOException, Exception { // TODO Auto-generated method stub final SendClass sc = new SendClass(“李欢”); new Thread() { public void run() { ServerSocket ss = null; SendClass c = null; try { ss = new ServerSocket(12345); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); c = (SendClass)ois.readObject(); ss.close(); ois.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(c); } }.start(); new Thread(new Runnable() { public void run() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(bos); oos.writeObject(sc); oos.close(); Socket s = new Socket(“127.0.0.1”, 12345); OutputStream os = s.getOutputStream(); os.write(bos.toByteArray()); s.close(); os.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } } class SendClass implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String name; public SendClass() { } public SendClass(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return “SendClass [name=” + name + “]”; } }
|