JAVA开发进阶实验报告模板_第1页
JAVA开发进阶实验报告模板_第2页
JAVA开发进阶实验报告模板_第3页
JAVA开发进阶实验报告模板_第4页
JAVA开发进阶实验报告模板_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、java开发进阶实验报告实验序号:4 实验项目名称:套接字通信学号1221145123姓名何征专业、班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 IOExceptionsupe

3、r();connectToClient=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&q

4、uot;)radius=Double.parseDouble(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");outToC

5、lient.flush();inFromClient.close();outToClient.close();connectToClient.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

6、); Socket connectToClient=null; while(true) connectToClient=serverSocket.accept(); 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(&q

7、uot;localhost",200);DataInputStream inFromServer=new DataInputStream(connectToServer.getInputStream();DataOutputStream outToServer=new DataOutputStream(connectToServer.getOutputStream();String outStr,inStr;boolean goon=true;BufferedReader buf=new BufferedReader(new InputStreamReader(System.in);

8、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();connectToServer.close();catch(IOException e)e.print

9、StackTrace();(3)import java.io.*;import .*;import .Socket;public class Client public static void main(String args)trySocket connectToServer=new Socket("localhost",300055);DataInputStream inFromServer=new DataInputStream(connectToServer.getInputStream();DataOutputStream outToServer=new Data

10、OutputStream(connectToServer.getOutputStream();System.out.println("输入半径数值发送到服务器,输入bye结束。");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=in

11、FromServer.readUTF();if(!inStr.equals("bye")System.out.println("从服务器返回的结果是"+inStr);elsegoon=false;inFromServer.close();outToServer.close();connectToServer.close();catch(IOException e)e.printStackTrace();二(1)、import java.io.*;import .*;import java.awt.*;import java.awt.Container;i

12、mport java.awt.FlowLayout;import java.awt.event.*;import javax.swing.*;public class chatServer implements ActionListener,RunnableJTextArea showArea;JTextField msgText;JFrame mainJframe;JButton sentBtn;JScrollPane JSPane;JPanel pane;Container con;Thread thread=null;ServerSocket serverSocket;Socket co

13、nnectToClient;DataInputStream inFromClient;DataOutputStream outToClient;public chatServer()mainJframe=new JFrame("聊天服务器端");con=mainJframe.getContentPane();showArea=new JTextArea();showArea.setEditable(false);showArea.setLineWrap(true);JSPane=new JScrollPane(showArea);msgText.setColumns(30)

14、;msgText.addActionListener(this);sentBtn=new JButton("发送");sentBtn.addActionListener(this);pane=new JPanel();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.setVi

15、sible(true);mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);tryserverSocket=new ServerSocket(5500);showArea.append("正在等待对话请求n");connectToClient=serverSocket.accept();inFromClient=new DataInputStream(connectToClient.getInputStream();outToClient=new DataOutputStream(connectToClient

16、.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 static void main(String args)new chatServer();public void actionPerformed(Action

17、Event 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 void run()trywhile(true)showAre

18、a.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 showAr

19、ea;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();showAr

20、ea=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();pane.setLayout(new FlowLayo

21、ut();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);tryconnectToServer=new Socket("localhost",5500);inFromServer=new

22、 DataInputStream(connectToServer.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");ms

23、gText.setEditable(false);sentBtn.setEnabled(false);public 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&quo

24、t;);msgText.setText(null);catch(IOException el)showArea.append("你的消息:"+msgText.getText()+"未能发送出去n");public void run()trywhile(true)showArea.append("对方说:"+inFromServer.readUTF()+"n");Thread.sleep(1000);catch(IOException el)catch(InterruptedException e)服务端:impor

25、t java.awt.*;import java.awt.event.*;import java.io.*;import .*;import javax.swing.*;public class chatServer implements ActionListener,RunnableJTextArea showArea;JTextField msgText;JFrame mainJframe;JButton sentBtn;JScrollPane JSPane;JPanel pane;Container con;Thread thread=null;ServerSocket serverSo

26、cket;Socket connectToClient;DataInputStream inFromClient;DataOutputStream outToClient;public chatServer()mainJframe=new JFrame("聊天-服务器端");con=mainJframe.getContentPane();showArea=new JTextArea();showArea.setEditable(false);showArea.setLineWrap(true);JSPane=new JScrollPane(showArea);msgText

27、=new JTextField();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(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);showArea.append("正在

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论