Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import javax.imageio.ImageIO;
- public class Sender extends JFrame {
- private JTextField textFieldSend;
- private JButton buttonSend;
- private JButton getImageButton;
- private JLabel imageLabel;
- public Sender() {
- createGUI();
- startServer();
- }
- private void createGUI() {
- setLayout(new AbsoluteLayout());
- textFieldSend = new JTextField();
- textFieldSend.setBounds(10, 10, 200, 25);
- add(textFieldSend);
- buttonSend = new JButton("Send");
- buttonSend.setBounds(10, 40, 100, 25);
- buttonSend.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String message = textFieldSend.getText();
- try {
- sendString(message); // Call the method to send the string
- } catch (IOException ex) {
- // Handle the exception
- ex.printStackTrace();
- }
- }
- });
- add(buttonSend);
- getImageButton = new JButton("Get Image");
- getImageButton.setBounds(10, 70, 100, 25);
- getImageButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- receiveImage(); // Call the method to receive the image
- }
- });
- add(getImageButton);
- imageLabel = new JLabel();
- imageLabel.setBounds(10, 100, 200, 150);
- add(imageLabel);
- setSize(250, 300);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- private void sendString(String message) throws IOException {
- Socket socket = new Socket("127.0.0.1", 8080); // Connect to the Receiver
- OutputStream outputStream = socket.getOutputStream();
- outputStream.write(message.getBytes()); // Send the string
- outputStream.close();
- socket.close();
- }
- private void receiveImage() {
- try {
- ServerSocket serverSocket = new ServerSocket(8081);
- Socket socket = serverSocket.accept();
- InputStream inputStream = socket.getInputStream();
- BufferedImage image = ImageIO.read(inputStream);
- // Display the received image in a JLabel or any other suitable component
- imageLabel.setIcon(new ImageIcon(image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement