package com.softeem.example;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* IO
* 线程
* 网络
* @author mrchai
*
*/
public class FTPServer{
public static void main(String[] args) throws IOException {
File target = new File("D:\\素材\\视频\\demo.mp4");
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器启动");
while(true){
Socket s = ss.accept();
System.out.println(s);
new FileServer(target,s).start();
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
public class FileServer extends Thread{
private File file;
private Socket s;
public FileServer(File file,Socket s) {
super();
this.file = file;
this.s = s;
}
@Override
public void run() {
System.out.println("已连接客户端:"+s.getInetAddress()+",准备传输...");
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
//获取目标文件的输入流
bis = new BufferedInputStream(new FileInputStream(file));
//获取socket的输出流
bos = new BufferedOutputStream(s.getOutputStream());
byte[] b = new byte[1024];
int len = 0;
while((len=bis.read(b)) != -1){
bos.write(b, 0, len);
}
System.out.println("传输完成:"+s.getInetAddress());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bis != null){
bis.close();
}
if(bos != null){
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class FileClient {
public static void main(String[] args) throws UnknownHostException, IOException {
//连接到指定地址指定端口的服务
Socket s = new Socket("192.168.4.254",9999);
BufferedOutputStream br = null;
// //获取基于Socket的输出流并包装为打印流
File f = new File("D:\\素材\\视频\\demo.mp4");
//获取目标文件的输出流
br = new BufferedOutputStream(new FileOutputStream(f));
//获取socket的输入流
BufferedInputStream os =new BufferedInputStream( s.getInputStream());
int len=0;
byte []b = new byte[1024];
while((len=os.read(b))!=-1){
br.write(b,0,len);
}
os.close();
br.close();
s.close();
}
}
2.
package com.softeem.net.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyQQServer {
public static void main(String[] args) throws IOException {
//占据指定端口创建服务
ServerSocket ss = new ServerSocket(6799);
//开始监听,一旦有客户端连接,则获取跟此客户端通信的Socket对象
Socket s = ss.accept();
//获取基于Socket的输出流并包装为打印流
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
BufferedReader br = null;
pw.println("已连接Softeem聊天室,准备跟服务器畅聊吧!!!");
while(true){
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String msg = br.readLine();
System.out.println("Client:"+msg);
br = new BufferedReader(new InputStreamReader(System.in));
msg = br.readLine();
pw.println(msg);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyQQClient {
public static void main(String[] args) throws UnknownHostException, IOException {
//连接到指定地址指定端口的服务
Socket s = new Socket("192.168.4.140",6799);
BufferedReader br = null;
//获取基于Socket的输出流并包装为打印流
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
while(true){
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String msg = br.readLine();
System.out.println("Server:"+msg);
br = new BufferedReader(new InputStreamReader(System.in));
msg = br.readLine();
pw.println(msg);
}
}
}