本帖最后由 陈洋 于 2013-9-23 16:29 编辑
- //TCP在服务端上传一个文件,也就是客户端文件复制到服务端上
- 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("IPDemo.java"));
- PrintWriter out = new PrintWriter(s.getOutputStream(),true);
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- out.println(line);
- }//关闭客户端的输出流,相当于给流中加入一个结束标记-1。
- s.shutdownOutput();
- 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)
- {
- /*if("over".equals(line))要加入结束标记,要不然一直等待
- break;客户端的数据,关闭客户端的输出流与标记效果一样*/
- out.println(line);
- }
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("上传成功");
- out.close();
- s.close();
- ss.close();
- }
- }
复制代码 不知道是哪里有问题,看着老毕可以运行,而我就运行不起来。 |
|