欢迎分享,转载请注明来源:内存溢出
对于socket通信的 *** 作,不要放在主线程,另开一个子线程,顺便一说,accept这个方法是堵塞的,在没有收到消息的情况下会一直堵塞在那里,所以如果你客户端没发送数据给服务端,那么基本服务端所在那条线程会停滞在那个方法,估计就这原因让你的程序看起来是卡死的,所以咯,别把通信的 *** 作写在主线程,要写在子线程服务器端: package chat_01import java.awt.BorderLayoutimport java.io.DataInputStreamimport java.io.DataOutputStreamimport java.io.IOExceptionimport java.net.ServerSocketimport java.net.Socketimport java.text.SimpleDateFormatimport java.util.Dateimport javax.swing.JFrameimport javax.swing.JTextAreapublic class ChatServer extends JFrame { private static final long serialVersionUID = 1LSimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")private JTextArea jtaprivate DataInputStream disprivate DataOutputStream dosprivate ServerSocket serverSocketprivate String dateStringpublic static void main(String[] args) { ChatServer server = new ChatServer()server.showServer()server.send()} public void showServer() { jta = new JTextArea()this.setLayout(new BorderLayout())this.add(jta, BorderLayout.CENTER)this.setLocation(300, 100)this.setSize(500, 400)this.setTitle("服务器端")this.setVisible(true)this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)} public void send() { try { serverSocket = new ServerSocket(8007)while (true) { Socket socket = serverSocket.accept()System.out.println("一个用户连接上了。。。")dis = new DataInputStream(socket.getInputStream())dos = new DataOutputStream(socket.getOutputStream())new Thread(new ClientThread()).start()} } catch (IOException e) { e.printStackTrace()} } private class ClientThread implements Runnable { public void run() { while (true) { try { Date currentTime = new Date()dateString = format.format(currentTime)String str = dis.readUTF()dos.writeUTF(str)jta.append(dateString + "\n" + str + "\n")}catch (IOException e) { e.printStackTrace()} } } } } 客户端: package chat_01import java.awt.*import java.awt.event.*import java.io.*import java.net.Socketimport java.net.UnknownHostExceptionimport java.text.SimpleDateFormatimport java.util.Dateimport javax.swing.*public class ChatClient extends JFrame implements ActionListener { private static final long serialVersionUID = 1Lprivate JTextArea jtaprivate JTextField jtfprivate JButton jbtDataOutputStream dosDataInputStream dispublic static void main(String[] args) { ChatClient client = new ChatClient()client.connected()client.showClient()} public void showClient() { jta = new JTextArea()jta.setEditable(false)jtf = new JTextField()jbt = new JButton("发送")JPanel panel = new JPanel()panel.setLayout(new BorderLayout())panel.add(jtf, BorderLayout.CENTER)panel.add(jbt, BorderLayout.EAST)this.setLayout(new BorderLayout())this.add(jta, BorderLayout.CENTER)this.add(panel, BorderLayout.SOUTH)jtf.addActionListener(this)jbt.addActionListener(this)this.setLocation(300, 100)this.setSize(500, 400)this.setTitle("客户端")this.setVisible(true)this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)} public void actionPerformed(ActionEvent e) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")Date currentTime = new Date()String dateString = format.format(currentTime)String text = jtf.getText()jta.append(dateString + "\n")jta.append(text + "\n")jtf.setText("")jtf.grabFocus()// 文本框获取光标(焦点) try { dos.writeUTF(text)dos.flush()jta.append(dis.readUTF())System.out.println(text + "client")} catch (IOException e1) { e1.printStackTrace()} } public void connected() { try { Socket socket = new Socket("127.0.0.1", 8007)dos = new DataOutputStream(socket.getOutputStream())dis = new DataInputStream(socket.getInputStream())} catch (UnknownHostException e) { e.printStackTrace()} catch (IOException e) { e.printStackTrace()} } }
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
红娘测姻缘小程序,“解测姻缘”的软件
上一篇
2023-05-21
怎样用C语言编写心形?
下一篇
2023-05-21
评论列表(0条)