Advertisement
piffy

TCP client (Java)

Aug 6th, 2015
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package javatcp;
  2. import java.net.*;
  3. import java.io.*;
  4. public class JavaClient {
  5. /**
  6. * @param args porta da utilizzare (default 6666)
  7. */  
  8.     public static void main(String[] args) throws Exception{
  9.         int port=6666;
  10.         if (args.length == 1) {
  11.             try {port = Integer.parseInt(args[0]);}
  12.             catch (Exception e) { System.out.println("Errore nel parametro, uso il default"); }
  13.         }
  14.         // Apre una connessione TCP (port di default 6666)
  15.         Socket socket = new Socket("localhost",port);
  16.         // Apre gli stream
  17.         DataOutputStream bufferOutput = new DataOutputStream(socket.getOutputStream());        
  18.         BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
  19.             while (true) {            
  20.                 // Legge Dal Server
  21.                 String messaggio = input.readLine();
  22.                 bufferOutput.writeBytes(messaggio + '\n');
  23.                 if (messaggio.toUpperCase().trim().equals("CIAO"))
  24.                     break;
  25.             }
  26.         socket.close();
  27.     }  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement