Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- client
- package LV8;
- import java.net.*;
- import java.io.*;
- //OBA FILE-A OVDJE SU COPYPASTE SA NEKE STRANICE NA NETU
- public class Client
- { private Socket socket = null;
- private DataInputStream console = null;
- private DataOutputStream streamOut = null;
- public Client(String serverName, int serverPort)
- { System.out.println("Establishing connection. Please wait ...");
- try
- { socket = new Socket(serverName, serverPort);
- System.out.println("Connected: " + socket);
- start();
- }
- catch(UnknownHostException uhe)
- { System.out.println("Host unknown: " + uhe.getMessage());
- }
- catch(IOException ioe)
- { System.out.println("Unexpected exception: " + ioe.getMessage());
- }
- String line = "";
- while (!line.equals(".bye"))
- { try
- { line = console.readLine();
- streamOut.writeUTF(line);
- streamOut.flush();
- }
- catch(IOException ioe)
- { System.out.println("Sending error: " + ioe.getMessage());
- }
- }
- }
- public void start() throws IOException
- { console = new DataInputStream(System.in);
- streamOut = new DataOutputStream(socket.getOutputStream());
- }
- public void stop()
- { try
- { if (console != null) console.close();
- if (streamOut != null) streamOut.close();
- if (socket != null) socket.close();
- }
- catch(IOException ioe)
- { System.out.println("Error closing ...");
- }
- }
- public static void main(String args[])
- { Client client = new Client("0.0.0.0", 8081);
- if (args.length != 2)
- System.out.println("Usage: java ChatClient host port");
- else
- client = new Client(args[0], Integer.parseInt(args[1]));
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- server
- package LV8;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Server {
- private ServerSocket serverSocket;
- private Socket clientSocket;
- private DataInputStream streamInput;
- private List<String> BannedWords = Arrays.asList("psovka1", "psovka2", "psovka3", "uvreda1, uvreda2, uvreda3");
- public Server(int port) {
- try {
- System.out.println("Creating server with port: " + port + "...");
- serverSocket = new ServerSocket(port);
- System.out.println("Server started: " + serverSocket);
- System.out.println("Waiting for a client...");
- clientSocket = serverSocket.accept();
- System.out.println("Client accepted:" + clientSocket);
- streamInput = new DataInputStream((new BufferedInputStream(clientSocket.getInputStream())));
- boolean done = false;
- while(!done) {
- try {
- String line = streamInput.readUTF();
- for(String word: BannedWords) {
- if(line.toLowerCase().contains(word)) {
- line = line.replace(word, "***");
- }
- }
- System.out.println(line);
- done = line.equals(".bye");
- } catch (IOException ex) {
- done = true;
- }
- }
- if(clientSocket != null)
- clientSocket.close();
- if(streamInput != null)
- streamInput.close();
- } catch (IOException ex) {
- System.out.println("Error:" + ex.getMessage());
- }
- }
- public static void main(String args[])
- {
- Server server = new Server(8081);
- if (args.length != 1)
- System.out.println("Usage: java ChatServer port");
- else
- server = new Server(Integer.parseInt(args[0]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement