java—使用套接字将jframe图形从客户端发送到服务器

yuvru6vn  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(364)

我正试图发送我的java文件,其中有一个jframe绘图按钮,它可以将背景的颜色从客户机更改为服务器。服务器接收该绘图并打开它,但当我单击按钮时,没有任何更改。我做错什么了?由于某些原因,应用程序有时无法运行。
按图纸编码

  1. package Drawings;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.geom.Rectangle2D;
  6. import javax.swing.*;
  7. public class DrawingTwo extends JFrame {
  8. public static final int DEFAULT_WIDTH = 300;
  9. public static final int DEFAULT_HEIGHT = 300;
  10. public Color tvColor;
  11. public Color smileColor;
  12. public int h;
  13. public int h2;
  14. public int h3;
  15. public int h4;
  16. public int h5;
  17. public int h6;
  18. public String l;
  19. public DrawComponent c;
  20. public static void main(String[] args) {
  21. EventQueue.invokeLater(new Runnable() {
  22. @Override
  23. public void run() {
  24. new DrawingTwo();
  25. }
  26. });
  27. }
  28. public DrawingTwo() {
  29. super();
  30. setOneChanell();
  31. Container container = getContentPane();
  32. container.setBackground(new Color(242, 212, 252));
  33. container.setLayout(new BorderLayout(20, 20));
  34. container.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
  35. ButtonListener listener = new ButtonListener();
  36. JLabel lb = new JLabel(l);
  37. lb.setText(l);
  38. container.add(lb);
  39. JButton j2 = new JButton("2");
  40. j2.addActionListener(listener);
  41. container.add(j2, BorderLayout.NORTH);
  42. c = new DrawComponent();
  43. container.add(c, BorderLayout.CENTER);
  44. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  45. pack();
  46. setVisible(true);
  47. }
  48. public void setOneChanell() {
  49. this.tvColor = new Color(255, 153, 153);
  50. this.smileColor = new Color(255, 247, 10);
  51. this.h = 110;
  52. this.h2 = 55;
  53. this.h3 = 60;
  54. this.h4 = 60;
  55. this.h5 = 0;
  56. this.h6 = -180;
  57. this.l = "Канал для веселых";
  58. }
  59. public void setTwoChanell() {
  60. this.tvColor = new Color(172, 194, 157);
  61. this.smileColor = new Color(0, 161, 219);
  62. this.h = 115;
  63. this.h2 = 87;
  64. this.h3 = 50;
  65. this.h4 = 40;
  66. this.h5 = 0;
  67. this.h6 = +180;
  68. this.l = "Канал для грустных";
  69. }
  70. public class DrawComponent extends JComponent {
  71. @Override
  72. public void paintComponent(Graphics g) {
  73. super.paintComponent(g);
  74. g.setColor(Color.black);
  75. g.fillRect(37, 26, 210, 130);
  76. g.setColor(tvColor);
  77. g.fillRect(42, 30, 200, 120);
  78. g.setColor(Color.darkGray);
  79. g.fillRect(135, 156, 15, 20);
  80. g.setColor(Color.darkGray);
  81. g.fillRect(83, 170, 120, 13);
  82. g.setColor(smileColor);
  83. g.fillOval(100, 45, 80, 80);
  84. g.setColor(Color.BLACK);
  85. g.drawArc(120, 70, 10, 10, 0, 360);
  86. g.drawArc(150, 70, 10, 10, 0, 360);
  87. g.drawString(l, 83, 200);
  88. g.setColor(Color.BLACK);
  89. g.drawArc(h, h2, h3, h4, h5, h6);
  90. }
  91. }
  92. public class ButtonListener implements ActionListener {
  93. @Override
  94. public void actionPerformed(ActionEvent event) {
  95. JButton button = (JButton) event.getSource();
  96. if (button.getText().equals("2")) {
  97. setTwoChanell();
  98. button.setText("1");
  99. } else {
  100. setOneChanell();
  101. button.setText("2");
  102. }
  103. c.repaint();
  104. }
  105. }
  106. }

这是客户端文件

  1. package ClientToServer;
  2. import java.io.*;
  3. import java.net.Socket;
  4. import Drawings.DrawingTwo;
  5. public class Client {
  6. public static void main(String[] args) throws IOException {
  7. Socket socket = new Socket("localhost", 12345);
  8. OutputStream outputStream = socket.getOutputStream();
  9. ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
  10. objectOutputStream.writeObject(new DrawingTwo());
  11. objectOutputStream.flush();
  12. objectOutputStream.close();
  13. }
  14. }

