《网络编程实验报告.docx》由会员分享,可在线阅读,更多相关《网络编程实验报告.docx(4页珍藏版)》请在第壹文秘上搜索。
1、Java课后作业(班级)指导教师:提交日期:以下为指导教师的评语及评分:课后作业(班级)指导教师:评分教师日期一、实验题目Java网络编程二、实验内容代码:importjava.io.*;.*;impoPtjavaiUtil.*;publicclassServerpublicstaticvoidmain(Stringargs)ServerSocketserver=null;ServerThreadthread;Socketyou=null;while(true)trySerVer=newSerVerSOCket(2010);)catch(IOEceptionel)SyStem.out.Prin
2、tln(正在监听”);)trySyStem.out.printin(等待客户呼叫“);you=server.accept();System.out.println(客户的地址:”+you.getInetAddress();)catch(IOEceptione)SyStem.out.println(正在等待客户“);)if(you!=null)newServerThread(you).start();)classServerThreadextendsThreadSocketsocket;DataOutputStreamout=null;DataInputStreamin=null;Strings
3、=null;ServerThread(Sockett)socket=t;try(out=newDataOutputStream(socket.getutputstream();in=newDataInputStream(socket.getInputStream();catch(IOExceptione)publicvoidrun()while(true)try(doublea=in.readDouble();doubleb=in.readDouble();doublec=in.readDouble();if(a+b=ca+c=bb+c=a)doublearea=l;out.writeDoub
4、le(area);)elsedoublep=(a+b+c)2;doubleA=(p*(p-a)*(p-b)*(p-c);doublearea=Math.sqrt(八);out.writeDouble(area);)catch(IOExceptione)SyStem.out.Println(客户离开);return;)DriVatedOUbIeSart(doubleCI)return0;)importjava.io.*;publicclassReadimplementsRunnableDataInputStreamin;publicvoidSetDataInputStream(DataInput
5、Streamin)this.in=in;)publicvoidrun()doubleresult=0;while(true)try(result=in.readDouble();if(result0)System.OUt.printIn(你输入的三条边长不能组成三角形”);System.OUt.printin(,重新输入三角形的三条边长(放弃请输入N):);)elseSystem.out.println(三角形的面积为:+result);SyStem.out.printin(输入三角形的三条边长(放弃请输入N):);)catch(IOExceptione)SyStem.out.Println(
6、与服务器己断开+e);break;)importjava.io.*;.*;importjava.util.*;publicclassclientpublicstaticvoidmain(Stringargs)Scannerscanner=newScanner(System.in);Socketmysocket=null;DataInputStreamin=null;DataOutputStreamout=null;ThreadreadData;Readread=null;try(mysocket=newSocket();read=newRead();readData=newThread(rea
7、d);System.OUt.print(请输入服务器的IP地址:);StringIP=Scanner.nextLine();System.out.Print(请输入端口号:”);intport=scanner.nextlnt();if(mysocket.isConnected()elseInetAddressaddress=InetAddress.getyne(IP);InetSocketAddresssocketAddress=newInetSOCketAddress(address,port);mysocket.connect(SocketAddress);in=newDataInputS
8、tream(mysocket.getInputStream();out=newDataOutputStream(mysocket.getOutputStream();read.SetDataInputStream(In);readData.start();)catch(Exceptione)SyStem.out.print(服务器已经断开+e);)SyStem.out.PrintIn(请输入三角形的三条边长(放弃请输入N):);while(scanner.hasNext()doublea=0;doubleb=0;doublec=0;try(a=scanner.nextDouble();b=scanner.nextDouble();c=scanner.nextDouble();)catch(InputMismatChExceptionexp)System.exit(0);)try(out.WriteDouble(a);out.writeDouble(b);out.WriteDouble(c);)catch(Exceptione)运行结果: