Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zad_ii_1;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Serwer
- {
- public static final int serverPort = 2020;
- ServerSocket s;
- /* Konstruktor próbuje utworzyć gniazdo */
- Serwer()
- {
- try{
- s = new ServerSocket(serverPort);
- System.out.println("Serwer dziala");
- }catch(Exception e) {
- System.out.println("Nie można utworzyć gniazda");
- System.exit(1);
- }
- }
- void uruchom() throws Exception
- {
- /* Czekaj, aż klient się połączy */
- Socket socket = s.accept();
- /* Czynności przygotowawcze do obsługi klienta */
- /* 1. Otworzenie strumienia (wejściowego lub wyjściowego) i
- /* skojarzenie go z gniazdem klienta */
- Scanner in = new Scanner(System.in);
- PrintWriter out = new PrintWriter(
- new OutputStreamWriter(
- socket.getOutputStream()), true);
- String komunikat;
- while(true)
- {
- komunikat = in.nextLine();
- if(komunikat.equalsIgnoreCase("q"))
- {
- System.out.println("Koniec pracy serwera");
- socket.close();
- s.close();
- return;
- }
- else
- {
- try
- {
- /* Wyślij/Odbierz dane do/od klienta */
- /* (do/ze strumienia związanego z gniazdem) */
- out.println(komunikat);
- }catch(Exception e) {
- System.out.println("Brak klienta - koncze prace");
- socket.close();
- return;
- }
- }
- }//koniec pętli while(true)
- }//koniec funkcji uruchom()
- public static void main(String args[]) throws Exception
- {
- Serwer server = new Serwer();
- server.uruchom();
- server.s.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement