1、四、实验结果与数据处理五、分析与讨论编写的过程当中,客户端和服务端经常出现系统崩溃,后来才发现是编写的代码出现了问题,导致使用不了,后来在努力查找下终于实现了运行。第二个实验也是,比较多的问题都是代码出错,表示写得有点纠结,不过之后经过修改就成功了。发现细心很重要。六、教师评语签名:日期:成绩列出程序代码:一(1)、import java.io.*;import .*;import .Socket;public class ServerThread extends Thread private Socket connectToClient; private DataInputStream in
2、FromClient; private DataOutputStream outToClient; public ServerThread(Socket socket) throws IOException super(); connectToClient=socket; inFromClient=new DataInputStream(connectToClient.getInputStream(); outToClient=new DataOutputStream(connectToClient.getOutputStream(); start(); public void run() t
3、ry String str; double radius,area; boolean goon=true; while(goon) str=inFromClient.readUTF(); if(!str.equals(bye) radius=Double.parseDouble(str); System.out.println(接收到得半径值为:+radius); area=radius*radius*Math.PI; str=Double.toString(area); outToClient.writeUTF(str); outToClient.flush();圆面积+str+已经发送);
4、 else goon=false; outToClient.writeUTF( inFromClient.close(); outToClient.close(); connectToClient.close(); catch(IOException e) e.printStackTrace();(2)public class MultiServer public static void main(String args) try System.out.println(等待连接 ServerSocket serverSocket=new ServerSocket(3500); Socket c
5、onnectToClient=null; while(true) connectToClient=serverSocket.accept(); new ServerThread(connectToClient); catch(IOException e) e.printStackTrace(); (3)public class Client public static void main(String args) Socket connectToServer=new Socket(localhost,200); DataInputStream inFromServer=new DataInpu
6、tStream(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(); outToServer.writeUTF(outSt
7、r); outToServer.flush(); inStr=inFromServer.readUTF(); if(!inStr.equals() System.out.println(从服务器返回的结果是+inStr); else goon=false; inFromServer.close(); outToServer.close(); connectToServer.close(); ,300055);输入半径数值发送到服务器,输入bye结束。二(1)、import java.io.*;import java.awt.*;import java.awt.Container;import
8、java.awt.FlowLayout;import java.awt.event.*;import javax.swing.*;public class chatServer implements ActionListener,Runnable JTextArea showArea; JTextField msgText; JFrame mainJframe; JButton sentBtn; JScrollPane JSPane; JPanel pane; Container con; Thread thread=null; ServerSocket serverSocket; Socke
9、t 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.setColumns(30)
10、; 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.setVisib
11、le(true); mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); serverSocket=new ServerSocket(5500); showArea.append(正在等待对话请求n connectToClient=serverSocket.accept(); inFromClient=new DataInputStream(connectToClient.getInputStream(); outToClient=new DataOutputStream(connectToClient.getOutputStre
12、am(); thread=new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start();对不起,不能创建服务器n msgText.setEditable(false); sentBtn.setEnabled(false); new chatServer(); public void actionPerformed(ActionEvent e) String s=msgText.getText(); if(s.length()0) try outToClient.writeUTF(s); showArea.ap
13、pend(我说:+msgText.getText()+n catch(IOException e1) showArea.append(你的消息:”“未能发送出去n public void run() while(true) showArea.append(对方说:+inFromClient.readUTF()+ Thread.sleep(1000); catch(IOException el) catch(InterruptedException e)实验二public class chatClient implements ActionListener,Runnable Socket con
14、nectToServer; DataInputStream inFromServer; DataOutputStream outToServer; public chatClient()聊天-客户端 msgText=new JTextField(); con.add(JSPane, BorderLayout.CENTER); con.add(pane, BorderLayout.SOUTH); connectToServer=new Socket(,5500); inFromServer=new DataInputStream( connectToServer.getInputStream()
15、; outToServer=new DataOutputStream( connectToServer.getOutputStream();连接成功,请说话n thread=new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start();对不起,没能连接到服务器n msgText.setEditable(false); sentBtn.setEnabled(false); public static void main(String args) new chatClient(); outToServer.wri
16、teUTF(s);我说: msgText.setText(null); catch(IOException el)未能发送出去n+inFromServer.readUTF()+ Thread.sleep(1000); catch(InterruptedException e) 服务端:public class chatServer implements ActionListener,Runnable public chatServer() 聊天-服务器端 inFromClient=new DataInputStream( connectToClient.getInputStream(); outToClient=new DataOutputStream( connectToClient.getOutputStream();对不起,不能创建服务器 sentBtn.setEnabl
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1