UNIVERSIDAD
TECNICA DE
MANABI.
DOCENTE: Miguel Rodriguez.
NOMBRES:
Jerimy Isaac Moreira Alcivar.
Servidor:
package ProgramaSTCP;
import com.sun.source.tree.TryTree;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ServidorTCP {
public static void main(String[] args) {
ServerSocket servidor = null;
Socket sc = null;
DataInputStream in;
DataOutputStream out;
final int PUERTO = 5000;
try{
servidor = new ServerSocket(PUERTO);
System.out.println("Servidor Iniciado");
while (true){
sc = servidor.accept();
System.out.println("Cliente Conectado");
in = new DataInputStream(sc.getInputStream());
out = new DataOutputStream(sc.getOutputStream());
String mensaje = in.readUTF();
System.out.println(mensaje);
out.writeUTF("¡Saludos de parte del servidor!");
while (true){
try{
mensaje = in.readUTF();
System.out.println(mensaje);
if("BYE".equals(mensaje))break;
}catch(IOException e){break;
}
}
sc.close();
System.out.println("Cliente Descnoectado");
}
}
catch(IOException ex){
Logger.getLogger(ServidorTCP.class.getName()).log(Level.SEVERE, null, ex);
}
}
Cliente:
package ProgramaSTCP;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ClienteTCP {
public static void main(String[] args){
final String HOST = "127.0.0.1";
final int PUERTO = 5000;
DataInputStream in;
DataOutputStream out;
try{
Socket sc = new Socket(HOST, PUERTO);
in = new DataInputStream(sc.getInputStream());
out = new DataOutputStream(sc.getOutputStream());
out.writeUTF("Saludos de parte del CLIENTE");
String mensaje = in.readUTF();
System.out.println(mensaje);
Scanner scan = new Scanner(System.in);
int c=1, a=0;
while(c==1){
a=a+1;
System.out.println("Ingrese mensaje");
mensaje = scan.nextLine();
out.writeUTF("Este es el mejor numero"+a+":"+mensaje+"\n");
if(mensaje.equals("Bye"))break;
}
sc.close();
}catch(IOException ex){
Logger.getLogger(ClienteTCP.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
LINK:
https://drive.google.com/drive/folders/1uDUQIR_mLuwK1HqZOLXnl7W-
o0dygA71?usp=drive_link