这是服务器文件

  1. package ClientToServer;
  2. import java.io.*;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import Drawings.DrawingTwo;
  6. import javax.swing.*;
  7. public class Server {
  8. public static void main(String[] args) throws IOException, ClassNotFoundException {
  9. ServerSocket serverSocket = new ServerSocket(12345);
  10. Socket client = serverSocket.accept();
  11. ObjectInputStream inputStream = new ObjectInputStream(client.getInputStream());
  12. DrawingTwo object = (DrawingTwo) inputStream.readObject();
  13. object.setVisible(true);
  14. object.setTitle("Server");
  15. object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. client.close();
  17. inputStream.close();
  18. serverSocket.close();
  19. }
  20. }
zwghvu4y

zwghvu4y1#

我建议你多读一点关于socket通信的知识。如果您有一个服务器套接字,它应该一直运行。在您的代码中,当您从客户机接收到数据后立即关闭套接字连接。
另一件事是;只有一种类型限制了通信:drawingtwo()类。在服务器端,您无法接收任何其他数据。
让我们一步一步地看看你的代码。
必须为通信目的定义新对象

  1. import java.io.Serializable;
  2. public class CommunicationObject implements Serializable{
  3. private DrawingTwo mDrawingTwo;
  4. private boolean mSmileyFace = true;
  5. private boolean mIsInitialConnection = true;
  6. public DrawingTwo getmDrawingTwo() {
  7. return mDrawingTwo;
  8. }
  9. public void setmDrawingTwo(DrawingTwo mDrawingTwo) {
  10. this.mDrawingTwo = mDrawingTwo;
  11. }
  12. public boolean ismSmileyFace() {
  13. return mSmileyFace;
  14. }
  15. public void setmSmileyFace(boolean mSmileyFace) {
  16. this.mSmileyFace = mSmileyFace;
  17. }
  18. public boolean ismIsInitialConnection() {
  19. return mIsInitialConnection;
  20. }
  21. public void setmIsInitialConnection(boolean mIsInitialConnection) {
  22. this.mIsInitialConnection = mIsInitialConnection;
  23. }
  24. }

这里有3个字段。
图纸2;你的实际布局是什么
布尔笑脸;指示按钮的情况
布尔初始连接;决定是初始化以便服务器初始化布局还是更改笑脸
你的服务器必须一直监听

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.ObjectInputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import javax.swing.JFrame;
  7. public class Server {
  8. static boolean isRunning = true;
  9. public static void main(String[] args) throws IOException, ClassNotFoundException {
  10. ServerSocket serverSocket = new ServerSocket(12345);
  11. Socket client = null;
  12. ObjectInputStream inputStream = null;
  13. InputStream is = null;
  14. DrawingTwo drawingTwo = null;
  15. CommunicationObject communicationObject = null;
  16. while (isRunning) {
  17. client = serverSocket.accept();
  18. inputStream = new ObjectInputStream(client.getInputStream());
  19. is = client.getInputStream();
  20. communicationObject = (CommunicationObject) inputStream.readObject();
  21. if (communicationObject.ismIsInitialConnection()) {
  22. drawingTwo = (DrawingTwo) communicationObject.getmDrawingTwo();
  23. drawingTwo.setVisible(true);
  24. drawingTwo.setTitle("Server");
  25. drawingTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. } else {
  27. if (communicationObject.ismSmileyFace()) {
  28. drawingTwo.setOneChanell();
  29. } else {
  30. drawingTwo.setTwoChanell();
  31. }
  32. drawingTwo.c.repaint();
  33. }
  34. }
  35. client.close();
  36. inputStream.close();
  37. serverSocket.close();
  38. }
  39. }

