0

我编写了这个简单的 JFRAME 程序,它由两个类组成:客户端和服务器。

服务器从客户端接收文本并打印出来。

我刚开始学习它,所以我真的不知道问题是什么。

我想问题是while循环或我获取输入的方式。请告诉我我做错了什么以及如何解决它:)

这是服务器:

import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import javax.swing.*;
public class Server extends JFrame {
private int running =0;

    private JPanel showInformation;
    private JButton start;
    private JButton stop;
    private JTextArea infoArea;
    private JScrollPane scroll;

    private ServerSocket server;
    private Socket client;   

    private InputStreamReader IR;
    private BufferedReader BR;

public Server(){
   //setting looks of Frame
   setLayout(null);
   showInformation=new JPanel();
   start = new JButton("Start Server");
   stop=new JButton("Stop Server");
   infoArea= new JTextArea(20,27);
   infoArea.setEnabled(false);
   infoArea.setEditable(false);
   scroll = new JScrollPane(infoArea);
   scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   scroll.setBounds(25,10,325,200);
   scroll.setEnabled(true);
   add(scroll);

   showInformation.setSize(300, 200);
   showInformation.setBounds(25,5,325,200);
   add(showInformation);
   infoArea.setLineWrap(true);
   start.setBounds(25,225,150,50);
   stop.setBounds(200,225,150,50);

   add(start);
   add(stop);     
   //events stuff
   eventHandler handler = new eventHandler();
   start.addActionListener(handler);
   stop.addActionListener(handler);

   //sockets 
      }

   public void printOnScreen(String m){
    infoArea.append("\n"+m);
  }

  public class eventHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==start){
           if(isServerRunning()){
               printOnScreen("Server already running");
           }
           else{
               try{
               startServer();
               }catch(Exception e1){

               }
           }
        }else 
          if(e.getSource()==stop){
              if(isServerRunning()){
                   try{
                   stopServer();
                  }catch(Exception e1){

                  }
               }
               else{
                   printOnScreen("Server Not Running yet");
               }
          }

    }

}



public boolean isServerRunning(){
    if(running==1)
        return true;
    else
        return false;
}


public void startServer() throws Exception{
    server = new ServerSocket(4444);
    client = server.accept();
    IR = new InputStreamReader(client.getInputStream());
    BR = new BufferedReader(IR);
    printOnScreen("Server started..");
    getMessages();
}

public void stopServer() throws Exception{
    try{
      server.close();
      client.close();
      IR.close();
      BR.close();
      printOnScreen("Server stopped..");
    }catch(Exception e1){

    }
}

private void getMessages() throws IOException{
    String M = BR.readLine();
    while(true){
        if(M!=null){
            printOnScreen(M);
        }
        M = BR.readLine();
    }
}


public static void main(String[]args){
   Server s = new Server();
   s.setDefaultCloseOperation(EXIT_ON_CLOSE);
   s.setSize(385, 350);
   s.setVisible(true);
   s.setTitle("      ----Server----");
   s.setTitle("Server Manager");
}

}

这是客户:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

public class client1 extends JFrame{
private int connected = 0;
private JTextArea txt;
private JTextField mText;
private JButton sendMessage;
private JScrollPane scroll;
private JButton connect;
private JButton disconnect;
private Socket socket;
private ObjectInputStream input;
private ObjectOutputStream output;

public client1(){
    setLayout(null);

    txt = new JTextArea();
    scroll = new JScrollPane(txt);
    scroll.setBounds(25,10,325,200);
    add(scroll);


    mText = new JTextField();
    mText.setBounds(25,220,250,30);
    add(mText);

    sendMessage=new JButton("Send");
    sendMessage.setBounds(280,220,70,30);   
    add(sendMessage);

    connect= new JButton("Connect");
    connect.setBounds(25,260,100,30);
    add(connect);

    disconnect = new JButton("Disconnect");
    disconnect.setBounds(135,260,100,30);
    add(disconnect);
  //events stuff
       eventHandler handler = new eventHandler();
       sendMessage.addActionListener(handler);
       connect.addActionListener(handler);
       disconnect.addActionListener(handler);
   }
public class eventHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==sendMessage){
            if(connected==1){
              try {
                sendMessage();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            }
        }
        else
            if(e.getSource()==connect){
               connected=1;
               printOnScreen("connected");
            }
        else
            if(e.getSource()==disconnect){
                connected=0;
                printOnScreen("Disconnected");
            }
    }
}

public void printOnScreen(String m){
    txt.append("\n"+m);
}

public void sendMessage() throws IOException{
    String message = mText.getText();
    txt.append("\nclient1:  "+message);
    output.writeObject(message);

}
public void connect() throws UnknownHostException, IOException{
    socket = new Socket("localhost",4444);
    output = new ObjectOutputStream(socket.getOutputStream());
    output.flush();
}

public static void main(String[]args){
    client1 c1Frame = new client1();
    c1Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    c1Frame.setSize(385,350);
    c1Frame.setTitle("             ---___Client1___---");
    c1Frame.setVisible(true);
}

  }
4

1 回答 1

0

你必须得到一个 NullPointerExpection。那是因为当单击连接按钮时,您永远不会在客户端调用连接函数。所以没有建立套接字连接,因为你的输出对象是空的,你不能发送任何东西。

于 2013-06-10T19:46:29.897 回答