Created
June 6, 2017 11:58
-
-
Save joffilyfe/994e8e8723bc0997f03afcfd08b1c125 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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