elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado:


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  [Sockets] Sirven en internet?
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] 2 Ir Abajo Respuesta Imprimir
Autor Tema: [Sockets] Sirven en internet?  (Leído 5,006 veces)
klaine

Desconectado Desconectado

Mensajes: 112



Ver Perfil WWW
[Sockets] Sirven en internet?
« en: 2 Diciembre 2011, 21:07 pm »

Hola, estuve viendo unos videotutoriales que me paso un amigo, el tipo se llama nikitus y enseña a crear una conexion con sockets, en el video muestra como hacer un pequeño xat, todo el codigo funka, pero me gustaria saber si es posible hacer que funke en internet, traté de poner mi direccion ip en el programa cliente y pasarselo a un amigo, pero no funciono, codeé en base al video, dos clases, un cliente y un server, en localhost corre bien, pero me gustaría saber, si es posible, que debo cambiarle para que funcione en internet, de antemano gracias.

Clase servidor:

Código
  1.  
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.io.ObjectOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import javax.swing.JOptionPane;
  8.  
  9. public class XServer {
  10.  
  11.  private ServerSocket ss;
  12. private Socket s;
  13. private ObjectOutputStream oos;
  14. private boolean isRunning=true;
  15. private ObjectInputStream ois;
  16. private volatile Command cmd=null;
  17.  
  18.  
  19.    XServer(){
  20.  
  21.          try{
  22.  
  23.     ss = new ServerSocket(9999);
  24.     s = ss.accept();
  25.     oos=new ObjectOutputStream(s.getOutputStream());
  26.     ois=new ObjectInputStream(s.getInputStream());
  27.  
  28.     }catch(IOException ioex){
  29.  
  30.      ioex.printStackTrace();
  31.  
  32.     }
  33.  
  34. this.sender();
  35. this.receiver();
  36. this.dealReceive();
  37. this.autoClose();
  38.  
  39.    }
  40.  
  41.    void sender(){
  42.  
  43. Thread t=new Thread(new Runnable(){
  44.  
  45.  public void run(){
  46.  
  47.   while(XServer.this.isRunning){
  48.  
  49.    int type=0;
  50.  
  51.    try{
  52.  
  53.  String msg=JOptionPane.showInputDialog("Mensaje para el Cliente");
  54.  
  55.   Command c=new Command();
  56.  
  57.   c.setMsg(msg);
  58.  
  59.   type=(msg.equalsIgnoreCase("Close"))? 0:1;
  60.  
  61.   c.setType(type);
  62.  
  63.   oos.writeObject(c);
  64.  
  65.  }catch(IOException ex) {
  66.  
  67.   ex.printStackTrace();
  68.  
  69.  }
  70.  
  71.    if(type==0){
  72.  
  73.     break;
  74.  
  75.    }
  76.  
  77.   }
  78.  
  79.  }
  80.  
  81. });
  82.  
  83. t.start();
  84.  
  85.    }
  86.  
  87.    void receiver(){
  88.  
  89.    Thread t=new Thread(new Runnable(){
  90.  
  91.     public void run(){
  92.  
  93.      while(XServer.this.isRunning){
  94.  
  95.        try{
  96.  
  97.      Thread.sleep(1000);
  98.  
  99.      Object aux=ois.readObject();
  100.  
  101.      if(aux!=null && aux instanceof Command){
  102.  
  103.        XServer.this.cmd = (Command) aux;
  104.  
  105.      }
  106.  
  107.     }catch(InterruptedException intex){
  108.  
  109.      intex.printStackTrace();
  110.  
  111.     }catch(IOException ioex){
  112.  
  113.      ioex.printStackTrace();
  114.  
  115.     }catch(ClassNotFoundException classex){
  116.  
  117.      classex.printStackTrace();
  118.  
  119.     }
  120.  
  121.      }
  122.  
  123.     }
  124.  
  125.    });
  126.  
  127.    t.start();
  128.  
  129.    }
  130.  
  131.    void dealReceive(){
  132.  
  133.     Thread t=new Thread(new Runnable(){
  134.  
  135.      public void run(){
  136.  
  137.       while(XServer.this.isRunning){
  138.  
  139.       try{
  140.  
  141.        Thread.sleep(1000);
  142.  
  143.        Command c=XServer.this.cmd;
  144.  
  145.        if(c.getType().equals("Message")){
  146.  
  147.         System.out.println(c.getMsg());
  148.  
  149.        }else if(c.getType().equals("Action") && c.getMsg().equals("Close")){
  150.  
  151.         XServer.this.isRunning=false;
  152.  
  153.        }
  154.  
  155.     }catch(InterruptedException intex){
  156.  
  157.      intex.printStackTrace();
  158.  
  159.     }catch(NullPointerException nullex){
  160.  
  161.     }finally{
  162.  
  163.       XServer.this.cmd=null;
  164.  
  165.     }
  166.  
  167.     }
  168.  
  169.      }
  170.  
  171.     });
  172.  
  173.     t.start();
  174.  
  175.    }
  176.  
  177.    void autoClose(){
  178.  
  179. Thread t=new Thread(new Runnable(){
  180.  
  181.  public void run(){
  182.  
  183.   while(true){
  184.  
  185.    try{
  186.  
  187.     Thread.sleep(200);
  188.  
  189.     if(!XServer.this.isRunning){
  190.  
  191.      XServer.this.ois.close();
  192.      XServer.this.oos.close();
  193.      XServer.this.s.close();
  194.  
  195.     }
  196.  
  197.    }catch(InterruptedException intex){
  198.  
  199.     intex.printStackTrace();
  200.  
  201.    }catch(IOException ioex){
  202.  
  203.     ioex.printStackTrace();
  204.  
  205.    }
  206.  
  207.   }
  208.  
  209.  }
  210.  
  211. });
  212.  
  213. t.start();
  214.  
  215. }
  216.  
  217. }
  218.  
  219.  
  220.  