请注意,isrunning现在总是正确的。必须处理异常和其他情况才能停止或重新启动它。
在drawingtwo类中为jbutton创建一个getter

  1. import javax.swing.JButton;
  2. import javax.swing.JComponent;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.WindowConstants;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.geom.Rectangle2D;
  10. import java.beans.PropertyChangeListener;
  11. public class DrawingTwo extends JFrame{
  12. public static final int DEFAULT_WIDTH = 300;
  13. public static final int DEFAULT_HEIGHT = 300;
  14. public Color tvColor;
  15. public Color smileColor;
  16. public int h;
  17. public int h2;
  18. public int h3;
  19. public int h4;
  20. public int h5;
  21. public int h6;
  22. public String l;
  23. public DrawComponent c;
  24. private JButton j2;
  25. public static void main(String[] args) {
  26. EventQueue.invokeLater(new Runnable() {
  27. @Override
  28. public void run() {
  29. new DrawingTwo();
  30. }
  31. });
  32. }
  33. public DrawingTwo() {
  34. super();
  35. setOneChanell();
  36. Container container = getContentPane();
  37. container.setBackground(new Color(242, 212, 252));
  38. container.setLayout(new BorderLayout(20, 20));
  39. container.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
  40. ButtonListener listener = new ButtonListener();
  41. JLabel lb = new JLabel(l);
  42. lb.setText(l);
  43. container.add(lb);
  44. j2 = new JButton("2");
  45. j2.addActionListener(listener);
  46. container.add(j2, BorderLayout.NORTH);
  47. c = new DrawComponent();
  48. container.add(c, BorderLayout.CENTER);
  49. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  50. pack();
  51. setVisible(true);
  52. }
  53. public void setOneChanell() {
  54. this.tvColor = new Color(255, 153, 153);
  55. this.smileColor = new Color(255, 247, 10);
  56. this.h = 110;
  57. this.h2 = 55;
  58. this.h3 = 60;
  59. this.h4 = 60;
  60. this.h5 = 0;
  61. this.h6 = -180;
  62. this.l = "1";
  63. }
  64. public void setTwoChanell() {
  65. this.tvColor = new Color(172, 194, 157);
  66. this.smileColor = new Color(0, 161, 219);
  67. this.h = 115;
  68. this.h2 = 87;
  69. this.h3 = 50;
  70. this.h4 = 40;
  71. this.h5 = 0;
  72. this.h6 = +180;
  73. this.l = "2";
  74. }
  75. public JButton getJButton() {
  76. return j2;
  77. }
  78. public class DrawComponent extends JComponent {
  79. @Override
  80. public void paintComponent(Graphics g) {
  81. super.paintComponent(g);
  82. g.setColor(Color.black);
  83. g.fillRect(37, 26, 210, 130);
  84. g.setColor(tvColor);
  85. g.fillRect(42, 30, 200, 120);
  86. g.setColor(Color.darkGray);
  87. g.fillRect(135, 156, 15, 20);
  88. g.setColor(Color.darkGray);
  89. g.fillRect(83, 170, 120, 13);
  90. g.setColor(smileColor);
  91. g.fillOval(100, 45, 80, 80);
  92. g.setColor(Color.BLACK);
  93. g.drawArc(120, 70, 10, 10, 0, 360);
  94. g.drawArc(150, 70, 10, 10, 0, 360);
  95. g.drawString(l, 83, 200);
  96. g.setColor(Color.BLACK);
  97. g.drawArc(h, h2, h3, h4, h5, h6);
  98. }
  99. }
  100. public class ButtonListener implements ActionListener {
  101. @Override
  102. public void actionPerformed(ActionEvent event) {
  103. JButton button = (JButton) event.getSource();
  104. if (button.getText().equals("2")) {
  105. setTwoChanell();
  106. button.setText("1");
  107. } else {
  108. setOneChanell();
  109. button.setText("2");
  110. }
  111. c.repaint();
  112. }
  113. }
  114. }

记住你的按钮有两个功能
更改您已经处理过的客户机上的布局
发送信号到你的服务器,这样它也可以改变它
最后是client类。

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.io.IOException;
  4. import java.io.ObjectOutputStream;
  5. import java.io.OutputStream;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8. import javax.swing.JButton;
  9. public class Client {
  10. static OutputStream outputStream;
  11. static ObjectOutputStream objectOutputStream;
  12. static Socket socket;
  13. public static void main(String[] args) throws UnknownHostException, IOException {
  14. DrawingTwo drawingTwo = new DrawingTwo();
  15. CommunicationObject communicationObject = new CommunicationObject();
  16. communicationObject.setmDrawingTwo(drawingTwo);
  17. communicationObject.setmIsInitialConnection(true);
  18. write(communicationObject);
  19. JButton button = drawingTwo.getJButton();
  20. button.addActionListener(new ActionListener() {
  21. @Override
  22. public void actionPerformed(ActionEvent arg0) {
  23. try {
  24. if (button.getText().equals("2")) {
  25. communicationObject.setmIsInitialConnection(false);
  26. communicationObject.setmSmileyFace(false);
  27. write(communicationObject);
  28. } else {
  29. communicationObject.setmIsInitialConnection(false);
  30. communicationObject.setmSmileyFace(true);
  31. write(communicationObject);
  32. }
  33. } catch (Exception e) {
  34. // TODO: handle exception
  35. }
  36. }
  37. });
  38. }
  39. public static void write(CommunicationObject comObject) throws IOException
  40. {
  41. socket = new Socket("localhost", 12345);
  42. outputStream = socket.getOutputStream();
  43. objectOutputStream = new ObjectOutputStream(outputStream);
  44. objectOutputStream.writeObject(comObject);
  45. objectOutputStream.flush();
  46. objectOutputStream.close();
  47. }
  48. }

在这里创建一个communicationobject,并将drawingtwo类指定给它。
看看从哪里将jbutton从drawingtwo提取到客户机类中,这样就可以将actionlistener绑定到它。通过这个动作监听器,您现在可以与服务器通信了。
注意,由于客户机和服务器都可以访问drawingtwo,所以不需要通过socket发送整个类。只需向服务器发送一条消息,它应该自己创建一个示例。

展开查看全部

相关问题