Devi_cry
2016-06-27 03:20
采纳率: 66.7%
浏览 1.2k

JAVA基础关于Scoket与GUI的编程问题,十万火急!!!!!!!!!!

我用Scoket编了HELLO WORLD 级别的服务端与客户端

 import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8686);
            Socket client = server.accept();

            InputStream is = client.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            OutputStream os = client.getOutputStream();
            PrintStream ps = new PrintStream(os);
            Scanner scanner = new Scanner(System.in);

            boolean flag = true;
            while (flag) {
                String s = br.readLine();
                System.out.println(s);
                if ( s.equals("ok")) {
                    System.out.println("连接结束");
                    break;
                }

                String s2 = scanner.nextLine();
                ps.println(s2); 
            }
            client.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
 import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        try {
            Socket client = new Socket("192.168.1.113", 8686);// IP地址与端口

            InputStream is = client.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));

            OutputStream os = client.getOutputStream();
            PrintStream ps = new PrintStream(os);
            Scanner scanner = new Scanner(System.in);
            boolean flag = true;
            while (flag)
            {
                String s2 = scanner.nextLine();
                ps.println(s2);
                if ( s2.equals("end")) {
                    System.out.println("连接结束");
                    break;
                }

                String s = bf.readLine();
                System.out.println(s);
            }

            client.close();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

想设计一个界面 ,图片说明

可是总有各种各样的问题,希望哪个好心人能教教我

在此感谢,

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • angle_sun 2016-06-27 03:30

    错误是什么? 没有界面代码啊

    打赏 评论
  • Devi_cry 2016-06-27 03:33

    抱歉了,,,界面代码:

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class Myserver extends JFrame {
    
        private JPanel contentPane;
        private JTextField textField;
        private JTextArea textArea;
    
        private BufferedReader reader;
        private ServerSocket server;
        private Socket socket;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Myserver frame = new Myserver();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public Myserver() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 300, 302);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
    
            textField = new JTextField();
            textField.setBounds(10, 215, 196, 36);
            contentPane.add(textField);
            textField.setColumns(10);
    
            JButton btnNewButton = new JButton("\u53D1\u9001");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                }
            });
            btnNewButton.setBounds(210, 215, 64, 36);
            contentPane.add(btnNewButton);
    
            textArea = new JTextArea();
            textArea.setBounds(10, 10, 264, 195);
            contentPane.add(textArea);
    
            try {
                server = new ServerSocket(8998);
                while (true) {
                    socket = server.accept();
                    reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    getClientMessage();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void getClientMessage() {
            // TODO Auto-generated method stub
            try {
                while (true) {
                    textArea.setText("客户机:" + reader.readLine());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    

    run不了的

    打赏 评论
  • Devi_cry 2016-06-27 03:54

    没人吗 好困惑啊 来个人帮帮我,要不直接帮我实现呗 然后我自己理解

    打赏 评论
  • Devi_cry 2016-06-27 04:06

    我把客户端做好了,只剩服务端了,怎么样能让服务端先发消息呢 图片说明

    打赏 评论
  • Devi_cry 2016-06-27 04:06

    是不是悬赏太少了 都没人的

    打赏 评论
  • 我爱铁板烧 2016-06-27 04:18

    netbeans设计java的gui界面很容易的,你要查文档看看JFrame和Jpanel类,就会了

    打赏 评论
  • jzen 2016-06-27 06:13

    服务端界面构造函数不要用于accpet,否则不能创建gui界面。应该tcp连接用一个线程,界面创建一个线程。

    打赏 评论
  • willkinson 2016-06-29 11:14

    你这里的问题多多,首先无限循环阻塞了线程,让后续的代码无法执行,界面当然无法正常显示,还有看你的代码,感觉是想支持多个客户端连接的?
    那就在服务器端每接收到一个客户端的连接就为这个连接新开一个线程,把这个客户端的Sokcet对象扔给这个线程对象去处理

    打赏 评论

相关推荐 更多相似问题