package chatingApplication;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerSide {
	ServerSide() {
		try {
			ServerSocket server = new ServerSocket(2000);
			Socket socket = server.accept();
			DataInputStream di = new DataInputStream(socket.getInputStream());
			DataOutputStream d = new DataOutputStream(socket.getOutputStream());
			Scanner sc = new Scanner(System.in);
			String message_in = "", message_out = "";
			System.out.println("\t\t------Server-----");
			System.out.println();
			while (true) {
				message_in = di.readUTF();
				System.out.println("Message received:"+message_in);
				System.out.println();
				System.out.print("Message to send:");
				message_out = sc.nextLine();
				System.out.println();
				if (message_out.equals("bye")) {
					d.writeUTF("Server is offline");
					break;
				}
				d.writeUTF(message_out);
				d.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new ServerSide();
	}
}
