Answer the question
In order to leave comments, you need to log in
Java. Sockets. How to make the server constantly listen until the button is pressed?
Good evening!
I have a program with a single form (Swing) that presents a graphical interface for two identical applications. That is, by running the class with a graphical interface twice, we get 2 applications that have a Toggle Button - listen and Send (a regular button) - send, as well as 2 text fields: 1) just text - textForFile, 2) text in as bytes - textInBytes.
When you enter text into a text field, it is written as bytes to another text field. By clicking on the Send button, the message arrives in another application (with an identical graphical interface), where the Listen button is already pressed and is written in the form of bytes and text in the corresponding text fields.
Everything works, but the server listens only once, how to make the server listen all the time, and when a message arrives, it immediately fills the text fields, and when the listen button is pressed in an unclipped state, the resources are released?
Tried both with infinite while and with threads, but without success.
Listen button click handler:
private void listenActionPerformed(java.awt.event.ActionEvent evt) {
// Если нажата кнопка Прослушать:
if (listen.isSelected()) {
Server.main(); // запуск сервера
} else {
}
}
private void sendActionPerformed(java.awt.event.ActionEvent evt) {
// Запись текста в байтах в текстовое поле:
textInBytes.setText(Arrays.toString(textForFile.getText().getBytes()));
Client.main(); // запуск клиента
}
import static application.UserInterface.textForFile;
import static application.UserInterface.textInBytes;
import java.io.*;
import java.net.*;
import java.util.Arrays;
public class Server {
private static final int port = 6666; // порт сервера
public static void main() {
InetAddress ia = null;
try {
ia = InetAddress.getByName("localhost");
} catch (UnknownHostException e) {
System.out.println("Server not found: " + e.getMessage());
}
try (ServerSocket serverSocket = new ServerSocket(port, 0, ia)) {
Socket socket = serverSocket.accept();
try (DataInputStream dIn = new DataInputStream(socket.getInputStream())) {
byte[] bytes = null;
int length = dIn.readInt();
if (length > 0) {
bytes = new byte[length];
dIn.readFully(bytes, 0, bytes.length);
textInBytes.setText(Arrays.toString(bytes));
textForFile.setText(new String(bytes));
}
}
} catch (IOException e) {
System.out.println("I/O error: " + e.getMessage());
}
}
}
import static application.UserInterface.textForFile;
import java.io.*;
import java.net.*;
public class Client {
private static final int serverPort = 6666;
private static final String localhost = "127.0.0.1";
public static void main() {
InetAddress ipAddress = null;
DataOutputStream dOut = null;
try {
ipAddress = InetAddress.getByName(localhost);
} catch (UnknownHostException e) {
System.out.println("Server not found: " + e.getMessage());
}
try (Socket socket = new Socket(ipAddress, serverPort)) {
byte[] bytes = textForFile.getText().getBytes();
if (dOut == null) {
dOut = new DataOutputStream(socket.getOutputStream());
}
dOut.writeInt(bytes.length);
dOut.write(bytes);
} catch (UnknownHostException e) {
System.out.println("Server not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("I/O error: " + e.getMessage());
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question