1:如何通过socket代理来访问服务端:
- String proxyHost = "192.168.204.212";
- String proxyPort = "1080";
- //通知Java要通过代理进行连接。
- System.getProperties().put("socksProxySet","true");
- //指定代理所在的机器
- System.getProperties().put("socksProxyHost",proxyHost);
- //指定代理监听的端口。
- System.getProperties().put("socksProxyPort",proxyPort);
- String host = "134.01.69.80";
- int port = 12086;
- System.out.println("connetioning:" + host + ":" + port);
- server = new Socket(host, port);
二:老socket传递Object对象:
要传递的对象:
- public class Employee implements Serializable {
- private int employeeNumber;
- private String employeeName;
- Employee(int num, String name) {
- employeeNumber = num;
- employeeName= name;
- }
- public int getEmployeeNumber() {
- return employeeNumber ;
- }
- public void setEmployeeNumber(int num) {
- employeeNumber = num;
- }
- public String getEmployeeName() {
- return employeeName ;
- }
- public void setEmployeeName(String name) {
- employeeName = name;
- }
- }
client:
- public class Client {
- public static void main(String[] arg) {
- try {
- Employee joe = new Employee(150, "Joe");
- System.out.println("employeeNumber= " + joe.getEmployeeNumber());
- System.out.println("employeeName= " + joe.getEmployeeName());
- Socket socketConnection = new Socket("127.0.0.1", 11111);
- ObjectOutputStream clientOutputStream = new ObjectOutputStream(
- socketConnection.getOutputStream());
- ObjectInputStream clientInputStream = new ObjectInputStream(
- socketConnection.getInputStream());
- clientOutputStream.writeObject(joe);
- joe = (Employee) clientInputStream.readObject();
- System.out.println("employeeNumber= " + joe.getEmployeeNumber());
- System.out.println("employeeName= " + joe.getEmployeeName());
- clientOutputStream.close();
- clientInputStream.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
server端:
java 代码
- public class Server {
- public static void main(String[] arg) {
- Employee employee = null;
- try {
- ServerSocket socketConnection = new ServerSocket(11111);
- System.out.println("Server Waiting");
- Socket pipe = socketConnection.accept();
- ObjectInputStream serverInputStream = new ObjectInputStream(pipe
- .getInputStream());
- ObjectOutputStream serverOutputStream = new ObjectOutputStream(pipe
- .getOutputStream());
- employee = (Employee) serverInputStream.readObject();
- employee.setEmployeeNumber(256);
- employee.setEmployeeName("li");
- serverOutputStream.writeObject(employee);
- serverInputStream.close();
- serverOutputStream.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
三:nio socket传递Object:
client:
- public class Client {
- private String hostname;
- private int port;
- public Client(String hostname, int port)
- {
- this.hostname = hostname;
- this.port = port;
- }
- public static void main(String[] args) {
- String hostname = "192.168.0.81";
- int port = 8234;
- Student stu = new Student();
- stu.setId(849);
- stu.setName("Squall");
- Client client = new Client(hostname, port);
- try {
- client.write(stu);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void write(Object obj) throws IOException {
- SocketChannel channel = null;
- try {
- channel = SocketChannel.open(new InetSocketAddress(hostname, port));
- ByteBuffer buf = Client.getByteBuffer(obj);
- channel.write(Client.getByteBuffer(obj));
- channel.write(Client.getByteBuffer(obj));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- channel.close();
- }
- }
- public static ByteBuffer getByteBuffer(Object obj) throws IOException
- {
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(bOut);
- out.writeObject(obj);
- out.flush();
- byte[] arr = bOut.toByteArray();
- System.out.println("Object in " + arr.length + " bytes");
- ByteBuffer bb = ByteBuffer.wrap(arr);
- out.close();
- return bb;
- }
- }
server端:
java 代码
- public class Server {
- public static void main(String[] args) {
- System.out.println("in server!");
- ServerThread server = new ServerThread();
- new Thread(server).start();
- }
- static class ServerThread implements Runnable {
- public void run() {
- try {
- ServerSocketChannel sc = ServerSocketChannel.open();
- ServerSocket s = sc.socket();
- s.bind(new InetSocketAddress(8234));
- while (true) {
- Socket incoming = s.accept();
- Runnable r = new GetObjThread(incoming);
- Thread t = new Thread(r);
- t.start();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- static class GetObjThread implements Runnable {
- public GetObjThread(Socket s) {
- incoming = s;
- }
- public void run() {
- try {
- SocketChannel sc = incoming.getChannel();
- ByteBuffer bbIn = ByteBuffer.allocate(1024);
- sc.read(bbIn);
- sc.close();
- bbIn.flip();
- ByteArrayInputStream bIn = new ByteArrayInputStream(bbIn
- .array());
- ObjectInputStream in = new ObjectInputStream(bIn);
- Student nStu = (Student) in.readObject();
- System.out.println("student id is " + nStu.getId() + "/n"
- + "student name is " + nStu.getName());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private Socket incoming;
- }
- }
四:备份一个有用的util class:对象序列化,反序列化(序列化对象转byte[],ByteBuffer, byte[]转object:
java 代码
- public class ByteUtil {
- public static byte[] getBytes(Object obj) throws IOException
- {
- ByteArrayOutputStream bout = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(bout);
- out.writeObject(obj);
- out.flush();
- byte[] bytes = bout.toByteArray();
- bout.close();
- out.close();
- return bytes;
- }
- public static Object getObject(byte[] bytes) throws IOException, ClassNotFoundException
- {
- ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
- ObjectInputStream oi = new ObjectInputStream(bi);
- Object obj = oi.readObject();
- bi.close();
- oi.close();
- return obj;
- }
- public static ByteBuffer getByteBuffer(Object obj) throws IOException
- {
- byte[] bytes = ByteUtil.getBytes(obj);
- ByteBuffer buff = ByteBuffer.wrap(bytes);
- return buff;
- }
- }
五:如何通过xml传递Object对象:
可以先把object转成一个byte[]数组,然后用base64编码成一个base64格式的String,放入xml的CDATA中,就可以传了。
接收方,收到该xml后,把CDATA中的String用base64解码为byte[],进而根据四中的方法,还原为object:
java 代码
- public class Base64 {
- public static String getEncodedText(byte[] bytes) {
- try {
- BASE64Encoder encoder = new BASE64Encoder();
- String text = encoder.encode(bytes);
- return text;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public static byte[] decode(String src)
- {
- BASE64Decoder decoder = new BASE64Decoder();
- try {
- return decoder.decodeBuffer(src);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }
- }
- public static void main(String[] args) {
- String s = "ly89";
- byte[] bytes = s.getBytes();
- String encode = Base64.getEncodedText(bytes);
- System.out.println("the encode string is: " + encode);
- byte[] dbytes = Base64.decode(encode);
- for (int i = 0; i < bytes.length; i++) {
- System.out.println(dbytes[i]);
- }
- }
- }