0

在这里,我创建了一个充当客户端的 GUI。它包含一个文本字段和一个按钮。我的意图是打印用户在文本字段中键入的任何内容,必须在服务器命令提示符中显示。

服务器.java

import java.io.*;
import java.net.*;

public class Server {

    Server(){
            try{
                    ServerSocket server = new ServerSocket(4040);


                    while(true){
                            Socket sock = server.accept();
                            InputStream is = sock.getInputStream();
                            InputStreamReader streamReader = new InputStreamReader(is);
                            BufferedReader reader = new BufferedReader(streamReader);
                            String string = reader.readLine();
                            System.out.println(string);
                            reader.close();

                    }
                }   
            catch(IOException ex)
                {
                    ex.printStackTrace();
                }

        }   



    public static void main(String args[]){
        new Server();
    }

}

SimpleChatClientA.java

import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;

public class SimpleChatClientA {
    JTextField outgoing;
    PrintWriter writer;
    Socket sock;
    public void go(){
        JFrame frame = new JFrame("chat client");
        outgoing = new JTextField(20);
        JButton sendButton = new JButton("send");
        frame.add(outgoing);
        frame.add(sendButton);
        sendButton.addActionListener(new SendButtonListener());
        frame.setLayout(new FlowLayout());
        frame.setSize(500,100);
        setUpNetworking();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void setUpNetworking(){
        try {
            Socket sock = new Socket("localhost",4040);
            PrintWriter writer = new PrintWriter(sock.getOutputStream());
            System.out.println("connection established");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public class SendButtonListener implements ActionListener{

      public void actionPerformed(ActionEvent e) {
          try {
                String str = outgoing.getText();
                writer.println(str);
                writer.flush();
          } 
          catch (Exception ex) {
             ex.printStackTrace();
            }
          outgoing.setText("");
          outgoing.requestFocus();
        }

    }
    public static void main(String args[]){
        SimpleChatClientA simple  = new SimpleChatClientA();
        simple.go();
    }

}

当我运行这个程序时。连接建立。但我看不到在服务器命令提示符的 JTextField 中输入的消息。我看到发生了一些错误。我不明白它是什么。我是java新手。请帮助我

4

2 回答 2

1

问题似乎是,您正在创建编写器并存储在局部变量中。

private void setUpNetworking(){
        try {
            Socket sock = new Socket("localhost",4040);
            // PROBLEM IS HERE
            PrintWriter writer = new PrintWriter(sock.getOutputStream());
            System.out.println("connection established");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

尝试这个:

private void setUpNetworking(){
        try {
            Socket sock = new Socket("localhost",4040);
            // STORE WRITER IN INSTANCE VARIABLE
            writer = new PrintWriter(sock.getOutputStream());
            System.out.println("connection established");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

您一定有 NullPointerException,因为您还没有创建 writer 对象。

public void actionPerformed(ActionEvent e) {
          try {
                String str = outgoing.getText();
                // HERE YOU WILL GET EXCEPTION AS WRITER (instace variable) IS NOT CREATED
                writer.println(str);
                writer.flush();
          } 
          catch (Exception ex) {
             ex.printStackTrace();
            }
          outgoing.setText("");
          outgoing.requestFocus();
        }
于 2013-10-16T08:09:32.090 回答
1

您可以编写类似这样的服务器来接受多行:

package com.stackoverflow.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    Server(){
        try{
            ServerSocket server = new ServerSocket(4040);


            while(true){
                Socket sock = server.accept();
                SocketHandler handler = new SocketHandler(sock);
                Thread t = new Thread(handler);
                t.start();
            }
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

    }


    public static class SocketHandler implements Runnable {

        private final Socket socket;
        private final BufferedReader reader;

        public SocketHandler(Socket socket) throws IOException {
            this.socket = socket;
            this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        }

        @Override
        public void run() {
            String message = null;
            try {
                while ((message = reader.readLine()) != null) {
                    System.out.println("incoming message: " + message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String args[]){
        new Server();
    }
}
于 2013-10-16T08:28:48.283 回答