读书人

JAVA技巧:一个方便使用javaSocket的插

发布时间: 2009-02-23 10:03:47 作者: liuhuituzi

这个插件包装了关于java Socket的一些比较常用的方法 使Socket用起来比较方便
  package sockettest;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.net.InetAddress;
  import java.net.UnknownHostException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import org.shouchan.socket.impl.Socket;
  /**
  *
  * @author SHOUCHAN
  */
  public class Main {
  /**
  * @param args the command line arguments
  */
  public static void main(String[] args) {
  System.out.println("对客户端文字发送:");
  //接受文字的服务器
  new Thread(new Runnable() {
  public void run() {
  Socket socket = new Socket(9000);
  int i = 0;
  while(i < 4){
  Socket sk = socket.bindSocket();
  server(sk);
  i++;
  }
  }
  }).start();
  //发送文字的客户端(4个客户端)
  Socket socket1 = null;
  Socket socket2 = null;
  Socket socket3 = null;
  Socket socket4 = null;
  try {
  socket1 = new Socket(InetAddress.getLocalHost(), 9000);
  socket1.sendValue("Hello World");
  System.out.println("socket1: " + socket1.getValue());
  socket2 = new Socket(InetAddress.getLocalHost(), 9000);
  socket2.sendValue("Hello World");
  System.out.println("socket2: " + socket2.getValue());
  socket3 = new Socket(InetAddress.getLocalHost(), 9000);
  socket3.sendValue("Hello World");
  System.out.println("socket3: " + socket3.getValue());
  socket1.sendValue("bye");
  System.out.println("socket1: " + socket1.getValue());
  socket4 = new Socket(InetAddress.getLocalHost(), 9000);
  socket4.sendValue("Hello World");
  System.out.println("socket4: " + socket4.getValue());
  socket2.sendValue("bye");
  System.out.println("socket2: " + socket2.getValue());
  socket4.sendValue("bye");
  System.out.println("socket4: " + socket4.getValue());
  socket3.sendValue("bye");
  System.out.println("socket3: " + socket3.getValue());
  } catch (UnknownHostException ex) {
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  } finally {
  socket1.close();
  socket2.close();
  socket3.close();
  socket4.close();
  }
  System.out.println("\n发送二进制文件");
  //发送二进制文件
  test1();
  System.out.println("\n直接发送文件");
  //直接发送文件
  test2();
  }
  public static void server(final Socket socket) {
  new Thread(new Runnable() {
  public void run() {
  while (true) {
  String value = socket.getValue();
  String host = socket.getSocket().getInetAddress().getHostName();
  int port = socket.getSocket().getPort();
  System.out.println(host + ":" + port + " - SAY: " + value);
  socket.sendValue("server-_ .........");
  if (value.equals("bye")) {
  socket.close();
  break;
  }
  }
  }
  }).start();
  }

//发送二进制文件
  public static void test1() {
  //服务端
  new Thread(new Runnable() {
  public void run() {
  Socket socket = new Socket(7000);
  byte[] v = socket.getFileValue(3);
  System.out.println(v[0] + "\n" + v[1] + "\n" + v[2]);
  socket.close();
  }
  }).start();
  //客户端
  try {
  Socket socket = new Socket(InetAddress.getLocalHost(), 7000);
  socket.sendFileValue(new byte[]{1, 1, 0});
  socket.close();
  } catch (UnknownHostException ex) {
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  }
  }
  //直接发送文件
  public static void test2() {
  //接受文件的服务器
  new Thread(new Runnable() {
  public void run() {
  Socket socket = new Socket(7505).bindSocket();
  java.io.DataInputStream in = socket.getFileStream();
  java.io.DataOutputStream out = null;
  try {
  out = new java.io.DataOutputStream(new java.io.FileOutputStream("G:/123.jpg"));
  } catch (FileNotFoundException ex) {
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  }
  int i = 0;
  try {
  while ((i = in.read()) != -1) {
  out.write(i);
  System.out.print(i);
  }
  } catch (IOException ex) {
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  } finally {
  try {
  out.flush();
  out.close();
  } catch (IOException ex) {
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  }
  socket.close();
  }
  }
  }).start();
  //发送文件的客户端
  try {
  Socket socket = new Socket(InetAddress.getLocalHost(), 7505);
  try {
  socket.sendFileStream(new java.io.DataInputStream(new java.io.FileInputStream("G:/106372200_2.jpg")));
  } catch (FileNotFoundException ex) {
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  }finally{
  socket.close();
  }
  } catch (UnknownHostException ex) {
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  }
  }
  }
  运行后显示:
  init:
  deps-jar:
  Compiling 1 source file to G:\java\SocketTest\build\classes
  compile:
  run:
  对客户端文字发送:
  SHOUCHAN-PC:50232 - SAY: Hello World
  socket1: server-_ .........
  SHOUCHAN-PC:50233 - SAY: Hello World
  socket2: server-_ .........
  SHOUCHAN-PC:50234 - SAY: Hello World
  socket3: server-_ .........
  SHOUCHAN-PC:50232 - SAY: bye
  socket1: server-_ .........
  SHOUCHAN-PC:50235 - SAY: Hello World
  socket4: server-_ .........
  SHOUCHAN-PC:50233 - SAY: bye
  socket2: server-_ .........
  SHOUCHAN-PC:50235 - SAY: bye
  socket4: server-_ .........
  SHOUCHAN-PC:50234 - SAY: bye
  socket3: server-_ .........
  成功生成(总时间:1 秒)


3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.net/exam/

读书人网 >复习指导

热点推荐