当前位置: 代码迷 >> J2SE >> Socket网络编程报错误
  详细解决方案

Socket网络编程报错误

热度:83   发布时间:2016-04-24 00:43:48.0
Socket网络编程报异常
Client类
Java code
package ex30;import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Client extends JFrame{    // Text field for receiving radius    private JTextField jtf = new JTextField();        // Text area to display contents    private JTextArea jta = new JTextArea();        // IO streams    private DataOutputStream toServer;    private DataInputStream fromServer;        public static void main(String[] args) {        // TODO 自动生成方法存根        new Client();    }        public Client(){        // Panel p to hold the label and text field        JPanel p = new JPanel();        p.setLayout(new BorderLayout());        p.add(new JLabel("Enter radius"), BorderLayout.WEST);        p.add(jtf, BorderLayout.CENTER);        jtf.setHorizontalAlignment(JTextField.RIGHT);                setLayout(new BorderLayout());        add(p, BorderLayout.NORTH);        add(new JScrollPane(jta), BorderLayout.CENTER);                jtf.addActionListener(new TextFieldListener());                setTitle("Client");        setSize(500, 300);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setVisible(true);  // It is necessary to show the frame here!                try{            // Create a socket to connect to the server            Socket socket = new Socket("localhost", 8000);            // Socket socket = new Socket("130.254.204.36", 8000);            // Socket socket = new Socket("drake.Armstrong.edu", 8000);                        // Create an input stream to receive data from the server            toServer =                new DataOutputStream(socket.getOutputStream());        }        catch(IOException ex){            jta.append(ex.toString() + '\n');        }    }        private class TextFieldListener implements ActionListener{        public void actionPerformed(ActionEvent e){            try{                // Get the radius from the text field                double radius = Double.parseDouble(jtf.getText().trim());                                // Send the radius to the server                toServer.writeDouble(radius);                toServer.flush();                                // Get area from the server                double area = fromServer.readDouble();  //这一行为什么会报异常,但程序还能运行                                // Display to the text area                jta.append("Radius is " + radius + "\n");                jta.append("Area received from the server is "                        + area + '\n');            }            catch(IOException ex){                System.err.println(ex);            }        }    }}

Server类
Java code
package ex30;import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Client extends JFrame{    // Text field for receiving radius    private JTextField jtf = new JTextField();        // Text area to display contents    private JTextArea jta = new JTextArea();        // IO streams    private DataOutputStream toServer;    private DataInputStream fromServer;        public static void main(String[] args) {        // TODO 自动生成方法存根        new Client();    }        public Client(){        // Panel p to hold the label and text field        JPanel p = new JPanel();        p.setLayout(new BorderLayout());        p.add(new JLabel("Enter radius"), BorderLayout.WEST);        p.add(jtf, BorderLayout.CENTER);        jtf.setHorizontalAlignment(JTextField.RIGHT);                setLayout(new BorderLayout());        add(p, BorderLayout.NORTH);        add(new JScrollPane(jta), BorderLayout.CENTER);                jtf.addActionListener(new TextFieldListener());                setTitle("Client");        setSize(500, 300);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setVisible(true);  // It is necessary to show the frame here!                try{            // Create a socket to connect to the server            Socket socket = new Socket("localhost", 8000);            // Socket socket = new Socket("130.254.204.36", 8000);            // Socket socket = new Socket("drake.Armstrong.edu", 8000);                        // Create an input stream to receive data from the server            toServer =                new DataOutputStream(socket.getOutputStream());        }        catch(IOException ex){            jta.append(ex.toString() + '\n');        }    }        private class TextFieldListener implements ActionListener{        public void actionPerformed(ActionEvent e){            try{                // Get the radius from the text field                double radius = Double.parseDouble(jtf.getText().trim());                                // Send the radius to the server                toServer.writeDouble(radius);                toServer.flush();                                // Get area from the server                double area = fromServer.readDouble();  //这一行为什么会报异常,但程序还能运行                                // Display to the text area                jta.append("Radius is " + radius + "\n");                jta.append("Area received from the server is "                        + area + '\n');            }            catch(IOException ex){                System.err.println(ex);            }        }    }}
  相关解决方案