Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.io.IOException;
- class Odbior extends Thread {
- Socket sock;
- BufferedReader sockReader;
- PrintWriter sockWriter;
- public Odbior(Socket sock) throws IOException {
- this.sock = sock;
- this.sockReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- }
- public void run() {
- try {
- String str;
- while(true) {
- if (sockReader.ready()) {
- str = this.sockReader.readLine();
- if (str.equalsIgnoreCase("koniec")) {
- sock.shutdownOutput();
- break;
- }
- System.out.println(str);
- }
- else if (sock.isInputShutdown()) {
- break;
- }
- }
- }
- catch (Exception e) {
- e.printStackTrace();;
- }
- }
- }
- class Nadaj extends Thread
- {
- Socket sock;
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter sockWriter;
- String nick;
- public Nadaj(Socket sock, String nick) throws IOException
- {
- this.sock = sock;
- this.nick = nick;
- this.sockWriter = new PrintWriter(sock.getOutputStream());
- }
- public void run() {
- try {
- PrintWriter out = this.sockWriter;
- String str;
- while (true) {
- if(in.ready()) {
- str = in.readLine();
- if (str.equalsIgnoreCase("koniec")) {
- out.println(str);
- sock.shutdownInput();
- in.close();
- break;
- }
- out.println(this.nick + " " + str);
- } else if (sock.isOutputShutdown()) break;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public class Klient
- {
- public static final int PORT=50007;
- public static final String HOST = "155.158.131.155";
- public static void main(String[] args) throws IOException
- {
- //nawiazanie polaczenia z serwerem
- Socket sock;
- sock=new Socket(HOST, PORT);
- System.out.println("Nawiazalem polaczenie: "+sock);
- //tworzenie watka odbierajacego
- Odbior odbior = new Odbior(sock);
- Nadaj nadaj = new Nadaj(sock, "Gaspar");
- odbior.start();
- nadaj.start();
- while(true) {
- if (!nadaj.isAlive() | !odbior.isAlive()) {
- break;
- }
- }
- System.out.println("Koniec poloczenia");
- //zamykanie polaczenia
- sock.close();
- }
- }
- *********************************************************************************************************************
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class SerwerMulti {
- public static final int PORT=5007;
- private static Set<String>names = new HashSet<>();
- private static Set<PrintWriter>senders = new HashSet<>();
- public static void main(String args[]) throws IOException {
- ServerSocket server = new ServerSocket(PORT);
- Socket client;
- System.out.println("Waiting for connection to: " + server);
- while(true) {
- client = server.accept();
- new ClientHandler(client).start();
- }
- }
- private static class ClientHandler extends Thread {
- private String name;
- private Socket client;
- private BufferedReader clientReceiver;
- private PrintWriter clientSender;
- public ClientHandler(Socket client) {
- this.client = client;
- }
- public void run() {
- try {
- this.clientReceiver = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
- this.clientSender = new PrintWriter(this.client.getOutputStream());
- this.clientSender.println("Your name?");
- this.clientSender.flush();
- while(true) {
- if (this.clientReceiver.ready()) {
- this.name = this.clientReceiver.readLine();
- synchronized (names) {
- if (!this.name.trim().isEmpty() && !names.contains(name)) {
- names.add(this.name);
- this.clientSender.println("Welcome "+this.name+"!");
- this.clientSender.flush();
- break;
- } else {
- this.clientSender.println("Name: "+this.name+" is already in use!\n"+"Your name?");
- this.clientSender.flush();
- }
- }
- }
- }
- for (PrintWriter sender: senders) {
- sender.println(this.name + "has joined");
- sender.flush();
- }
- senders.add(this.clientSender);
- String message = "";
- boolean exit = false;
- while(!exit) {
- if (this.clientReceiver.ready()) {
- message = this.clientReceiver.readLine();
- if(!message.equalsIgnoreCase("end")) {
- message = "<" + this.name + ">:" + message;
- } else {
- message = this.name + "has left";
- exit = true;
- }
- for (PrintWriter sender: senders) {
- if(sender != this.clientSender) {
- sender.println(message);
- sender.flush();
- }
- }
- }
- }
- senders.remove(this.clientSender);
- names.remove(this.name);
- this.clientReceiver.close();
- this.clientSender.close();
- this.client.close();
- }
- catch (IOException e) {
- System.out.println(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement