《JAVA开发进阶实验报告模板.doc》由会员分享,可在线阅读,更多相关《JAVA开发进阶实验报告模板.doc(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、java开发进阶实验报告实验序号:4 实验项目名称:套接字通信学号姓名何征专业、班12软件工程实验地点实1-518指导教师唐朝辉实验时间2013.3.26一、实验目的及要求了解套接字通信并且熟悉输入流、输出流得操作同时掌握套接字编程二、实验设备(环境)及要求windows 操作系统;Eclipse PC计算机三、实验内容与步骤1. 请看例子4-2。2. 请看例子4-4。四、实验结果与数据处理五、分析与讨论编写的过程当中,客户端和服务端经常出现系统崩溃,后来才发现是编写的代码出现了问题,导致使用不了,后来在努力查找下终于实现了运行。第二个实验也是,比较多的问题都是代码出错,表示写得有点纠结,不过
2、之后经过修改就成功了。发现细心很重要。六、教师评语签名:日期:成绩列出程序代码:一(1)、import java.io.*;import .*;import .Socket;public class ServerThread extends Thread private Socket connectToClient;private DataInputStream inFromClient;private DataOutputStream outToClient;public ServerThread(Socket socket) throws IOExceptionsuper();connec
3、tToClient=socket;inFromClient=new DataInputStream(connectToClient.getInputStream();outToClient=new DataOutputStream(connectToClient.getOutputStream();start();public void run()tryString str;double radius,area;boolean goon=true;while(goon)str=inFromClient.readUTF();if(!str.equals(bye)radius=Double.par
4、seDouble(str);System.out.println(接收到得半径值为:+radius);area=radius*radius*Math.PI;str=Double.toString(area);outToClient.writeUTF(str);outToClient.flush();System.out.println(圆面积+str+已经发送);elsegoon=false;outToClient.writeUTF(bye);outToClient.flush();inFromClient.close();outToClient.close();connectToClient
5、.close();catch(IOException e)e.printStackTrace();(2)import java.io.*;import .*;public class MultiServer public static void main(String args) try System.out.println(等待连接); ServerSocket serverSocket=new ServerSocket(3500); Socket connectToClient=null; while(true) connectToClient=serverSocket.accept();
6、 new ServerThread(connectToClient); catch(IOException e) e.printStackTrace(); (3)import java.io.*;import .*;import .Socket;public class Client public static void main(String args)trySocket connectToServer=new Socket(localhost,200);DataInputStream inFromServer=new DataInputStream(connectToServer.getI
7、nputStream();DataOutputStream outToServer=new DataOutputStream(connectToServer.getOutputStream();String outStr,inStr;boolean goon=true;BufferedReader buf=new BufferedReader(new InputStreamReader(System.in);while(goon)outStr=buf.readLine();outToServer.writeUTF(outStr);outToServer.flush();inStr=inFrom
8、Server.readUTF();if(!inStr.equals(bye)System.out.println(从服务器返回的结果是+inStr);elsegoon=false;inFromServer.close();outToServer.close();connectToServer.close();catch(IOException e)e.printStackTrace();(3)import java.io.*;import .*;import .Socket;public class Client public static void main(String args)tryS
9、ocket connectToServer=new Socket(localhost,);DataInputStream inFromServer=new DataInputStream(connectToServer.getInputStream();DataOutputStream outToServer=new DataOutputStream(connectToServer.getOutputStream();System.out.println(输入半径数值发送到服务器,输入bye结束。);String outStr,inStr;boolean goon=true;BufferedR
10、eader buf=new BufferedReader(new InputStreamReader(System.in);while(goon)outStr=buf.readLine();outToServer.writeUTF(outStr);outToServer.flush();inStr=inFromServer.readUTF();if(!inStr.equals(bye)System.out.println(从服务器返回的结果是+inStr);elsegoon=false;inFromServer.close();outToServer.close();connectToServ
11、er.close();catch(IOException e)e.printStackTrace();二(1)、import java.io.*;import .*;import java.awt.*;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event.*;import javax.swing.*;public class chatServer implements ActionListener,RunnableJTextArea showArea;JTextField msgText;JFram
12、e mainJframe;JButton sentBtn;JScrollPane JSPane;JPanel pane;Container con;Thread thread=null;ServerSocket serverSocket;Socket connectToClient;DataInputStream inFromClient;DataOutputStream outToClient;public chatServer()mainJframe=new JFrame(聊天服务器端);con=mainJframe.getContentPane();showArea=new JTextA
13、rea();showArea.setEditable(false);showArea.setLineWrap(true);JSPane=new JScrollPane(showArea);msgText.setColumns(30);msgText.addActionListener(this);sentBtn=new JButton(发送);sentBtn.addActionListener(this);pane=new JPanel();pane.setLayout(new FlowLayout();pane.add(msgText);pane.add(sentBtn);con.add(J
14、SPane,BorderLayout.CENTER);con.add(pane,BorderLayout.SOUTH);mainJframe.setSize(500,400);mainJframe.setVisible(true);mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);tryserverSocket=new ServerSocket(5500);showArea.append(正在等待对话请求n);connectToClient=serverSocket.accept();inFromClient=new DataI
15、nputStream(connectToClient.getInputStream();outToClient=new DataOutputStream(connectToClient.getOutputStream();thread=new Thread(this);thread.setPriority(Thread.MIN_PRIORITY);thread.start();catch(IOException e)showArea.append(对不起,不能创建服务器n);msgText.setEditable(false);sentBtn.setEnabled(false);public
16、static void main(String args)new chatServer();public void actionPerformed(ActionEvent e)String s=msgText.getText();if(s.length()0)tryoutToClient.writeUTF(s);outToClient.flush();showArea.append(我说:+msgText.getText()+n);catch(IOException e1)showArea.append(你的消息:”+msgText.getText()+“未能发送出去n);public voi
17、d run()trywhile(true)showArea.append(对方说:+inFromClient.readUTF()+n);Thread.sleep(1000);catch(IOException el)catch(InterruptedException e)实验二import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import javax.swing.*;public class chatClient implements ActionListener,RunnableJTextArea sh
18、owArea;JTextField msgText;JFrame mainJframe;JButton sentBtn;JScrollPane JSPane;JPanel pane;Container con;Thread thread=null;Socket connectToServer;DataInputStream inFromServer;DataOutputStream outToServer;public chatClient()mainJframe=new JFrame(聊天-客户端);con=mainJframe.getContentPane();showArea=new J
19、TextArea();showArea.setEditable(false);showArea.setLineWrap(true);JSPane=new JScrollPane(showArea);msgText=new JTextField();msgText.setColumns(30);msgText.addActionListener(this);sentBtn=new JButton(发送);sentBtn.addActionListener(this);pane=new JPanel();pane.setLayout(new FlowLayout();pane.add(msgTex
20、t);pane.add(sentBtn);con.add(JSPane, BorderLayout.CENTER);con.add(pane, BorderLayout.SOUTH);mainJframe.setSize(500,400);mainJframe.setVisible(true);mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);tryconnectToServer=new Socket(localhost,5500);inFromServer=new DataInputStream(connectToServer
21、.getInputStream();outToServer=new DataOutputStream(connectToServer.getOutputStream();showArea.append(连接成功,请说话n);thread=new Thread(this);thread.setPriority(Thread.MIN_PRIORITY);thread.start();catch(IOException e)showArea.append(对不起,没能连接到服务器n);msgText.setEditable(false);sentBtn.setEnabled(false);publi
22、c static void main(String args)new chatClient();public void actionPerformed(ActionEvent e)String s=msgText.getText();if(s.length()0)tryoutToServer.writeUTF(s);outToServer.flush();showArea.append(我说:+msgText.getText()+n);msgText.setText(null);catch(IOException el)showArea.append(你的消息:+msgText.getText
23、()+未能发送出去n);public void run()trywhile(true)showArea.append(对方说:+inFromServer.readUTF()+n);Thread.sleep(1000);catch(IOException el)catch(InterruptedException e)服务端:import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import javax.swing.*;public class chatServer implements ActionListen
24、er,RunnableJTextArea showArea;JTextField msgText;JFrame mainJframe;JButton sentBtn;JScrollPane JSPane;JPanel pane;Container con;Thread thread=null;ServerSocket serverSocket;Socket connectToClient;DataInputStream inFromClient;DataOutputStream outToClient;public chatServer()mainJframe=new JFrame(聊天-服务
25、器端);con=mainJframe.getContentPane();showArea=new JTextArea();showArea.setEditable(false);showArea.setLineWrap(true);JSPane=new JScrollPane(showArea);msgText=new JTextField();msgText.setColumns(30);msgText.addActionListener(this);sentBtn=new JButton(发送);sentBtn.addActionListener(this);pane=new JPanel
26、();pane.setLayout(new FlowLayout();pane.add(msgText);pane.add(sentBtn);con.add(JSPane, BorderLayout.CENTER);con.add(pane, BorderLayout.SOUTH);mainJframe.setSize(500,400);mainJframe.setVisible(true);mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);tryserverSocket=new ServerSocket(5500);showA
27、rea.append(正在等待对话请求n);connectToClient=serverSocket.accept();inFromClient=new DataInputStream(connectToClient.getInputStream();outToClient=new DataOutputStream(connectToClient.getOutputStream();thread=new Thread(this);thread.setPriority(Thread.MIN_PRIORITY);thread.start();catch(IOException e)showArea
28、.append(对不起,不能创建服务器);msgText.setEditable(false);sentBtn.setEnabled(false);public static void main(String args)new chatServer();public void actionPerformed(ActionEvent e)String s=msgText.getText();if(s.length()0)tryoutToClient.writeUTF(s);outToClient.flush();showArea.append(我说:+msgText.getText()+n);msgText.setText(null);catch(IOException el)showArea.append(你的消息:+msgText.getText()+未能发送出去n);public void run()trywhile(true)showArea.append(对方说:+inFromClient.readUTF()+n);Thread.sleep(1000);catch(IOException el)catch(InterruptedException e)