目录
- 案例演示:客户端键盘录入服务器控制台输出
- 案例演示:客户端键盘录入服务器写到文本文件
- 案例演示:客户端读取文本文件服务器控制台输出
- 案例演示:TCP协议上传文本文件(客户端读取文本文件发送数据到服务器端,服务器端读取数据存储到文本文件中)
- 案例演示:TCP上传文本文件并给出反馈
- 案例演示:多客户端上传文件,多线程改进
案例演示:客户端键盘录入服务器控制台输出
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("192.168.15.100", 9999);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
System.out.println("请输入发送的消息");
String msg = reader.readLine();
//获取通道中输出流
OutputStream out = sk.getOutputStream();
out.write(msg.getBytes());
if("886".equals(msg)){
break;
}
}
//释放资源
sk.close();
}
}
服务端
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器开启,等待连接.....");
Socket sk = ss.accept();
while (true){
InputStream in = sk.getInputStream();
//获取客户端ip
String ip = sk.getInetAddress().getHostAddress();
byte[] bytes = new byte[1024];
int len = in.read(bytes);
String s = new String(bytes, 0, len);
System.out.println("IP:" + ip + "-给你发来消息:" + s);
if("886".equals(s)){
break;
}
}
ss.close();
}
}
案例演示:客户端键盘录入服务器写到文本文件
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("192.168.15.100", 9999);
OutputStream out= sk.getOutputStream();
//包装通道中的字节流,包装成字符流
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line=null;
while ((line=reader.readLine())!=null){
System.out.println("请输入内容");
writer.write(line);
writer.newLine();
writer.flush();
if("886".equals(line)){
break;
}
}
sk.close();
}
}
服务端
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器开启,等待连接.....");
Socket sk = ss.accept();
InputStream in = sk.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
BufferedWriter writer = new BufferedWriter(new FileWriter("msg.txt"));
String line=null;
while ((line=reader.readLine())!=null){
if("886".equals(line)){
break;
}
writer.write(line);
writer.newLine();
writer.flush();
}
writer.close();
ss.close();
}
}
案例演示:客户端读取文本文件服务器控制台输出
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("192.168.15.100", 9999);
OutputStream out = sk.getOutputStream();
//包装通道中的字节流,包装成字符流
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
BufferedReader reader = new BufferedReader(new FileReader("msg.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.flush();
}
sk.close();
}
}
服务端
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器开启,等待连接.....");
Socket sk = ss.accept();
InputStream in = sk.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line=null;
while ((line=reader.readLine())!=null){
System.out.println(line);
}
ss.close();
}
}
案例演示:TCP协议上传文本文件(客户端读取文本文件发送数据到服务器端,服务器端读取数据存储到文本文件中)
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("192.168.15.100", 9999);
OutputStream out = sk.getOutputStream();
//包装通道中的字节流,包装成字符流
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
BufferedReader reader = new BufferedReader(new FileReader("msg.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.flush();
}
reader.close();
sk.close();
}
}
服务端
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器开启,等待连接.....");
Socket sk = ss.accept();
InputStream in = sk.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
BufferedWriter writer = new BufferedWriter(new FileWriter("msg2.txt"));
String line=null;
while ((line=reader.readLine())!=null){
writer.write(line);
writer.newLine();
writer.flush();
}
writer.close();
ss.close();
}
}
案例演示:TCP上传文本文件并给出反馈
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("192.168.15.100", 9999);
OutputStream out = sk.getOutputStream();
//包装通道中的字节流,包装成字符流
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
BufferedReader reader = new BufferedReader(new FileReader("msg.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.flush();
}
/* //手写要给标记给服务端,告诉他,文件数据传输完了
writer.write("over"); //手写标记,要注意上传的文件中不要有这个标记
writer.newLine();
writer.flush();*/
// void shutdownOutput ()
// 禁用此套接字的输出流。
sk.shutdownOutput();
//读取服务端的反馈
InputStream in = sk.getInputStream();
byte[] bytes = new byte[1024];
int len = in.read(bytes); //读取不到,服务端的反馈,就阻塞了
String string = new String(bytes, 0, len);
System.out.println(string);
reader.close();
sk.close();
}
}
服务端
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器开启,等待连接.....");
Socket sk = ss.accept();
InputStream in = sk.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
BufferedWriter writer = new BufferedWriter(new FileWriter("msg4.txt"));
String line=null;
while ((line=reader.readLine())!=null){
/* if("over".equals(line)){
break;
}*/
writer.write(line);
writer.newLine();
writer.flush();
}
//下不来
//上传成功后,给客户端要给反馈
OutputStream out= sk.getOutputStream();
out.write("上传成功".getBytes());
writer.close();
ss.close();
}
}
案例演示:多客户端上传文件,多线程改进
客户端
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("192.168.15.100", 9999);
OutputStream out = sk.getOutputStream();
//包装通道中的字节流,包装成字符流
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
BufferedReader reader = new BufferedReader(new FileReader("msg.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.flush();
}
// void shutdownOutput ()
// 禁用此套接字的输出流。
sk.shutdownOutput();
//读取服务端的反馈
InputStream in = sk.getInputStream();
byte[] bytes = new byte[1024];
int len = in.read(bytes); //读取不到,服务端的反馈,就阻塞了
String string = new String(bytes, 0, len);
System.out.println(string);
reader.close();
sk.close();
}
}
服务端
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器开启,等待连接.....");
int i=1;
//循环侦听客户端
while (true){
Socket sk = ss.accept();
System.out.println((i++)+"个客户端连接上了");
//为每一个连接上来的客户端,单独开启一个线程去处理
new UpLoadThread(sk).start();
}
}
}
多线程
public class UpLoadThread extends Thread {
private Socket sk;
public UpLoadThread(Socket sk) {
this.sk = sk;
}
@Override
public void run() {
try {
//我们应该为每一个连接上来的客户端,单独开启一个线程去处理
InputStream in = sk.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//文件名,随机取,防止覆盖
BufferedWriter writer = new BufferedWriter(new FileWriter(System.currentTimeMillis()+"msg4.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.flush();
}
//上传成功后,给客户端要给反馈
OutputStream out = sk.getOutputStream();
out.write("上传成功".getBytes());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}