Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4042698
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: Java

2012-09-28 20:10:18

ClientTcpSend.java:

  1. import java.io.DataOutputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.net.InetSocketAddress;
  5. import java.net.Socket;

  6. public class ClientTcpSend {
  7.     public static void main(String[] args) {
  8.         int length = 0;
  9.         byte[] sendBytes = null;
  10.         Socket socket = null;
  11.         DataOutputStream dos = null;
  12.         FileInputStream fis = null;

  13.         try {
  14.             try {
  15.                 socket = new Socket();
  16.                 socket.connect(new InetSocketAddress("127.0.0.1", 33456),
  17.                                10 * 1000);
  18.                 dos = new DataOutputStream(socket.getOutputStream());
  19.                 File file = new File("/root/6674541037_c3a9c8b64c_b.jpg");
  20.                 fis = new FileInputStream(file);
  21.                 sendBytes = new byte[1024];
  22.                 while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
  23.                     dos.write(sendBytes, 0, length);
  24.                     dos.flush();
  25.                 }
  26.             } finally {
  27.                 if (dos != null)
  28.                     dos.close();
  29.                 if (fis != null)
  30.                     fis.close();
  31.                 if (socket != null)
  32.                     socket.close();
  33.             }
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38. }

ServerTcpListener.java:

  1. import java.net.*;
  2. import java.io.*;

  3. public class ServerTcpListener implements Runnable {
  4.     public static void main(String[] args) {
  5.         try {
  6.             final ServerSocket server = new ServerSocket(33456);
  7.             Thread th = new Thread(new Runnable() {
  8.                 public void run() {
  9.                     while (true) {
  10.                         try {
  11.                             System.out.println("开始监听...");
  12.                             Socket socket = server.accept();
  13.                             System.out.println("有链接");
  14.                             receiveFile(socket);
  15.                         } catch (Exception e) {
  16.                         }
  17.                     }
  18.                 }
  19.             });

  20.             th.run(); //启动线程运行
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }

  25.     public void run() {
  26.     }

  27.     public static void receiveFile(Socket socket) {
  28.         byte[] inputByte = null;
  29.         int length = 0;
  30.         DataInputStream dis = null;
  31.         FileOutputStream fos = null;
  32.         try {
  33.             try {
  34.                 dis = new DataInputStream(socket.getInputStream());
  35.                 fos = new FileOutputStream(new File("./cc.jpg"));
  36.                 inputByte = new byte[1024];
  37.                 System.out.println("开始接收数据...");
  38.                 while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
  39.                     System.out.println(length);
  40.                     fos.write(inputByte, 0, length);
  41.                     fos.flush();
  42.                 }
  43.                 System.out.println("完成接收");
  44.             } finally {
  45.                 if (fos != null)
  46.                     fos.close();
  47.                 if (dis != null)
  48.                     dis.close();
  49.                 if (socket != null)
  50.                     socket.close();
  51.             }
  52.         } catch (Exception e) {
  53.         }
  54.     }
  55. }


阅读(35014) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~