JAVA SERVER SOCKET - 帮助它不会发送

时间:2015-08-10 10:31:22

标签: java sockets client server

我已经在这方面工作了将近一个星期,我已经将其剥离到基本代码,客户端仍然无法接收来自客户端的消息。任何帮助都将深表感谢。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Server {

    Socket socket;
    JTextArea textShow;
    PrintWriter writer;
    static String message;

    JButton buttonSend;

    // LAUNCH
    public static void main(String[] args) throws Exception {
        Server servobj = new Server();
        servobj.Server();

    }

    public void Server() throws Exception {

        // dECLARE
        JFrame frame = new JFrame();
        frame.setTitle("The Server");
        JPanel panel = new JPanel();
        textShow = new JTextArea(10, 40);
        JTextField textSend = new JTextField(35);
        buttonSend = new JButton("SEND");
        JScrollPane scrollPane = new JScrollPane(textShow,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        // INITIALIZE
        frame.getContentPane().add(panel);
        panel.add(textShow);
        textShow.setLineWrap(true);
        panel.add(scrollPane);
        panel.add(textSend);
        panel.add(buttonSend);
        frame.setSize(500, 500);
        frame.setVisible(true);
        setupServer();

    }

    // SETTING UP THE SERVERS
    public void setupServer() throws Exception {

        ServerSocket servSock = new ServerSocket(443);

        socket = servSock.accept();
        System.out.println("Server Socket accepted");

        writer = new PrintWriter(socket.getOutputStream());
        writer.println("Hello");
        System.out.println("MESSAGE SENT");

        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        message = reader.readLine();
        System.out.println(message);

    }

}
import java.awt.TextField;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client {

    Socket socket;
    PrintWriter writer;
    static JTextArea textShow;
    TextField textSend;

    // LAUNCH
    public static void main(String[] args) throws Exception {
        Client clientobj = new Client();
        clientobj.Client();

    }

    public void Client() throws Exception {

        // dECLARE
        JFrame frame = new JFrame();
        frame.setTitle("Client");
        JPanel panel = new JPanel();
        textShow = new JTextArea(10, 40);
        JTextField textSend = new JTextField(35);
        JButton buttonSend = new JButton("SEND");
        JScrollPane scrollPane = new JScrollPane(textShow,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        // INITIALIZE
        frame.getContentPane().add(panel);
        panel.add(textShow);
        textShow.setLineWrap(true);
        panel.add(scrollPane);
        panel.add(textSend);
        panel.add(buttonSend);
        frame.setSize(500, 500);
        frame.setVisible(true);

        setupConnections();
    }

    // SETTING UP THE CONNECTION
    public void setupConnections() throws Exception {

        Socket socket = new Socket("localhost", 443);
        System.out.println("Client accepted");

        // SETUP RECIEVE MESSAGE
        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        System.out.println("Done sam");
        String Message = reader.readLine();
        System.out.println(Message);
        System.out.println("Sent");

        // SETUP SEND MESSAGE
        writer = new PrintWriter(socket.getOutputStream());
        writer.println("Hello G");
        writer.flush();
        System.out.println("Network Established");

    }
}

1 个答案:

答案 0 :(得分:1)

似乎你知道可能是什么问题,但你并没有在任何地方纠正它。

在您的客户中,您有

// SETUP SEND MESSAGE
writer = new PrintWriter(socket.getOutputStream());
writer.println("Hello G");
writer.flush();

所以你意识到PrintWriter不会立即发送你的消息,而是将它放在内部缓冲区中,所以强制发送你调用flush

但是在服务器中你没有刷新那条消息

writer = new PrintWriter(socket.getOutputStream());
writer.println("Hello");
System.out.println("MESSAGE SENT");

避免此类问题的最简单方法是使用PrintWriter,它会自动刷新,就像使用println时一样。这样的编写器可以由PrintWriter(OutputStream out, boolean autoFlush)构造函数

创建

我们可以在那里读到

  

autoFlush - 布尔值;如果为true,则printlnprintfformat方法将刷新输出缓冲区

因此,请考虑在两个类中使用此构造函数

writer = new PrintWriter(socket.getOutputStream(),true);

当您通过println发送消息时,应自动刷新消息。

您当前的代码中还存在其他可能的问题

  • 您正在创建本地套接字变量而不是初始化套接字字段
  • 你有与构造函数同名的方法,这不是一个好习惯,所以不是

    public void Client(){
    
    }
    
    Client client = new Client();
    client.Client();//<-here you are calling your method
    

    您应该删除返回类型void以生成该方法构造函数,以便您的代码仅减少为

    Client client = new Client();