Skip to content

Instantly share code, notes, and snippets.

@Mithrandir0x
Last active December 24, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mithrandir0x/6779075 to your computer and use it in GitHub Desktop.
Save Mithrandir0x/6779075 to your computer and use it in GitHub Desktop.
clientsocket
package clientsocket;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author manel
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
try
{
Scanner entradaDades = new Scanner(System.in);
//podeu provar de conectar la IP 132.163.4.101
//que correspon al National Institute of Standards
//and Technology, en Boulder Colorado, i ofereix
//la mesura d'un rellotge atòmic de Cesi
System.out.println("Introdueix la IP del host");
String IP_Address = entradaDades.next();
//el port al que ens conectem és el 13
System.out.println("introdueix el port");
int port = entradaDades.nextInt();
Socket socket = new Socket(IP_Address, port);
try
{
InputStream entrada = socket.getInputStream();
OutputStream salida = socket.getOutputStream();
}
catch ( Exception ex )
{
return;
}
Scanner in = new Scanner(entrada);
while ( in.hasNextLine() )
{
String linia = in.nextLine();
System.out.println(linia);
}
//compte amb fer servir el Rellotge atomic
//aqui perque no podem enviar dades, només
//ens envia les dades i donarà una excepció
// per evitar-ho comenteu les següents 3 linies
PrintWriter out = new PrintWriter(salida,true);
String dadesAEnviar = in.next();
out.println(dadesAEnviar);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
finally
{
socket.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment