您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 实验08-Java之网络编程
实验八网络编程1.实验目的(1)掌握Socket通信。(2)掌握UDP通信2.实验内容实验题1利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。客户端向服务器端发送Time命令,服务器端接受到该字符串后将服务器端当前时间返回给客户端;客户端向服务器端发送Exit命令,服务器端向客户端返回“Bye”后退出。packagecn.e;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.DataInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.Socket;importjava.net.UnknownHostException;importjava.util.Scanner;publicclassClient{publicstaticvoidmain(String[]args){Socketclient=null;InputStreamReaderin=null;//将字节流变为字符流,有转换功能OutputStreamWriterout=null;try{client=newSocket(localhost,4331);}catch(UnknownHostExceptione2){//TODOAuto-generatedcatchblocke2.printStackTrace();}catch(IOExceptione2){//TODOAuto-generatedcatchblocke2.printStackTrace();}try{in=newInputStreamReader(client.getInputStream());}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}try{out=newOutputStreamWriter(client.getOutputStream());}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}BufferedWriterbout=newBufferedWriter(out);BufferedReaderbin=newBufferedReader(in);while(true){//从客户端向服务器传输数据System.out.println(请输入'Time'或者'Exit');Scannerreader=newScanner(System.in);try{bout.write(reader.next());}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{bout.newLine();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{bout.flush();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}Stringmassage=null;try{massage=bin.readLine();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}if(massage.equals(Bye)){System.out.println(Fromserver:+massage);break;}elseSystem.out.println(从服务器返回的时间:+massage);}try{client.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}packagecn.e;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.ServerSocket;importjava.net.Socket;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassServer{publicstaticvoidmain(String[]args){ServerSocketserver=null;try{server=newServerSocket(4331);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}System.out.println(服务器启动完毕);System.out.println(等待客户端连接...);Socketyou=null;try{you=server.accept();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}InputStreamReaderin=null;OutputStreamWriterout=null;try{in=newInputStreamReader(you.getInputStream());}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{out=newOutputStreamWriter(you.getOutputStream());}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}BufferedWriterbout=newBufferedWriter(out);BufferedReaderbin=newBufferedReader(in);if(you.isConnected()){System.out.println(客户端名称为+you.getInetAddress().getHostAddress()+连接成功!);}while(true){Strings=null;try{s=bin.readLine();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}if(s.equals(Time)){Datedate=newDate();System.out.println(客户端请求当前时间);SimpleDateFormatformat=newSimpleDateFormat(yyyy/MM/ddHH:mm:ss);}elseif(s.equals(exit)){try{bout.write(bye!!);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{bout.newLine();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{bout.flush();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}try{you.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}[基本要求]编写完整程序。实验题2编写一数据报通信程序,实现简单的聊天功能。聊天内容输入文本确定清空退出图3.9聊天程序界面[基本要求]两人一组编写完整程序。“聊天内容”和“输入文本”分别为当前聊天的历史信息和当前要传送出去的聊天文本。“确定”、“清空”、“退出”三个按钮分别实现发送当前聊天文本、清空当前聊天文本和退出系统的功能。packagecn.e;importjava.awt.*;importjava.awt.event.*;importjava.io.IOException;importjava.net.*;importjavax.swing.*;publicclassUDPmessageextendsJFrameimplementsActionListener{privateJTextAreatext;privateJTextFieldipText;privateJTextFieldsendText;privateJButtonbutton1;privateJButtonbutton2;privateJButtonbutton3;privateDatagramSocketsocket;privateJScrollBarvsBar;publicUDPmessage(){setTitle(UDP聊天程序);setBounds(100,100,400,300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(newBorderLayout());text=newJTextArea();text.setEditable(false);JScrollPanetextPanel=newJScrollPane(text);vsBar=textPanel.getVerticalScrollBar();add(textPanel,BorderLayout.CENTER);JPanelpanel=newJPanel();//一定要有的BorderLayoutpanelLayout=newBorderLayout();panelLayout.setHgap(5);panel.setLayout(panelLayout);ipText=newJTextField(192.168.1.103);panel.add(ipText,BorderLayout.WEST);sendText=newJTextField();panel.add(sendText,BorderLayout.CENTER);button1=newJButton(确定);//button2=newJButton(清空);//button3=newJButton(退出);panel.add(button1,BorderLayout.EAST);add(panel,BorderLayout.SOUTH);setVisible(true);server();button1.addActionListener(this);}privatevoidserver(){try{socket=newDatagramSocket(9527);}catch(SocketExceptione){//TODOAuto-generatedcatchblocke.printStackTrace
本文标题:实验08-Java之网络编程
链接地址:https://www.777doc.com/doc-5305210 .html