Last active
May 30, 2016 12:00
-
-
Save SparedRay/ad02b309754981e225876ce01b5cc6de to your computer and use it in GitHub Desktop.
Chat de consola en java basado en la version de https://www.daniweb.com/programming/software-development/code/448361/multithreaded-simple-clientserver-chat-console-program
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 ttext; | |
import java.io.*; | |
import java.net.*; | |
public class Client { | |
public static void main() { | |
try { | |
Socket sock = new Socket("localhost", 8000); | |
SendThread sendThread = new SendThread(sock); | |
Thread thread = new Thread(sendThread); | |
thread.start(); | |
RecieveThread recieveThread = new RecieveThread(sock); | |
Thread thread2 =new Thread(recieveThread); | |
thread2.start(); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
class RecieveThread implements Runnable { | |
Socket sock = null; | |
BufferedReader recieve = null; | |
public RecieveThread(Socket sock) { | |
this.sock = sock; | |
} | |
public void run() { | |
try { | |
recieve = new BufferedReader(new InputStreamReader(this.sock.getInputStream())); | |
String msgRecieved = null; | |
while((msgRecieved = recieve.readLine())!= null) { | |
System.out.println("Servidor: " + msgRecieved); | |
System.out.println("Su mensaje para el servidor.."); | |
} | |
} catch(Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
class SendThread implements Runnable { | |
Socket sock = null; | |
PrintWriter print = null; | |
BufferedReader brinput = null; | |
public SendThread(Socket sock) { | |
this.sock = sock; | |
} | |
public void run(){ | |
try { | |
if(sock.isConnected()) { | |
System.out.println("Conectado a " + sock.getInetAddress() + " en el puerto " + sock.getPort()); | |
this.print = new PrintWriter(sock.getOutputStream(), true); | |
while(true) { | |
System.out.println("Escriba su mensaje... (O escriba 'SALIR' para cerrar)"); | |
brinput = new BufferedReader(new InputStreamReader(System.in)); | |
String msgtoServerString = null; | |
msgtoServerString = brinput.readLine(); | |
this.print.println(msgtoServerString); | |
this.print.flush(); | |
if(msgtoServerString.equals("SALIR")) | |
break; | |
} | |
sock.close(); | |
} | |
} catch(Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
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 ttext; | |
import java.io.*; | |
import java.net.*; | |
import java.lang.*; | |
public class Server { | |
public static void main() throws IOException { | |
final int port = 8000; | |
System.out.println("Experando conexion en el puerto " + port); | |
ServerSocket ss = new ServerSocket(port); | |
Socket clientSocket = ss.accept(); | |
System.out.println("Conexion entrante de " + clientSocket.getInetAddress()); | |
RecieveFromClientThread recieve = new RecieveFromClientThread(clientSocket); | |
Thread thread = new Thread(recieve); | |
thread.start(); | |
SendToClientThread send = new SendToClientThread(clientSocket); | |
Thread thread2 = new Thread(send); | |
thread2.start(); | |
} | |
} | |
class RecieveFromClientThread implements Runnable { | |
Socket clientSocket=null; | |
BufferedReader brBufferedReader = null; | |
public RecieveFromClientThread(Socket clientSocket) { | |
this.clientSocket = clientSocket; | |
} | |
public void run() { | |
try { | |
brBufferedReader = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); | |
String messageString; | |
while(true) { | |
while((messageString = brBufferedReader.readLine()) != null){ | |
if(messageString.equals("SALIR")) | |
break; | |
System.out.println("Cliente: " + messageString); | |
System.out.println("Respuesta al cliente.."); | |
} | |
this.clientSocket.close(); | |
System.exit(0); | |
} | |
} catch(Exception ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
} | |
class SendToClientThread implements Runnable { | |
PrintWriter pwPrintWriter; | |
Socket clientSock = null; | |
public SendToClientThread(Socket clientSock) { | |
this.clientSock = clientSock; | |
} | |
public void run() { | |
try { | |
pwPrintWriter =new PrintWriter(new OutputStreamWriter(this.clientSock.getOutputStream())); | |
while(true) { | |
String msgToClientString = null; | |
BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); | |
msgToClientString = input.readLine(); | |
pwPrintWriter.println(msgToClientString); | |
pwPrintWriter.flush(); | |
System.out.println("Su mensaje para el cliente.."); | |
} | |
} catch(Exception ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
} |
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 ttext; | |
import java.io.*; | |
public class TText { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
if(args[0].equals("-s")) { | |
try { | |
Server.main(); | |
} catch(IOException e) { | |
System.out.println("Parece que hubo algun tipo de error en la conexion"); | |
} | |
} else { | |
Client.main(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment