本帖最后由 孙百鑫 于 2013-5-3 22:32 编辑
- import java.io.*;
- import java.net.*;
- class TextClient
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("192.168.1.254",10006);
- //字输入符流与文件关联
- BufferedReader bufr =
- new BufferedReader(new FileReader("TcpDemo.java"));
- //输出到服务器
- PrintWriter out = new PrintWriter(s.getOutputStream(),true);
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- out.println(line);
- }
- s.shutdownOutput();//关闭客户端的输出流。相当于给流中加入一个结束标记-1.
- //接收服务器返回的信息
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = bufIn.readLine();
- System.out.println(str);
- bufr.close();
- s.close();
- }
- }
- class TextServer
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10006);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+"....connected");
- //接收客户端发来的信息
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- //输出文件
- PrintWriter out = new PrintWriter(new FileWriter("Server.txt"),true);
- String line = null;
- while((line=bufIn.readLine())!=null)
- {
- out.println(line);
- }
-
- //都完成后告诉客户端上传成功
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("上传成功");
- //关闭
- out.close();
- s.close();
- ss.close();
- }
- }
复制代码 这个是复制文件.
可是输出什么的都正常.就是文件里面没有内容..看了好久还是没问题...
纠结...来帮我看看吧.
禽兽们我爱你 |