85 lines
2.3 KiB
Java
85 lines
2.3 KiB
Java
import java.net.*;
|
|
import java.io.*;
|
|
|
|
public class Client
|
|
{
|
|
public static void main(String args[])
|
|
{
|
|
//pobranie i sprawdzenie otrzymanych argument闚
|
|
if (args.length < 2){
|
|
System.out.println("Wywo豉nie programu: Client host port");
|
|
System.exit(-1);
|
|
}
|
|
String host = args[0];
|
|
int port = 0;
|
|
try{
|
|
port = Integer.valueOf(args[1]);
|
|
}
|
|
catch(NumberFormatException e){
|
|
System.out.println("Nieprawid這wy argument: port");
|
|
System.exit(-1);
|
|
}
|
|
//Inicjalizacja gniazda klienckiego
|
|
Socket clientSocket = null;
|
|
try{
|
|
clientSocket = new Socket(host, port);
|
|
}
|
|
catch(UnknownHostException e){
|
|
System.out.println("Nieznany host.");
|
|
System.exit(-1);
|
|
}
|
|
catch(ConnectException e){
|
|
System.out.println("Po章czenie odrzucone.");
|
|
System.exit(-1);
|
|
}
|
|
catch(IOException e){
|
|
System.out.println("B章d wej𦣇ia-wyj𦣇ia: " + e);
|
|
System.exit(-1);
|
|
}
|
|
System.out.println("Po章czono z " + clientSocket);
|
|
|
|
//Deklaracje zmiennych strumieniowych
|
|
String line = null;
|
|
BufferedReader brSockInp = null;
|
|
BufferedReader brLocalInp = null;
|
|
DataOutputStream out = null;
|
|
|
|
//Utworzenie strumieni
|
|
try{
|
|
out = new DataOutputStream(clientSocket.getOutputStream());
|
|
brSockInp = new BufferedReader(
|
|
new InputStreamReader(
|
|
clientSocket.getInputStream()));
|
|
brLocalInp = new BufferedReader(
|
|
new InputStreamReader(System.in));
|
|
}
|
|
catch(IOException e){
|
|
System.out.println("B章d przy tworzeniu strumieni: " + e);
|
|
System.exit(-1);
|
|
}
|
|
//P皻la g堯wna klienta
|
|
while(true){
|
|
try{
|
|
line = brLocalInp.readLine();
|
|
if(line != null){
|
|
System.out.println("Wysy豉m: " + line);
|
|
out.writeBytes(line + '\n');
|
|
out.flush();
|
|
}
|
|
if(line == null || "quit".equals(line)){
|
|
System.out.println("Ko鎍zenie pracy...");
|
|
clientSocket.close();
|
|
System.exit(0);
|
|
}
|
|
brSockInp.readLine();
|
|
System.out.println("Otrzymano: " + line);
|
|
}
|
|
catch(IOException e){
|
|
System.out.println("B章d wej𦣇ia-wyj𦣇ia: " + e);
|
|
System.exit(-1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|