Skip to content

Instantly share code, notes, and snippets.

@joffilyfe
Created June 6, 2017 11:58
Show Gist options
  • Save joffilyfe/994e8e8723bc0997f03afcfd08b1c125 to your computer and use it in GitHub Desktop.
Save joffilyfe/994e8e8723bc0997f03afcfd08b1c125 to your computer and use it in GitHub Desktop.
package Chat;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class Client extends Thread {
private String ip;
private int port;
private Socket socket;
public Client(String ip, int port) {
this.ip = ip;
this.port = port;
try {
this.socket = new Socket(ip, port);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
DataInputStream in = new DataInputStream(this.socket.getInputStream());
DataOutputStream out = new DataOutputStream(this.socket.getOutputStream());
// Enquanto estiver conectado
while(this.socket.isConnected()) {
System.out.println("[Cliente] Envie uma msg: ");
Scanner teclado = new Scanner(System.in);
out.writeUTF(teclado.nextLine());
// Recebendo uma mensagem
System.out.println("[Cliente] Mensagem recebida: " + in.readUTF());
}
} catch (IOException e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment