这几天不知怎的,很没精神,一天培训上课十个小时,最近特显的吃不消,一到下午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();