练习题2:客户端键盘录入,服务器输出到控制台
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public
classServerDemo {
public
static void main(String[] args)
throws IOException {
// 创建服务器Socket对象
ServerSocketss = newServerSocket(22222);
// 监听客户端连接
Sockets = ss.accept();
// 包装通道内容的流
BufferedReaderbr = newBufferedReader(newInputStreamReader(
s.getInputStream()));
Stringline = null;
while ((line = br.readLine())!=
null){
System.out.println(line);
}
// br.close();
s.close();
// ss.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
/*
* 客户端键盘录入,服务器输出到控制台
*/
public
classClientDemo {
public
static void main(String[] args)
throws IOException {
// 创建客户端Socket对象
Sockets = newSocket("219.216.80.19", 22222);
// 键盘录入数据
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
// 把通道内的流给包装一下
BufferedWriterbw = newBufferedWriter(newOutputStreamWriter(
s.getOutputStream()));
Stringline = null;
while ((line = br.readLine())!=
null){
// 键盘录入数据要自定义结束标记
if ("886".equals(line)) {
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
// 释放资源
// bw.close();
// br.close();
s.close();
}
}
练习题3:客户端键盘录入,服务器输出文本文件
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public
classServerDemo {
public
static void main(String[] args)
throws IOException {
// 创建服务器Socket对象
ServerSocketss = newServerSocket(23456);
// 监听客户端连接
Sockets = ss.accept();
// 封装通道内的数据
BufferedReaderbr = newBufferedReader(newInputStreamReader(
s.getInputStream()));
// 封装文本文件
BufferedWriterbw = newBufferedWriter(newFileWriter("a.txt"));
Stringline = null;
while ((line = br.readLine())!=
null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
// br.close();
s.close();
// ss.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
/*
* 客户端键盘录入,服务器输出文本文件
*/
public
classClientDemo {
public
static void main(String[] args)
throws IOException {
// 创建客户端Socket对象
Sockets = newSocket("219.216.80.19", 23456);
// 封装键盘录入
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
// 封装通道内的数据
BufferedWriterbw = newBufferedWriter(newOutputStreamWriter(
s.getOutputStream()));
Stringline = null;
while ((line = br.readLine())!=
null){
if ("over".equals(line)) {
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
// bw.close();
// br.close();
s.close();
}
}
练习题3:客户端文本文件,服务器输出到控制台
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public
classServerDemo {
public
static void main(String[] args)
throws IOException {
// 创建服务器Socket对象
ServerSocketss = newServerSocket(34567);
// 监听客户端连接
Sockets = ss.accept();
// 封装通道内的流
BufferedReaderbr = newBufferedReader(newInputStreamReader(
s.getInputStream()));
Stringline = null;
while ((line = br.readLine())!=
null){
System.out.println(line);
}
s.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
/*
* 客户端文本文件,服务器输出到控制台
*/
public
classClientDemo {
public
static void main(String[] args)
throws IOException {
// 创建Socket对象
Sockets = newSocket("192.168.12.92", 34567);
// 封装文本文件
BufferedReaderbr = newBufferedReader(newFileReader(
"InetAddressDemo.java"));
// 封装通道内的流
BufferedWriterbw = newBufferedWriter(newOutputStreamWriter(
s.getOutputStream()));
Stringline = null;
while ((line = br.readLine())!=
null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
s.close();
}
}