C:客户端键盘录入,服务器控制台输出
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",9999);
//创建键盘录入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//包装通道传输流为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
}
}
服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(9999);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//包装通道传输流数据为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//读出数据并显示
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
//关闭资源
s.close();
}
}
D:客户端键盘录入,服务器端写到文本文件
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",12345);
//封装键盘录入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//把通道传输流封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
if("over".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
}
}
服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端对象
ServerSocket ss = new ServerSocket(12345);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//把通道传输流封装为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//封装文本文件写出数据
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
bw.close();
s.close();
}
}
E:客户端读取文本文件,服务器端控制台输出
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10022);
//封装文本文件
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//把通道传输流封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
br.close();
}
}
服务器端:
public class ServerDemo{
public static void main(String[] args){
//创建服务器端对象
ServerSocket ss = new ServerSocket(10022);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//把通道传输流封装为高效字符流
BuferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream));
//读入数据并显示
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
//关闭资源
s.close();
}
}
F:客户端读取文本文件,服务器端写到文本文件,服务器端给出反馈。
客户端:
public calss ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10088);
//封装文本文件对象
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//把通道传输里封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStream(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//结束标记
s.shutdownOutput();
//接收反馈信息并显示
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = brClient.readLine();
System.out.println(str);
//关闭资源
s.close();
br.close();
}
}
服务器端:
public class ServerDemo{
public static void main(String[] args){
//创建服务器端对象
ServerSocket ss = new ServerSocket(10088);
//监听客户端连接,返回Socket对象
Socket s = ss.accept()
//封装文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
//把通道传输流封装为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//写出文本
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件传输完毕!");
bwServer.newLine();
bwServer.flush();
//关闭资源
s.close();
bw.close();
}
}
G:上传图片到服务器,服务器端给出反馈信息
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",6666);
//封装图片对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aa.jpg"));
//把通道传输流封装为高效字节流
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
//写出数据
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read()) != -1){
bos.write(bys,o.len);
bos.flush();
}
//结束标记
s.shutdownOutput();
//接收反馈并显示
InputStream is = s.getInputStream();
byte[] bys2 = new byte[1024];
int len2 = is.read(bys2);
String client = new String(bys2,0,len2);
System.out.println(client);
//关闭资源
bis.close();
s.close();
}
}
服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端对象
ServerSocket ss = new ServerSocket(6666);
//监听客户端,返回Socket对象
Socket s = ss.accept();
//封装图片对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("aa.jpg"));
//把通道传输流封装为高效字节流
BufferedInputStream bis = new BufferedInputStream(s.getInputStream);
//读出数据
byte[] bys = new byte{1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0.len);
bos.flush();
}
//给出反馈
OutputStream os = s.getOutputStream();
os.write("图片上传完毕!".getBytes());
//关闭资源
bos.close();
s.close();
}
}
H:上传图片的多线程改进
客户端:
无变化
服务器端:
public class ServerThread implements Runnable{
private Socket s;
public ServerThread(Socket s){
this.s = s;
}
public void run(){
try{
//封装通道传输流为高效字节流
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
//封装文件对象
String newName = System.currentTimeMillis() + ".jpg";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newName));
//读入数据
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0,len);
bos.flush();
}
//给出反馈
OutputStream os = s.getOutputStream();
os.write("图片上传完毕!".getBytes());
//关闭资源
bos.close();
r.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(12345);
while(true){
Socket s = ss.accept();
new Thread(new ServerThread(s)).start();
}
}
} |
|