aula02TCliente

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

//ligação TCP
public class aula02TCliente {
    public static final int SERVICE_PORT = 5001;
    static Socket daytime = null;

    public static void main(String args[]) throws IOException {
        if (args.length != 1){
            System.out.println ("Syntax – java DaytimeClient host");
            return;
        }

        // GET THE HOSTNAME OF SERVER
        String hostname = args[0];
        try {
            daytime = new Socket (hostname, SERVICE_PORT);
            System.out.println ("Connection established");

            // SET THE SOCKET OPTION JUST IN CASE SERVER STALLS
            daytime.setSoTimeout( 2000 ); //ms

            // READ FROM THE SERVER
            BufferedReader reader = new BufferedReader(new InputStreamReader(daytime.getInputStream()));
            System.out.println ("Results : " + reader.readLine()); //mudança de linha ou receber um socket fechar

            // CLOSE THE CONNECTION
            daytime.close();
        }catch (IOException e){ //catches also InterruptedIOException
            System.err.println ("Error " + e);
        }finally {
            //fechar o socket se ele for aberto
            if(daytime == null){
                daytime.close();
            }
        }
    }
}
Tags : ,

0 thoughts on “aula02TCliente”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.