Clase cliente:

Código
  1.  
  2. import java.net.Socket;
  3. import java.io.ObjectOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class XClient {
  9.  
  10.  private Socket s;
  11. private ObjectOutputStream oos;
  12. private boolean isRunning=true;
  13. private ObjectInputStream ois;
  14. private volatile Command cmd=null;
  15.  
  16.  
  17.    XClient(){
  18.  
  19.               try{
  20.  
  21.     s=new Socket("10.5.4.124", 9999);
  22.     oos=new ObjectOutputStream(s.getOutputStream());
  23.     ois=new ObjectInputStream(s.getInputStream());
  24.  
  25.     }catch(IOException ioex){
  26.  
  27.      ioex.printStackTrace();
  28.  
  29.     }
  30.  
  31.               this.sender();
  32.               this.receiver();
  33.               this.dealReceive();
  34.               this.autoClose();
  35.  
  36.    }
  37.  
  38.    void receiver(){
  39.  
  40.    Thread t=new Thread(new Runnable(){
  41.  
  42.     public void run(){
  43.  
  44.      while(XClient.this.isRunning){
  45.  
  46.        try{
  47.  
  48.      Thread.sleep(1000);
  49.  
  50.      Object aux=ois.readObject();
  51.  
  52.      if(aux!=null && aux instanceof Command){
  53.  
  54.        XClient.this.cmd = (Command) aux;
  55.  
  56.      }
  57.  
  58.     }catch(InterruptedException intex){
  59.  
  60.      intex.printStackTrace();
  61.  
  62.     }catch(IOException ioex){
  63.  
  64.      ioex.printStackTrace();
  65.  
  66.     }catch(ClassNotFoundException classex){
  67.  
  68.      classex.printStackTrace();
  69.  
  70.     }
  71.  
  72.      }
  73.  
  74.     }
  75.  
  76.    });
  77.  
  78.    t.start();
  79.  
  80.    }
  81.  
  82.        void dealReceive(){
  83.  
  84.     Thread t=new Thread(new Runnable(){
  85.  
  86.      public void run(){
  87.  
  88.       while(XClient.this.isRunning){
  89.  
  90.       try{
  91.  
  92.        Thread.sleep(1000);
  93.  
  94.        Command c=XClient.this.cmd;
  95.  
  96.        if(c.getType().equals("Message")){
  97.  
  98.         System.out.println(c.getMsg());
  99.  
  100.        }else if(c.getType().equals("Action") && c.getMsg().equals("Close")){
  101.  
  102.         XClient.this.isRunning=false;
  103.  
  104.        }
  105.  
  106.     }catch(InterruptedException intex){
  107.  
  108.      intex.printStackTrace();
  109.  
  110.     }catch(NullPointerException nullex){
  111.  
  112.     }finally{
  113.  
  114.       XClient.this.cmd=null;
  115.  
  116.     }
  117.  
  118.     }
  119.  
  120.      }
  121.  
  122.     });
  123.  
  124.     t.start();
  125.  
  126.    }
  127.  
  128. void sender(){
  129.  
  130. Thread t=new Thread(new Runnable(){
  131.  
  132.  public void run(){
  133.  
  134.   while(XClient.this.isRunning){
  135.  
  136.    int type=0;
  137.  
  138.    try{
  139.  
  140.  String msg=JOptionPane.showInputDialog("Mensaje para el Server");
  141.  
  142.   Command c=new Command();
  143.  
  144.   c.setMsg(msg);
  145.  
  146.   type=(msg.equalsIgnoreCase("Close"))? 0:1;
  147.  
  148.   c.setType(type);
  149.  
  150.   oos.writeObject(c);
  151.  
  152.  }catch(IOException ex) {
  153.  
  154.   ex.printStackTrace();
  155.  
  156.  }
  157.  
  158.    if(type==0){
  159.  
  160.     break;
  161.  
  162.    }
  163.  
  164.   }
  165.  
  166.  }
  167.  
  168. });
  169.  
  170. t.start();
  171.  
  172.    }
  173.  
  174. void autoClose(){
  175.  
  176. Thread t=new Thread(new Runnable(){
  177.  
  178.  public void run(){
  179.  
  180.   while(true){
  181.  
  182.    try{
  183.  
  184.     Thread.sleep(200);
  185.  
  186.     if(!XClient.this.isRunning){
  187.  
  188.      XClient.this.ois.close();
  189.      XClient.this.oos.close();
  190.      XClient.this.s.close();
  191.  
  192.     }
  193.  
  194.    }catch(InterruptedException intex){
  195.  
  196.     intex.printStackTrace();
  197.  
  198.    }catch(IOException ioex){
  199.  
  200.     ioex.printStackTrace();
  201.  
  202.    }
  203.  
  204.   }
  205.  
  206.  }
  207.  
  208. });
  209.  
  210. t.start();
  211.  
  212. }
  213.  
  214. }
  215.  
  216.  

