您的位置:首页 > 理论基础 > 计算机网络

Io流,多线程,网络编程的融合练习

2017-04-28 00:00 239 查看
摘要: 实现从客户端的东西传到服务端,同时从后台复制文件到指定目录

package doit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Timer;
import java.util.TimerTask;

//实现从客户端的东西传到服务端,同时从后台复制文件到指定目录
public class mainStart {
static FileInputStream in = null;

public static void main(String[] s) {
File fin = new File("d:\\ThreadPrint.java");
File fout = new File("e:\\ThreadPrint.java");
FileD fd = new FileD(fin, fout);
fd.writeCopy();
try {
in = new FileInputStream(fin);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 服务端启动
new Thread(new Runnable() {
public void run() {
new ServerSocketD(9000).read();
}
}).start();
// 客户端启动(体现一下延时的上传)
new Timer().schedule(new TimerTask() {
@Override
public void run() {
new SocketD("192.168.1.103", 9000, in).write();
}
}, 1000);
// new Thread(new Runnable() {
// @Override
// public void run() {
// new SocketD("192.168.1.103", 9000, in).write();// 客户端启动
// }
// }).start();

fd.closeInputStream();
fd.closeOutputStream();

}
}


package doit;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//文件操作类
public class FileD {
BufferedOutputStream bo = null;
BufferedInputStream bi = null;

File in = null;

FileD(File in, File out) {
this.in = in;
reset();
try {
bo = new BufferedOutputStream(new FileOutputStream(out));
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

// read
public void read() {
int len = 0;
byte[] by = new byte[1024 * 9];
try {
while ((len = bi.read(by)) != -1) {
System.out.println(new String(by, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}
reset();

}

// write
public void writeCopy() {
reset();
int len = 0;
byte[] by = new byte[1024 * 9];
try {
while ((len = bi.read(by)) != -1) {
bo.write(by, 0, len);
bo.flush();

}
} catch (IOException e) {
e.printStackTrace();
}
reset();

}

// reset()重置bi输入流
public void reset() {
try {
bi = new BufferedInputStream(new FileInputStream(in));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}

// 自定义关闭输入流
public void closeInputStream() {
try {
bi.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// 自定义关闭输出流
public void closeOutputStream() {
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}


package doit;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端类

public class ServerSocketD {
ServerSocket ss = null;
Socket s = null;

ServerSocketD(int port) {
try {
ss = new ServerSocket(port);
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}
}

// 数据来源

public void read() {
InputStream in = null;
try {
in = s.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}

while (true) {
int len = 0;
byte[] by = new byte[1024 * 9];
try {
while ((len = in.read(by)) != -1) {
System.out.println(new String(by, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

}


package doit;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketD {
Socket s = null;
FileInputStream in = null;
OutputStream out = null;

public SocketD(String inet, int port, FileInputStream in) {
try {
s = new Socket(inet, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.in = in;

}

//
public void write() {
try {
out = s.getOutputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
int len = 0;
byte[] by = new byte[1024 * 9];
try {
while ((len = in.read(by)) != -1) {
out.write(new String(by, 0, len).getBytes());
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Io SocketServer