黑马程序员技术交流社区

标题: TCP上传文本文件DEMO [打印本页]

作者: sven556677    时间: 2015-8-21 17:48
标题: TCP上传文本文件DEMO
服务端
  1. package com.cn.test2;
  2. import java.io.*;
  3. import java.net.*;
  4. public class TCPFileServer {
  5.         public static void main(String[] args) throws Exception {
  6.                 ServerSocket ss=new ServerSocket(5246);
  7.                 Socket s=ss.accept();
  8.                 BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));//接收的
  9.                 PrintWriter pw=new PrintWriter(new FileWriter("TCPRcv.txt"),true);
  10.                 String line=null;
  11.                 while((line=br.readLine())!=null){
  12.                         pw.println(line);
  13.                 }
  14.                 s.close();
  15.                 ss.close();
  16.         }
  17. }
复制代码

客户端
  1. package com.cn.test2;
  2. import java.io.*;
  3. import java.net.*;
  4. public class TCPUpFileClient {
  5.         public static void main(String[] args) throws Exception {
  6.                 Socket s=new Socket(InetAddress.getByName("192.168.1.4"),5246);
  7.                 BufferedReader br=new BufferedReader(new FileReader("ForTCP.txt"));
  8.                 PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
  9.                 String line=null;
  10.                 while((line=br.readLine())!=null){
  11.                         pw.println(line);
  12.                        
  13.                 }
  14.                 s.close();
  15.         }
  16. }
复制代码

作者: 风华正茂    时间: 2015-8-21 18:00
楼主写得不错,赞一个




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2