Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package LR1.server;
- import javax.xml.crypto.Data;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Map;
- public class Server implements Runnable {
- private int port;
- public Server(int port) {
- this.port = port;
- }
- public void handleClient(Socket client) {
- String message = null;
- try {
- DataInputStream input = new DataInputStream(client.getInputStream());
- message = input.readUTF();
- System.out.println("Received client's message: " + message);
- } catch (IOException e) {
- System.out.println("Failed to receive client's message");
- e.printStackTrace();
- return;
- }
- String reversed = new StringBuilder(message).reverse().toString();
- try {
- DataOutputStream outputStream = new DataOutputStream(client.getOutputStream());
- outputStream.writeUTF(reversed);
- System.out.println("Sent response: " + reversed);
- System.out.println("Client disconnected.");
- System.out.println();
- } catch (IOException ex) {
- System.out.println("Couldn't send response: ");
- ex.printStackTrace();
- System.out.println();
- }
- }
- @Override
- public void run() {
- ServerSocket server;
- try {
- server = new ServerSocket(this.port);
- System.out.println("Server is running...");
- System.out.println();
- } catch (IOException e) {
- System.out.println("Couldn't start server: ");
- e.printStackTrace();
- return;
- }
- while (true) {
- Socket client = null;
- try {
- client = server.accept();
- handleClient(client);
- client.close();
- } catch (IOException e) {
- System.out.println("Couldn't wait for client's connection: ");
- e.printStackTrace();
- }
- }
- }
- }
- package LR1.server;
- public class ServerApp {
- private static final int port = 8080;
- public static void main(String[] args) {
- Server server = new Server(port);
- server.run();
- }
- }
- package LR1.client;
- import java.io.*;
- import java.net.ConnectException;
- import java.net.Socket;
- public class Client {
- private static final int port = 8080;
- private static Socket client;
- private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- public static void handleMessage(String message) throws IOException {
- DataOutputStream output = new DataOutputStream(client.getOutputStream());
- DataInputStream input = new DataInputStream(client.getInputStream());
- output.writeUTF(message);
- String result = input.readUTF();
- System.out.println("Server response: " + result);
- }
- public static void main(String[] args) throws IOException {
- try {
- client = new Socket("localhost", port);
- System.out.println("Connected");
- } catch (ConnectException ex) {
- System.out.println("Couldn't connect to the server");
- return;
- }
- System.out.print("Enter word: ");
- String message = br.readLine();
- try {
- handleMessage(message);
- } catch (IOException ex) {
- System.out.println("Couldn't send message to server.");
- }
- client.close();
- System.out.println("Connection closed.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement