1、JAVA开发进阶实验报告模板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 IOExcept
3、ion super(); connectToClient=socket; inFromClient=new DataInputStream(connectToClient.getInputStream(); outToClient=new DataOutputStream(connectToClient.getOutputStream(); start(); public void run() try String str; double radius,area; boolean goon=true; while(goon) str=inFromClient.readUTF(); if(!st
4、r.equals(bye) 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+已经发送); else goon=false; outToClient.writeUTF(bye); outToClient.flush(); inFromClient.c
5、lose(); 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); Socket connectToClient=null; whi
6、le(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) try Socket connectToServer=new Socket(localhost,200); DataInputStream inF
7、romServer=new DataInputStream(connectToServer.getInputStream(); 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(); outT
8、oServer.writeUTF(outStr); outToServer.flush(); inStr=inFromServer.readUTF(); if(!inStr.equals(bye) System.out.println(从服务器返回的结果是+inStr); else goon=false; inFromServer.close(); outToServer.close(); connectToServer.close(); catch(IOException e) e.printStackTrace(); (3)import java.io.*;import .*;import
9、 .Socket;public class Client public static void main(String args) try Socket connectToServer=new Socket(localhost,); DataInputStream inFromServer=new DataInputStream(connectToServer.getInputStream(); DataOutputStream outToServer=new DataOutputStream(connectToServer.getOutputStream(); System.out.prin
10、tln(输入半径数值发送到服务器,输入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=inFromServer.readUTF(); if(!inStr.equals(bye) System.out.println(从服务
11、器返回的结果是+inStr); else goon=false; inFromServer.close(); outToServer.close(); connectToServer.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 cl
12、ass chatServer implements ActionListener,Runnable JTextArea showArea; JTextField msgText; JFrame mainJframe; JButton sentBtn; JScrollPane JSPane; JPanel pane; Container con; Thread thread=null; ServerSocket serverSocket; Socket connectToClient; DataInputStream inFromClient; DataOutputStream outToCli
13、ent; public chatServer() mainJframe=new JFrame(聊天服务器端); con=mainJframe.getContentPane(); showArea=new JTextArea(); showArea.setEditable(false); showArea.setLineWrap(true); JSPane=new JScrollPane(showArea); msgText.setColumns(30); msgText.addActionListener(this); sentBtn=new JButton(发送); sentBtn.addA
14、ctionListener(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); t
15、ry serverSocket=new ServerSocket(5500); showArea.append(正在等待对话请求n); connectToClient=serverSocket.accept(); inFromClient=new DataInputStream(connectToClient.getInputStream(); outToClient=new DataOutputStream(connectToClient.getOutputStream(); thread=new Thread(this); thread.setPriority(Thread.MIN_PRI
16、ORITY); 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(ActionEvent e) String s=msgText.getText(); if(s.length()0) try outToClient.writeUTF(s)
17、; outToClient.flush(); showArea.append(我说:+msgText.getText()+n); catch(IOException e1) showArea.append(你的消息:”+msgText.getText()+“未能发送出去n); public void run() try while(true) showArea.append(对方说:+inFromClient.readUTF()+n); Thread.sleep(1000); catch(IOException el) catch(InterruptedException e) 实验二impo
18、rt java.awt.*;import java.awt.event.*;import java.io.*;import .*;import javax.swing.*;public class chatClient implements ActionListener,Runnable JTextArea showArea; JTextField msgText; JFrame mainJframe; JButton sentBtn; JScrollPane JSPane; JPanel pane; Container con; Thread thread=null; Socket conn
19、ectToServer; DataInputStream inFromServer; DataOutputStream outToServer; public chatClient() mainJframe=new JFrame(聊天-客户端); con=mainJframe.getContentPane(); showArea=new JTextArea(); showArea.setEditable(false); showArea.setLineWrap(true); JSPane=new JScrollPane(showArea); msgText=new JTextField();
20、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
21、,400); mainJframe.setVisible(true); mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try connectToServer=new Socket(localhost,5500); inFromServer=new DataInputStream( connectToServer.getInputStream(); outToServer=new DataOutputStream( connectToServer.getOutputStream(); showArea.append(连接成功
22、,请说话n); 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 chatClient(); public void actionPerformed(ActionEvent e) Stri
23、ng s=msgText.getText(); if(s.length()0) try outToServer.writeUTF(s); outToServer.flush(); showArea.append(我说:+msgText.getText()+n); msgText.setText(null); catch(IOException el) showArea.append(你的消息:+msgText.getText()+未能发送出去n); public void run() try while(true) showArea.append(对方说:+inFromServer.readU
24、TF()+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 ActionListener,Runnable JTextArea showArea; JTextField msgText; JFrame mainJframe; JButton
25、 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 JTextArea();
26、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(msgText
27、); 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); try serverSocket=new ServerSocket(5500); showArea.append(正在等待对话请求n); connectToClient=ser
28、verSocket.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.append(对不起,不能创建服务器); msgText.setEditable(false); sentBtn.setEnabled(false);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1