Se me olvidaba, para lo que quiero hacer despues de aprender a manipular sockets en internet creé una clase comando, que en el futuro me servirá para mandar solo strings con referencias a lo que se debe hacer y no mandar objetos pesados a traves de la conexion, esta es la clase:

Código
  1.  
  2. import java.io.Serializable;
  3.  
  4. public class Command implements Serializable{
  5.  
  6. private String[] types={"Action", "Message", "Dialog", "Input", "Warning", "Error"};
  7. private String[] actions={"Close"};
  8.  
  9. private String type="";
  10. private String msg="";
  11.  
  12.    Command(){
  13.  
  14.     this.type= types[1];
  15.  
  16.    }
  17.  
  18.    Command(String msg){
  19.  
  20.     this.type=this.types[0];
  21.     this.msg=msg;
  22.  
  23.    }
  24.  
  25.    Command(String msg, int type){
  26.  
  27.     this.type=this.types[type];
  28.     this.msg=msg;
  29.  
  30.    }
  31.  
  32. public String getMsg() {
  33.  return msg;
  34. }
  35.  
  36. void setDialog(String msg){
  37.  
  38.  this.msg=msg;
  39.  this.type=types[2];
  40.  
  41. }
  42.  
  43. void setInput(String msg){
  44.  
  45.  this.msg=msg;
  46.  this.type=types[3];
  47.  
  48. }
  49.  
  50. void setWarning(String msg){
  51.  
  52.  this.msg=msg;
  53.  this.type=types[4];
  54.  
  55. }
  56.  
  57. void setError(String msg){
  58.  
  59.  this.msg=msg;
  60.  this.type=types[5];
  61.  
  62. }
  63.  
  64. public void setMsg(String msg) {
  65.  this.msg = msg;
  66. }
  67.  
  68. public void setType(int i){
  69.  
  70.  this.type=this.types[i];
  71.  
  72. }
  73.  
  74. public String getType(){
  75.  
  76.  return this.type;
  77.  
  78. }
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  


Bueno, de antemano agradezco muchísimo vuestras respuestas, saludos


En línea

RyogiShiki


Desconectado Desconectado

Mensajes: 745


げんしけん - Hikkikomori FTW!!!


Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #1 en: 2 Diciembre 2011, 22:14 pm »

No he visto el código, pero las conexiones entre sockets del tipo peer to peer suelen estar bastante liadas si alguno de los dos está detrás de un Firewall o un Router. Si la conexión es directa y no estás usando ningún puerto reservado seguro que va de las mil maravillas.

Saludos


En línea

Pablo Videla


Desconectado Desconectado

Mensajes: 2.274



Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #2 en: 2 Diciembre 2011, 22:16 pm »

Interesante, gracias por publicar el codigo, voy a revisarlo mas tarde y te cuento, saludos!
En línea

klaine

Desconectado Desconectado

Mensajes: 112



Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #3 en: 2 Diciembre 2011, 22:27 pm »

Olvide poner el main, aunque no hace mucho:

Código
  1.  
  2. public class Main {
  3.  
  4.    public static void main(String[] args) {
  5.  
  6.     //xserver();
  7.     //xclient();
  8.  
  9.    }
  10.  
  11. }
  12.  
  13.  

Para probarlo en localhost, primero se conecta como server, luego como client (sacando los respectivos comentarios) y en la linea 22 de XClient el primer argumento es "localhost", esta es mi duda, que debo poner en localhost, por que al hacer ipconfig, me salen un monton de numeros y el amigo que recibio el jar cliente ya no esta conectado Xd (la conexion aqui en mi instituto es ipv6, ¿sera por eso?  ojalá que no :-\)

Saludos!
En línea

Pablo Videla


Desconectado Desconectado

Mensajes: 2.274



Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #4 en: 2 Diciembre 2011, 22:39 pm »

Si tu eres el servidor coloca tu ip

http://whatismyipaddress.com/

ahi la ves, y el cliente colocara su propia ip,  :xD cualquier error alguien me corrige, saludos.
En línea

klaine

Desconectado Desconectado

Mensajes: 112



Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #5 en: 2 Diciembre 2011, 23:16 pm »

Puse la ip que sale en es pagina, el chico al que le pedi que ejecutara el cliente me dijo lo siguiente:

"ahora si abrio algo­­ y dije hola­­"

No se si creerle, pero si es cierto algo anda mal en mi code, si es mentira quizas sea algo mas que haya que hacer.

En línea

Pablo Videla


Desconectado Desconectado

Mensajes: 2.274



Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #6 en: 2 Diciembre 2011, 23:48 pm »

Puse la ip que sale en es pagina, el chico al que le pedi que ejecutara el cliente me dijo lo siguiente:

"ahora si abrio algo­­ y dije hola­­"

No se si creerle, pero si es cierto algo anda mal en mi code, si es mentira quizas sea algo mas que haya que hacer.



Y por que crees que te mintio?
En línea

egyware


Desconectado Desconectado

Mensajes: 526



Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #7 en: 4 Diciembre 2011, 04:58 am »

Cuando uno está detras de un Router es imposible abrir puertos. Si estás detras de un router o firewall o lo que sea, trata de desbloquearlo primero y luego te pones a la escucha en tales puertos.
Si estas directamente conectado a internet no deberias tener problemas en ponerte a la escucha en ningun puerto (cuidado con el firewall)

Saludos!!!
En línea

Chivin

Desconectado Desconectado

Mensajes: 4


Ver Perfil
Re: [Sockets] Sirven en internet?
« Respuesta #8 en: 4 Diciembre 2011, 06:50 am »

Ola como mencionan los compañeros, los más seguro es que estas tras un firewall o un router quien(es) probablemente (por no afirmarlo) tiene cerrado el puerto que estas utilizando. Es por ello que no permite la comunicación, para hacer la conexión es necesario el pase por esas barreras de lo contrario jamás se conectarán. El problema no tiene que ver con IPv6.

saludos!!
En línea

klaine

Desconectado Desconectado

Mensajes: 112



Ver Perfil WWW
Re: [Sockets] Sirven en internet?
« Respuesta #9 en: 4 Diciembre 2011, 18:22 pm »

No-Ip sirve?
En línea

Páginas: [1] 2 Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
firmware!!! ¿para que sirven?
Software
aliteroid 1 3,171 Último mensaje 10 Abril 2006, 17:21 pm
por el-brujo
para que sirven los puos en en un dvd?
Multimedia
druguito 1 1,966 Último mensaje 13 Julio 2006, 09:58 am
por CHI<`;´>NO©
Athero con USB, sirven?
Hacking Wireless
Belial & Grimoire 1 2,070 Último mensaje 29 Diciembre 2010, 10:03 am
por ChimoC
Liado con Sockets, Streams.. y ficheros de internet
.NET (C#, VB.NET, ASP)
xuturk 2 2,552 Último mensaje 1 Septiembre 2011, 08:58 am
por xuturk
Sockets a través de internet C#
.NET (C#, VB.NET, ASP)
Bob1098 3 2,411 Último mensaje 16 Agosto 2015, 15:20 pm
por Bob1098
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines