黑马程序员技术交流社区
标题:
TCP上传文本文件DEMO
[打印本页]
作者:
sven556677
时间:
2015-8-21 17:48
标题:
TCP上传文本文件DEMO
服务端
package com.cn.test2;
import java.io.*;
import java.net.*;
public class TCPFileServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(5246);
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));//接收的
PrintWriter pw=new PrintWriter(new FileWriter("TCPRcv.txt"),true);
String line=null;
while((line=br.readLine())!=null){
pw.println(line);
}
s.close();
ss.close();
}
}
复制代码
客户端
package com.cn.test2;
import java.io.*;
import java.net.*;
public class TCPUpFileClient {
public static void main(String[] args) throws Exception {
Socket s=new Socket(InetAddress.getByName("192.168.1.4"),5246);
BufferedReader br=new BufferedReader(new FileReader("ForTCP.txt"));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=br.readLine())!=null){
pw.println(line);
}
s.close();
}
}
复制代码
作者:
风华正茂
时间:
2015-8-21 18:00
楼主写得不错,赞一个
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2