打开JFrame时如何防止socket关闭?

时间:2015-12-18 21:59:36

标签: java swing sockets

被修改

我的问题是我制作了一个简单的客户端服务器程序,它启动一个简单的JFrame,但每次运行它时,套接字都会关闭,服务器终止,尽管我把它放在while循环中

我的服务器代码:

public class Server extends Thread{

Socket socket;
public Server(Socket socket)
{
    this.socket = socket;
}

public synchronized void run()
{
    try {  
        String connect = "Connecting Successfully :)";
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        out.writeUTF(connect);
        Login l = new Login();
        l.setLocation(350, 250);
        l.setSize(400, 350);
        l.setTitle("Inventory Management System");
        l.setResizable(false);
        l.setVisible(true);
        out.writeObject(l);
        out.close();

    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "IOException2: " + ex.getMessage() ,"Fatal Error", JOptionPane.ERROR_MESSAGE);
    }
}

public static void main(String argv[]) throws IOException{

    int i = 0;
    ServerSocket s1 = new ServerSocket(5000);
    while(i == 0)
    {
        Socket ss = s1.accept(); 
        new Server(ss).start();
    }
}
} 

我的客户代码:

    public class Client{

    public static void main(String argv[])
    {

    try{
    Socket s = new Socket("localhost",5000);
    ObjectInputStream in = new ObjectInputStream(s.getInputStream());
    String connect = in.readUTF();
    JOptionPane.showMessageDialog(null, connect, "Server Success", JOptionPane.INFORMATION_MESSAGE);

    Login l = (Login)in.readObject();

    //in.close();

    } catch(ConnectException e){
        JOptionPane.showMessageDialog(null, "Failed to connect to the server.", "Connection Failed", JOptionPane.ERROR_MESSAGE);
    } catch(IOException e){
        JOptionPane.showMessageDialog(null, "IOException: " + e.getMessage(), "Fatal Error", JOptionPane.ERROR_MESSAGE);
    } catch (ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "ClassNotFoundException: " + ex.getMessage(), "Fatal Error", JOptionPane.ERROR_MESSAGE);
    }
    finally{
        JOptionPane.showMessageDialog(null, this , "Warining", JOptionPane.WARNING_MESSAGE);
    }
}
}

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

套接字不会自发关闭。 正在关闭套接字。这里:

out.close();

关闭套接字的输入或输出流会关闭套接字。

答案 1 :(得分:1)

  

当我点击它时我有按钮它应该传递给另一个gui页面。

您的客户代码:

// connect socket
Socket ss = new Socket("localhost", 4000);

// create PrintStream from socket's output stream
// for **one way communication** from client to server only
PrintStream pr = new PrintStream(ss.getOutputStream(), true);

// push two Strings through socket
pr.println("Supplier Orders");
pr.println(Supplier.username);

// close GUI and possibly close program.
this.dispose();

因此,此代码似乎将2个字符串发送到服务器,然后尝试退出。

此外:
在您的服务器中,您使用while true循环完全绑定当前线程,如果这是在GUI事件线程上,GUI将“冻结”。您应该在后台线程中运行所有长时间运行的代码或线程暂停代码,这样它就不会妨碍GUI的事件线程。

如果仍然卡住,请再次创建并发布mcve

修改
我尝试用你的代码制作一个mcve,这就是我得到的:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

public class HisSocket {
    public static void main(final String[] args) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Server.main(args);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {

            @Override
            public void run() {
                Client.main(args);
            }
        }).start();
    }
}

class Server extends Thread {

    Socket socket;

    public Server(Socket socket) {
        this.socket = socket;
    }

    public synchronized void run() {
        try {
            String connect = "Connecting Successfully :)";
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            out.writeUTF(connect);
            Login l = new Login();
            l.setLocation(350, 250);
            l.setSize(400, 350);
            l.setTitle("Inventory Management System");
            l.setResizable(false);
            l.setVisible(true);
            out.writeObject(l);
            out.close();

        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "IOException2: " + ex.getMessage(), "Fatal Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String argv[]) throws IOException {

        int i = 0;
        ServerSocket s1 = new ServerSocket(5000);
        while (i == 0) {
            Socket ss = s1.accept();
            new Server(ss).start();
        }
    }
}

class Login extends JFrame {
    public Login() {
        super("Login");
        // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new JLabel("Login JFrame", SwingConstants.CENTER));
    }
}

class Client {

    public static void main(String argv[]) {

        try {
            Socket s = new Socket("localhost", 5000);
            ObjectInputStream in = new ObjectInputStream(s.getInputStream());
            String connect = in.readUTF();
            JOptionPane.showMessageDialog(null, connect, "Server Success",
                    JOptionPane.INFORMATION_MESSAGE);

            Login l = (Login) in.readObject();

            // in.close();

        } catch (ConnectException e) {
            JOptionPane.showMessageDialog(null, "Failed to connect to the server.",
                    "Connection Failed", JOptionPane.ERROR_MESSAGE);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "IOException: " + e.getMessage(), "Fatal Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "ClassNotFoundException: " + ex.getMessage(),
                    "Fatal Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            // !! I couldn't use this because this doesn't exist in a static context
            // and so your code wouldn't compile.
            // JOptionPane.showMessageDialog(null, this, "Warining",
            // JOptionPane.WARNING_MESSAGE);

            JOptionPane.showMessageDialog(null, "client this", "Warining",
                    JOptionPane.WARNING_MESSAGE);
        }
    }
}

告诉我我应该体验的是什么。