【文本复制】
[客户端]
package net;
import java.io.*;
import java.net.*;
public class TCPCliect {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("111.114.119.77",10004);
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\lenovo\\Desktop\\笔记\\java基础\\map.txt"));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);//true表示自动刷新
String line = null;
while((line = br.readLine())!=null){
pw.println(line);
}
s.shutdownOutput();//关闭客户端输出流,相当于给流中加入一个结束标记-1
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
bufIn.close();
s.close();
}
}
[服务端]
package net;
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(new FileWriter("C:\\Users\\lenovo\\Desktop\\笔记\\java基础\\map111.php"),true);
String line = null;
while((line = br.readLine())!= null){
pw.println(line);
}
PrintWriter pww = new PrintWriter(s.getOutputStream(),true);
pww.println("上传成功");
pww.close();
pw.close();
s.close();
ss.close();
}
}
【图片复制】
【客户端】
package net;
import java.io.*;
import java.net.*;
public class TCPCliect {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("111.114.119.77",10004);
FileInputStream fis = new FileInputStream("C:\\Users\\lenovo\\Desktop\\笔记\\zhaopin.png");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len;
while((len = fis.read(buf))!=-1){
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufin = new byte[1024];
int num;
while((num = in.read(bufin))!=-1){
System.out.println(new String(bufin,0,num));
}
fis.close();
s.close();
}
}
【服务端】
package net;
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\\Users\\lenovo\\Desktop\\笔记\\server.png");
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf))!=-1){
fos.write(buf,0,len);
}
PrintWriter pw= new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
pw.println("上传成功");
fos.close();
pw.close();
s.close();
ss.close();
}
}
[客户端]
package net;
import java.io.*;
import java.net.*;
public class TCPCliect {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("111.114.119.77",10004);
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\lenovo\\Desktop\\笔记\\java基础\\map.txt"));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);//true表示自动刷新
String line = null;
while((line = br.readLine())!=null){
pw.println(line);
}
s.shutdownOutput();//关闭客户端输出流,相当于给流中加入一个结束标记-1
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
bufIn.close();
s.close();
}
}
[服务端]
package net;
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(new FileWriter("C:\\Users\\lenovo\\Desktop\\笔记\\java基础\\map111.php"),true);
String line = null;
while((line = br.readLine())!= null){
pw.println(line);
}
PrintWriter pww = new PrintWriter(s.getOutputStream(),true);
pww.println("上传成功");
pww.close();
pw.close();
s.close();
ss.close();
}
}
【图片复制】
【客户端】
package net;
import java.io.*;
import java.net.*;
public class TCPCliect {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("111.114.119.77",10004);
FileInputStream fis = new FileInputStream("C:\\Users\\lenovo\\Desktop\\笔记\\zhaopin.png");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len;
while((len = fis.read(buf))!=-1){
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufin = new byte[1024];
int num;
while((num = in.read(bufin))!=-1){
System.out.println(new String(bufin,0,num));
}
fis.close();
s.close();
}
}
【服务端】
package net;
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\\Users\\lenovo\\Desktop\\笔记\\server.png");
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf))!=-1){
fos.write(buf,0,len);
}
PrintWriter pw= new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
pw.println("上传成功");
fos.close();
pw.close();
s.close();
ss.close();
}
}