package com.zhiyou100.demo044;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLOutput;
import java.util.Scanner;
public class Demo01ServerSocket {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(6666);
while (true) {
Socket socket = server.accept();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
try (
FileInputStream fis = new FileInputStream("day31_Net\\1.jpeg");
OutputStream os = socket.getOutputStream();
) {
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}
package com.zhiyou100.demo044;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class Demo01Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 6666);
InputStream is = socket.getInputStream();
File file = new File("C:\\Users\\Administrator\\Desktop");
if (!file.exists()){
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file + "\\" + Thread.currentThread().getName() + System.currentTimeMillis() + "2.jpeg");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = is.read(bytes)) != -1){
fos.write(bytes, 0, len);
}
fos.close();
is.close();
socket.close();
}
}