Last active
December 24, 2015 09:39
-
-
Save Mithrandir0x/6779075 to your computer and use it in GitHub Desktop.
clientsocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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