Untitled

 avatar
unknown
plain_text
10 months ago
2.7 kB
4
Indexable
import java.io.*;
import java.net.*;
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(5000); // create a server socket object listening on port 5000
        System.out.println("Server started...");

        Socket clientSocket = serverSocket.accept(); // wait for a client to connect and accept the connection
        System.out.println("Client connected...");

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); // create a PrintWriter object to send data to the client
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // create a BufferedReader object to read data from the client
        String inputLine;

        while ((inputLine = in.readLine()) != null) { // read lines of data from the client
            System.out.println("Received message: " + inputLine);
            out.println("Server received message: " + inputLine); // send a response back to the client
        }
        out.close(); // close the PrintWriter
        in.close(); // close the BufferedReader
        clientSocket.close(); // close the client socket
        serverSocket.close(); // close the server socket
    }
}










Client Code
import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) throws IOException {
        String serverHostname = "localhost"; // server hostname
        int serverPort = 5000; // server port

        Socket socket = new Socket(serverHostname, serverPort); // create a socket object to connect to the server
        System.out.println("Connected to server...");

        PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // create a PrintWriter object to send data to the server

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // create a BufferedReader object to read data from the server

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); // create a BufferedReader object to read user input from the console

        String userInput;

        while ((userInput = stdIn.readLine()) != null) { // read lines of user input
            out.println(userInput); // send the user input to the server
            String serverResponse = in.readLine(); // read the response from the server
            System.out.println("Server response: " + serverResponse);
        }

        out.close(); // close the PrintWriter
        in.close(); // close the BufferedReader
        stdIn.close(); // close the standard input
        socket.close(); // close the socket
    }
}
Editor is loading...
Leave a Comment