Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javatcp;
- import java.net.*;
- import java.io.*;
- public class JavaServer {
- /**
- * @param args porta da utilizzare (default 6666)
- */
- public static void main(String[] args) throws Exception{
- int port=6666;
- if (args.length == 1) {
- try {port = Integer.parseInt(args[0]);}
- catch (Exception e) { System.out.println("Errore nel parametro, uso il default"); }
- }
- // Apre una connessione TCP (port di default 6666)
- ServerSocket socket = new ServerSocket(port);
- Socket connectionSocket=socket.accept();
- // Apre lo stream bufferizzato
- BufferedReader bufferInput =
- new BufferedReader (new InputStreamReader(connectionSocket.getInputStream()));
- while(true){
- // Legge dal client
- String messaggio = bufferInput.readLine();
- System.out.println("Ricevuto: " + messaggio);
- if (messaggio.toUpperCase().trim().equals("CIAO"))
- break;
- }
- socket.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement