第四次过程性考核
码云地址:https://gitee.com/Helen_en/fourth_process_assessment/tree/master
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
- 1.客户端从控制台进行输入,并将客户端的输出内容和时间保存到“学号.txt”文件中
- 2.服务器端读取到客户端的程序后,给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到文本文件中
- 3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个“学号_ip.txt”的文件
- 4.4.提交文件结果包括:代码,通信后生成的txt文件
Client_Main:
import java.io.*;
import java.net.*;
import java.util.*;
public class Client_Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Socket mysocket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread readData;
Read read=null;
try{
mysocket=new Socket();
read =new Read();
readData=new Thread(read);//负责读取信息的线程
System.out.print("输入服务器的IP:");
String IP=scanner.nextLine();
System.out.print("输入端口号:");
int port=scanner.nextLine();
if (mysocket.isConnected()){}
else{
InetAddress address=InetAddress.getByName("10.43.11.98");
InetSocketAddress socketAddress=new InetSocketAddress(address,port);
mysocket.connect(socketAddress);
in =new DataInputStream(mysocket.getInputStream());
out =new DataOutputStream(mysocket.getOutputStream());
read.setDataInputStream(in);
readData.start();//启动负者读取信息的线程
}
}
catch(Exception e){
System.out.println("服务器已断开"+e);
}
System.out.print("输入圆的半径(放弃请输入N):");
while(scanner.hasNext()){
double radius =0;
try{
radius=scanner.nextDouble();
}
catch (InputMismatchException exp){
System.exit(0);
}
try {
out.writeDouble(radius);
}
catch(Exception e){}
}
}
}
Server_Main:
import java.io.*;
import java.net.*;
import java.util.*;
public class Server_Main {
public static void main(String[] args) {
ServerSocket server =null;
ServerThread thread;
Socket you = null;
while(true) {
try{ server= new ServerSocket (2010);
}
catch (IOException e1){
System.out.println("正在监听"); //ServerSocket 对象不能重复创建
}
try{ System.out.println("等待客户呼叫");
you=server.accept () ;
System。out.println("'客户地址:"+you.getInetAddress());
}
catch (IOException e) {
System.out .println("正在等待客户");
}
if(you!=null) {
new ServerThread (you).start()); //为每一客户启动一个专门的线程
}
}
}
}
class ServerThread extends Thread {
Socket socket;
DataOutputStream out = null;
DataInputStream in = null;
String s = null;
ServerThread(Socket t) {
socket =t;
try { out = new DataOutputStream (socket.getOutputStream());
in =new DataInputStream (socket.getInputstream());
}
catch(IOException e){}
}
public void run(){
while(true){
try {
double r =in.readDouble();
double area=Math.PI*r*r;
out.writeDouble(area);
}
catch (IOException e){
System.out.println("客户离开");
return;
}
}
}
}
read:
import java.io.*;
public class Read implements Runnable {
DataInputStream in;
public void setDataInputStream(DataInputstream in){
this.in = in;
}
public void run(){
double result = 0;
while(true) {
try{ result =In.readDouble(); //读取服务器友送来的信息
System. out .printin (“圓的面枳:"+result);
System.out.print ("綸入圓的半径(放弃靖綸入N):");
}
catch(IOException e) {
System.out.println("与服务器已断幵"+e);
break;
}
}
}
}
考试总结:
学习Java已经一学期了,从最开始的迷茫,到现在可以编写一些简单的程序。这个考试让我明白了,这学期学的没有那么扎实,这次考试是这学期的回顾
以后在学习Java的时候,不应该浮躁,应该一步一个脚印的学习,将基础的的程序的复习再复习一遍。加深印象,使自己更加的优秀。
posted on 2018-12-15 13:04 nihaoya!!! 阅读(199) 评论(0) 收藏 举报
浙公网安备 33010602011771号