# 20192324 2020-2021-1 《数据结构与面向对象程序设计》实验四报告
课程:《程序设计与数据结构》
班级: 1923
姓名: 夏馨
学号:20192324
实验教师:王志强
实验日期:2020年10月26日
必修/选修: 必修
1.实验内容
(一)Java Socket编程
1.学习蓝墨云上教材《Java和Android编程》“第16章 输入/输出 ”和“第22章 网络”,学习JavaSocket编程
2.结对编程。结对伙伴A编写客户端SocketClient.java,结对伙伴B编写服务器端。
3.截图加学号水印上传蓝墨云,代码push到码云,并撰写实验报告。
(二)Java和密码学
参考 http://www.cnblogs.com/rocedu/p/6683948.html
以结对的方式完成Java密码学相关内容的学习(帖子中所有代码和相关知识点需要学习)。提交学习成果码云链接和代表性成果截图,要有学号水印。
(三)编写有理数/复数计算器
结对编程,结对伙伴A编写有理数计算器。结对伙伴B编写复数计算器。截图加水印上传蓝墨云,代码push码云。
(四)远程有理数计算器
结对编程,结对伙伴A编程实现客户端,结果伙伴B实现服务器端。
客户端通过键盘输入一个有理数计算的公式(例如:1/4 + 1/6 = ),并把该公式以字符串的形式发送给伙伴B(服务器端),服务器端根据字符串计算出结果为5/12,并把结果返回给客户端A,A收到结果后输出结果。截图加水印上传蓝墨云,代码push码云。
(五)远程复数计算器
结对编程,结对伙伴B编程实现客户端,结果伙伴A实现服务器端。
客户端通过键盘输入一个有理数计算的公式(例如:1/4 + 1/6 = ),并把该公式以字符串的形式发送给伙伴A(服务器端),服务器端根据字符串计算出结果为5/12,并把结果返回给客户端B,B收到结果后输出结果。截图加水印上传蓝墨云,代码push码云。
注意实验四(4)和实验四(5),一个人不能仅实现客户端,必须实现一个客户端和服务器,否则两个实验均不得分!!!
2. 实验过程及结果
(1)Java Socket编程
-
服务器
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class Servertest { public static void main(String[] args) throws IOException { //1.建立一个服务器Socket(ServerSocket)绑定指定端口 ServerSocket serverSocket=new ServerSocket(1234); //2.使用accept()方法阻止等待监听,获得新连接 Socket socket=serverSocket.accept(); //3.获得输入流 InputStream inputStream=socket.getInputStream(); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream)); //获得输出流 OutputStream outputStream=socket.getOutputStream(); PrintWriter printWriter=new PrintWriter(outputStream); //4.读取用户输入信息 String info=null; System.out.println("服务器已经建立......"); while(!((info = bufferedReader.readLine()) ==null)){ System.out.println("实验真的好难啊!!!!!:" + info); } //给客户一个响应 String reply="Ture!!!"; printWriter.write(reply); printWriter.flush(); //5.关闭资源 printWriter.close(); outputStream.close(); bufferedReader.close(); inputStream.close(); socket.close(); serverSocket.close();
}
} -
客户端
import java.io.*;
import java.net.Socket;public class SocketClient { public static void main(String[] args) throws IOException { //1.建立客户端Socket连接,指定服务器位置和端口 Socket socket = new Socket("192.168.56.1",8800); // Socket socket = new Socket("192.168.43.55",8800); //2.得到socket读写流 OutputStream outputStream = socket.getOutputStream(); PrintWriter printWriter = new PrintWriter(outputStream); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); //输入流 InputStream inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); //3.利用流按照一定的操作,对socket进行读写操作 String info1 = " 用户名:Billy,密码:123456"; String info = new String(info1.getBytes("GBK"),"utf-8"); printWriter.write(info); printWriter.flush(); outputStreamWriter.write(info1); outputStreamWriter.flush(); socket.shutdownOutput(); //接收服务器的响应 String reply = null; while (!((reply = bufferedReader.readLine()) ==null)){ System.out.println("接收服务器的信息为:" + reply); } //4.关闭资源 bufferedReader.close(); inputStream.close(); outputStreamWriter.close(); //printWriter.close(); outputStream.close(); socket.close();
}
} -
运行结果
(二)JAVA和密码学
(三)编写有理数/复数计算器
- 建立分数类
import java.util.StringTokenizer;
public class Fraction {
int fenzi,fenmu;
char ch;
public Fraction(String str) {
StringTokenizer st=new StringTokenizer(str,"/",true);
this.fenzi = Integer.parseInt(st.nextToken());
this.ch=st.nextToken().charAt(0);
this.fenmu = Integer.parseInt(st.nextToken());
}
public Fraction yuefen(int fz,int fm){
int i;
for (i=2;i<=fz&&i<=fm;i++){
if(fz%i==0&&fm%i==0){
fz=fz/i;
fm=fm/i;
}
}
Fraction result=new Fraction(fz+"/"+fm);
return result;
}
public Fraction getJia(Fraction x){
int newFenmu=fenmu*x.fenmu;
int newFenzi=fenzi*x.fenmu+x.fenzi*fenmu;
return yuefen(newFenzi,newFenmu);
}
public Fraction getJian(Fraction x){
int newFenmu=fenmu*x.fenmu;
int newFenzi=fenzi*x.fenmu-x.fenzi*fenmu;
return yuefen(newFenzi,newFenmu);
}
public Fraction getCheng(Fraction x){
int newFenmu=fenmu*x.fenmu;
int newFenzi=fenzi*x.fenzi;
return yuefen(newFenzi,newFenmu);
}
public Fraction getChu(Fraction x){
int newFenmu=fenmu*x.fenzi;
int newFenzi=fenzi*x.fenmu;
return yuefen(newFenzi,newFenmu);
}
@Override
public String toString() {
return fenzi + "/" + fenmu;
}
}
-
编写计算器
public class RationalNumberCalculator {
public static void main(String[] args) {Fraction result; Fraction frac1,frac2; if(args[0].contains("/")){ frac1=new Fraction(args[0]); } else{ frac1=new Fraction(args[0]+"/"+1); } if(args[2].contains("/")){ frac2=new Fraction(args[2]); } else{ frac2=new Fraction(args[2]+"/"+1); } char ch=args[1].charAt(0); switch (ch) { case '+': result=frac1.getJia(frac2); System.out.println(frac1+String.valueOf(ch)+frac2+"="+result); break; case '-': result=frac1.getJian(frac2); System.out.println(frac1+String.valueOf(ch)+frac2+"="+result); break; case '*': result=frac1.getCheng(frac2); System.out.println(frac1+String.valueOf(ch)+frac2+"="+result); break; case '/': result=frac1.getChu(frac2); System.out.println(frac1+String.valueOf(ch)+frac2+"="+result); break; default: System.out.println("Illegal Input"); break; }
}
} -
运行结果
(四)远程有理数计算器
之前编写过有理数和服务器的代码,这里可以直接用
- 服务器
package Demo;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
public class Server192324 {
private static Fraction frac2;
private static Fraction frac1;
private static String a,b;
private static char ch;
private static Fraction result = null;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(4567);
Socket socket=serverSocket.accept();
InputStream inputStream=socket.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
OutputStream outputStream=socket.getOutputStream();
PrintWriter printWriter=new PrintWriter(outputStream);
String info=null;
System.out.println("服务器已经建立......");
while(!((info = bufferedReader.readLine()) ==null)){
System.out.println("我是服务器,用户信息为:" + info);
StringTokenizer st = new StringTokenizer(info, " ", false);
a=st.nextToken();
ch=st.nextToken().charAt(0);
b=st.nextToken();
frac1=new Fraction(a);
frac2=new Fraction(b);
switch (ch)
{
case '+':
result=frac1.getJia(frac2);
break;
case '-':
result=frac1.getJian(frac2);
break;
case '*':
result=frac1.getCheng(frac2);
break;
case '/':
result=frac1.getChu(frac2);
break;
default:
break;
}
}
String reply=frac1+String.valueOf(ch)+frac2+"="+result;
printWriter.write(reply);
printWriter.flush();
printWriter.close();
outputStream.close();
bufferedReader.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
- 客户端
package Demo;
import java.io.*;
import java.net.Socket;
public class SocketClient2 {
public static void main(String[] args) throws IOException {
//1.建立客户端Socket连接,指定服务器位置和端口
Socket socket = new Socket("",4567);
// Socket socket = new Socket("172.16.43.187",8800);
//2.得到socket读写流
OutputStream outputStream = socket.getOutputStream();
// PrintWriter printWriter = new PrintWriter(outputStream);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
//输入流
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
//3.利用流按照一定的操作,对socket进行读写操作
String info1 = " 1/4 + 1/6";
// String info = new String(info1.getBytes("GBK"),"utf-8");
// printWriter.write(info);
// printWriter.flush();
outputStreamWriter.write(info1);
outputStreamWriter.flush();
socket.shutdownOutput();
//接收服务器的响应
String reply = null;
while (!((reply = bufferedReader.readLine()) ==null)){
System.out.println("接收服务器的信息为:" + reply);
}
//4.关闭资源
bufferedReader.close();
inputStream.close();
outputStreamWriter.close();
//printWriter.close();
outputStream.close();
socket.close();
}
}
- 运行结果
(五)远程复数计算器
-
复数类
package Demo;
import java.util.StringTokenizer;
public class Complex{
private int RealPart;
private int ImagePart;
private char ch;//构造函数
public Complex(){}
public Complex(String str){
StringTokenizer st=new StringTokenizer(str,".",true);
this.RealPart=Integer.parseInt(st.nextToken());
this.ch=st.nextToken().charAt(0);
this.ImagePart=Integer.parseInt(st.nextToken());
}//返回
public double getRealPart(){
return RealPart;
}public double getImagePart(){
return ImagePart;
}//接收
public void setRealPart(int realPart){
RealPart=realPart;
}public void setImagePart(int imagePart){
ImagePart=imagePart;
}//重写函数toString
public String toString(){
return RealPart + "." + ImagePart;
}public boolean equals(Object a){
if (a==this){
return true;
}
else{
return false;
}
}// 定义公有方法:加减乘除
public Complex ComplexAdd(Complex a){
Complex b = new Complex((this.RealPart+a.RealPart)+"."+(this.ImagePart+a.ImagePart));
return b;
}
public Complex ComplexSub(Complex a){
Complex b = new Complex((this.RealPart-a.RealPart)+"."+(this.ImagePart-a.ImagePart));
return b;
}
public Complex ComplexMulti(Complex a){
Complex b = new Complex((this.RealParta.RealPart-this.ImageParta.ImagePart)+"."+(this.ImageParta.RealPart+this.RealParta.ImagePart));
return b;
}
public Complex ComplexDiv(Complex a){
double scale = a.getRealPart()a.getRealPart() + a.getImagePart()a.getImagePart();
Complex b = new Complex((a.getRealPart() / scale)+"."+
(- a.getImagePart() / scale));
return b;
}
} -
服务器
package Demo;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
public class Server192324_1 {
private static Complex cplx1;
private static Complex cplx2;
private static String a,b;
private static char ch;
private static Complex result = null;
public static void main(String[] args) throws IOException {
//1.建立一个服务器Socket(ServerSocket)绑定指定端口
ServerSocket serverSocket=new ServerSocket(8809);
//2.使用accept()方法阻止等待监听,获得新连接
Socket socket=serverSocket.accept();
//3.获得输入流
InputStream inputStream=socket.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
//获得输出流
OutputStream outputStream=socket.getOutputStream();
PrintWriter printWriter=new PrintWriter(outputStream);
//4.读取用户输入信息
String info=null;
System.out.println("服务器已经建立......");
while(!((info = bufferedReader.readLine()) ==null)) {
System.out.println("我是服务器,用户信息为:" + info);
StringTokenizer st = new StringTokenizer(info, " ", false);
a = st.nextToken();
ch = st.nextToken().charAt(0);
b = st.nextToken();
cplx1 = new Complex(a);
cplx2 = new Complex(b);
switch (ch) {
case '+':
result = cplx1.ComplexAdd(cplx2);
break;
case '-':
result = cplx1.ComplexSub(cplx2);
break;
case '*':
result = cplx1.ComplexMulti(cplx2);
break;
case '/':
result = cplx1.ComplexDiv(cplx2);
break;
default:
break;
}
}
//给客户一个响应
String reply=cplx1+String.valueOf(ch)+cplx2+"="+result;
printWriter.write(reply);
printWriter.flush();
//5.关闭资源
printWriter.close();
outputStream.close();
bufferedReader.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
- 客户端
import java.io.*;
import java.net.Socket;
public class SocketClient3 {
public static void main(String[] args) throws IOException {
//1.建立客户端Socket连接,指定服务器位置和端口
Socket socket = new Socket("",8809);
// Socket socket = new Socket("172.16.43.187",8800);
//2.得到socket读写流
OutputStream outputStream = socket.getOutputStream();
// PrintWriter printWriter = new PrintWriter(outputStream);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
//输入流
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
//3.利用流按照一定的操作,对socket进行读写操作
String info1 = " 4.8 * 5.6";
// String info = new String(info1.getBytes("GBK"),"utf-8");
// printWriter.write(info);
// printWriter.flush();
outputStreamWriter.write(info1);
outputStreamWriter.flush();
socket.shutdownOutput();
//接收服务器的响应
String reply = null;
while (!((reply = bufferedReader.readLine()) ==null)){
System.out.println("接收服务器的信息为:" + reply);
}
//4.关闭资源
bufferedReader.close();
inputStream.close();
outputStreamWriter.close();
//printWriter.close();
outputStream.close();
socket.close();
}
}
- 运行结果
3. 实验过程中遇到的问题和解决过程
- 问题1:结对服务器和客户端发送信息时总是飘红
- 问题1解决方案:cmd中找到自己的ip地址后输入host中且连在一个wifi下解决了
- 问题2:密码学习比较困难
- 问题2解决方案:参照链接的学习方法与自己网上搜索课外资料慢慢学习
其他(感悟、思考等)
- java真的太难了,头皮发麻
- 至此实验结束,java学习便告一段落了,自己可能学到的东西并没有想象中的多,但还是都敲了一下代码,下网在下一阶段的学习中能更认真努力一些。