Client类
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类
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类,再运行client类,在Client中填入数字后,在Server类的第70行就报异常,
不知道为什么会这样,而且报异常后程序还能运行,就是一直报异常
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类
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类,再运行client类,在Client中填入数字后,在Server类的第70行就报异常,
不知道为什么会这样,而且报异常后程序还能运行,就是一直报异常
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ex30.Client$TextFieldListener.actionPerformed(Client.java:70)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
package ex30;import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;public class Server extends JFrame{
// Text area for displaying contents
private JTextArea jta = new JTextArea();
public static void main(String[] args) {
// TODO 自动生成方法存根
new Server();
}
public Server(){
// Place text area on the frame
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("Server");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
try{
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("Server started at " + new Date() + '\n');
// Listen for a connection request
Socket socket = serverSocket.accept();
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
while(true){
// Receive radius from the client
double radius = inputFromClient.readDouble();
// Compute area
double area = radius * radius * Math.PI;
// Send area back to the client
outputToClient.writeDouble(area);
jta.append("Radius received from client: " + radius + '\n');
jta.append("Area found: " + area + '\n');
}
}
catch(IOException ex){
System.err.println(ex);
}
}}
// 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());
fromServer = new DataInputStream(socket.getInputStream());
}
catch(IOException ex){
jta.append(ex.toString() + '\n');
}