跟小D每日学口语

WhiteSpirit

宁愿跑起来被拌倒无数次,也不愿规规矩矩走一辈子。就算跌倒也要豪迈的笑!

导航

学习感言

  这几天不知怎的,很没精神,一天培训上课十个小时,最近特显的吃不消,一到下午3点,疲惫不堪,精神变的恍惚,不想听课。现在非常喜欢安静的环境,喜欢一个人独自待着,思考下人生规划,怎么在几年内达到自己心中的目标是我想的最多的一个问题,再回忆去年的美好时光,也会傻傻的笑。不知不觉已经和她分开一个多月了,时不时的想起也能影响到我的心情,明知道不能把自己弄的这么伤感,可做不到,到处充满她的影子,令我不得不回忆她。看着她心情,知道她每天过的很开心,这也是对我最好的安慰,有时想,罢了,如果她过的好过的开心,我知足了,才发现还是这么深深着爱着.........默默的也好。

     嘿,有些八婆了,纯当写在这里当作发泄吧,进入正题,发现自己对流这块了解不够,所以晚上给自己一个目标,做一个基于简单的TCP协议文件发送类,一个客户端类:ClientTCPSent 一个服务器端类:ServerTCPRecive ,拿着JDK API看了半天,终于理清了思路 ,以后在学习的过程中都把代码发到BLOG中,方便以后自己查阅。

服务端代码:

 1 import java.io.DataInputStream;
2 import java.io.File;
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.net.ServerSocket;
7 import java.net.Socket;
8
9
10 public class ServerTCPRecive {
11 byte[] recivebyte =null;
12 Socket s = null;
13 ServerSocket ss =null;
14 DataInputStream dis = null;
15 FileOutputStream fos = null;
16
17 //初始化变量
18 public ServerTCPRecive(String savename){
19 recivebyte = new byte[1024];
20 try {
21 ss = new ServerSocket(9999);
22 System.out.println("服务器启动成功.......");
23 //开始监听
24 s = ss.accept();
25 System.out.println("IP:"+s.getInetAddress().getHostAddress()+"连接成功");
26 dis = new DataInputStream(s.getInputStream());
27 fos = new FileOutputStream(new File(savename));
28
29 } catch (FileNotFoundException e) {
30 e.printStackTrace();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35
36 //接收文件
37 public void reciveFile(){
38 System.out.println("开始接收文件.....");
39 int result=0;
40
41 try {
42 while((result =dis.read(recivebyte,0,recivebyte.length))>0){
43 fos.write(recivebyte);
44 fos.flush();
45 }
46 } catch (IOException e) {
47 e.printStackTrace();
48 }
49 //关闭所有流
50 closeAll();
51
52 }
53
54 //关闭所有流
55 public void closeAll(){
56 try {
57 dis.close();
58 } catch (IOException e2) {
59 e2.printStackTrace();
60 }
61 try {
62 fos.close();
63 } catch (IOException e1) {
64 e1.printStackTrace();
65 }
66 try {
67 s.close();
68 } catch (IOException e) {
69 e.printStackTrace();
70 }
71 try {
72 ss.close();
73 } catch (IOException e) {
74 e.printStackTrace();
75 }
76 }
77
78 //主函数
79 public static void main(String[] args) {
80 //参数是要传送的文件
81 ServerTCPRecive s = new ServerTCPRecive("C:/Documents and Settings/Administrator/桌面/recive.jpg");
82 s.reciveFile();
83 }
84
85 }

客户端代码:

 1 import java.io.DataOutputStream;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.net.Socket;
7 import java.net.UnknownHostException;
8
9
10 public class ClientTCPSend {
11 byte[] sendbyte =null;
12 Socket s = null;
13 DataOutputStream dos = null;
14 FileInputStream fis = null;
15
16 public ClientTCPSend(String filename){
17 sendbyte = new byte[1024];
18 try {
19 s = new Socket("127.0.0.1",9999);
20
21 System.out.println("服务器连接成功.....");
22 dos = new DataOutputStream(s.getOutputStream());
23 fis = new FileInputStream(new File(filename));
24 } catch (UnknownHostException e) {
25 e.printStackTrace();
26 } catch (FileNotFoundException e) {
27 e.printStackTrace();
28 } catch (IOException e) {
29 e.printStackTrace();
30 }
31 }
32
33 //发送文件
34 public void SendFile(){
35 System.out.println("开始传输文件....");
36 int result=0;
37 try {
38 while((result = fis.read(sendbyte,0,sendbyte.length))>0){
39 dos.write(sendbyte);
40 dos.flush();
41 }
42 } catch (IOException e) {
43 e.printStackTrace();
44 }
45 closeAll();
46 }
47
48 //关闭所有流
49 public void closeAll(){
50 try {
51 fis.close();
52 } catch (IOException e2) {
53 e2.printStackTrace();
54 }
55 try {
56 dos.close();
57 } catch (IOException e1) {
58 e1.printStackTrace();
59 }
60 try {
61 s.close();
62 } catch (IOException e) {
63 e.printStackTrace();
64 }
65
66 }
67
68
69
70 public static void main(String[] args) {
71 ClientTCPSend c = new ClientTCPSend("C:/Documents and Settings/Administrator/桌面/send.jpg");
72 c.SendFile();
73
74
75 }
76
77 }

posted on 2011-08-19 21:36  Ai:C  阅读(369)  评论(4)    收藏  举报