服务端
- 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();
- }
- }
复制代码 |
|