Created
June 6, 2017 13:25
-
-
Save joffilyfe/9f8cac28a65ba9f772c28c325b31eb3e 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.ServerSocket; | |
import java.net.Socket; | |
import java.util.Scanner; | |
public class Server extends Thread { | |
private Socket socket; | |
public Server(Socket socket) { | |
this.socket = socket; | |
} | |
// Esse loop vai ficar rodando em outra thread e fica recebendo as mensagens vindas do cliente. | |
public void run() { | |
while(this.socket.isConnected()) { | |
try { | |
DataInputStream in = new DataInputStream(socket.getInputStream()); | |
System.out.println(in.readUTF()); | |
} catch (IOException e) {} | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
ServerSocket servidor = new ServerSocket(7171, 10); | |
for (int i = 0; i < 10; i++) { | |
Socket socket = servidor.accept(); | |
// Criando a thread de leitura do servidor | |
Server server = new Server(socket); | |
server.start(); | |
while(socket.isConnected()) { | |
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); | |
System.out.println("Digite algo para enviar: "); | |
Scanner teclado = new Scanner(System.in); | |
out.writeUTF("[Servidor]:" + teclado.nextLine()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment