Search code examples
javaswingsocketsserverclient

How can i send multiple information from a client application to a server?


I'm new in regards to sockets and I would like to know how can i take what is written in several textfields from the client application and put that data into labels located in the Server application depending on the textfield that they come from.

So far I made the connection between the client and the server and when you press the button in the client class you can send the string from the textfield and succesfully write that in the label located in the server class.

Here I have the Client class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

class Client extends JFrame implements ActionListener{
    JTextField field1;
    JTextField field2;
    JPanel panel;
    JButton btnStart;
    JLabel lblText;

    public Client(){
        panel = new JPanel();
        panel.setLayout(null);

        lblText=new JLabel("CLIENT");
        lblText.setBounds(75,0,150,30);
        field1=new JTextField(20);
        field1.setBounds(10,40,225,30);
        field2=new JTextField(20);
        field2.setBounds(10,80,225,30);
        btnStart=new JButton("Send");
        btnStart.setBounds(75,100,75,30);
        btnStart.addActionListener(this);

        panel.add(btnStart);
        panel.add(field1);
        panel.add(field2);
        panel.add(lblText);

        this.add(panel);
        this.setBounds(600,300,275,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent event){
        if(event.getSource() == this.btnStart){
            try {
                Socket socket = new Socket("192.168.1.90",9000);
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                out.writeUTF(field1.getText());
                out.close();
                field1.setText("");/*
                DataOutputStream out2 = new DataOutputStream(socket.getOutputStream());
                out2.writeUTF(field2.getText());
                out2.close();
                */
            }catch (Exception e) {
                System.out.println("Connection failed");
            }

        }
    }
}

Server class

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;

class Server extends JFrame implements Runnable{
  JLabel lblUsername;
  JLabel lblNss;
  JPanel panel;
  Thread thread1;

  public Server(){
    panel= new JPanel();
        panel.setLayout(null);
        lblUsername = new JLabel("");
    lblUsername.setBounds(0,0,150,30);
    lblNss = new JLabel("");
    lblNss.setBounds(0,40,150,30);
    panel.add(lblUsername);panel.add(lblNss);

    thread1 = new Thread(this);
    thread1.start();


        this.add(panel);
    this.setBounds(1200,300,280,350);
        this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
  public void run(){
    try {
      ServerSocket server = new ServerSocket(9000); 
      while(true){
        Socket socket = server.accept(); 
        DataInputStream in = new DataInputStream(socket.getInputStream());
        String text_message = in.readUTF();
        lblUsername.setText(text_message);
        //lblNss.setText();
        socket.close();
      }
    }catch (Exception e) {
      e.printStackTrace();
    }

  }
}

Solution

  • Thanks to @Frakcool for the help.

    "To send multiple Strings, you need to create your own custom object, add all the info from the text fields to it, then send the object, on the server side, use that object fields to populate the labels"

    Here I created the custom object that would recieve the strings from the textfields

    import javax.swing.*;
    import java.awt.*;
    
    class Object extends JFrame{
      String name;
      String nss;
    
      public Object(String field1, String field2){
       this.name = field1;
       this.nss = field2;
      }
    }
    

    Here is part of the Client application, when a button is pressed you can send the info to the server

    public void actionPerformed(ActionEvent event){
        if(event.getSource() == this.btnEnviar){
            try {
                Socket socket = new Socket("192.168.1.90",9000); // Server IP
                ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
                Object o = new Object(name.getText(),nss.getText()); //Here I would created the object based on the data from two different JTextFields
                os.writeObject(o);
                os.close();
                name.setText("");
                nss.setText("");
            }catch (Exception e) {
                System.out.println("Connection failed");
            }
    
        }
    }
    

    So in the server you need to create the object and if needed you can store the atributtes of the object

    public void run(){
      try {
        ServerSocket server = new ServerSocket(9000);
        while(true){
          Socket socket = server.accept(); 
          ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
          Objeto o = (Objeto)is.readObject();
          lblNombre.setText(o.nombre);
          lblNSS.setText(o.nss);
          socket.close();
        }
      }catch (Exception e) {
        e.printStackTrace();
      }
     }