New paste Use cases Explore public pastes Text tools Developer API The Paste Library Journal Security Sign in with Google
JAVACreated 2026-12-036 viewsExpires 2026-12-04

Untitled

Raw New paste
javaRead-only
1
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class TCPServer {

    private static final int PORT = 5000;

    public static void main(String[] args) {

        System.out.println("Uruchamianie serwera...");

        try (ServerSocket server =
                 new ServerSocket(PORT)) {

            System.out.println("Serwer działa!");
            System.out.println("Port: " + PORT);
            System.out.println("Oczekiwanie na telefon...");

            try (Socket client = server.accept()) {

                System.out.println("Telefon połączony!");

                BufferedReader input =
                    new BufferedReader(
                        new InputStreamReader(
                            client.getInputStream(), "UTF-8"));

                PrintWriter output =
                    new PrintWriter(
                        new OutputStreamWriter(
                            client.getOutputStream(), "UTF-8"),
                        true);

                Thread receiver = new Thread(() -> {
                    try {
                        String message;

                        while ((message = input.readLine()) != null) {
                            System.out.println(
                                "ANDROID: " + message);
                        }

                    } catch (IOException e) {
                        System.out.println(
                            "Połączenie zakończone.");
                    }
                });

                receiver.start();

                Scanner scanner = new Scanner(System.in);

                while (client.isConnected()) {
                    String message = scanner.nextLine();

                    output.println(message);

                    if (message.equalsIgnoreCase("exit")) {
                        break;
                    }
                }

                System.out.println("Zamykanie serwera...");

            }

        } catch (IOException e) {
            System.out.println("Błąd serwera: " +
                e.getMessage());
        }
    }
}
Report abuse

Paste details

Visibility
Public
Size
2.1 KB
Protection
Standard link access
Retention
Until 2026-12-04

Share with confidence

Unlisted links are not searchable, but anyone with the URL can open them. Never paste live credentials or personal data.