Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Destro- en 27 Enero 2011, 01:07 am



Título: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Destro- en 27 Enero 2011, 01:07 am
Holas :).
Se puede hacer eso?.Tuve buscando pero no encontre nada para c/c++,era para otro lenguaje :S.
Si se puede me dan un ejemplo :).

Grax de antemano  ;D.


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Leber en 28 Enero 2011, 13:14 pm
Puedes hacerlo por sockets, pipes etc.

http://lucas.hispalinux.es/Universitarios/seminario-1.html


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Khronos14 en 28 Enero 2011, 17:55 pm
Sockets, FileMappings, utilizando la API SendMessage..

Saludos.


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Garfield07 en 28 Enero 2011, 21:58 pm
http://foro.elhacker.net/programacion_cc/como_sacar_una_ip_code-t299839.0.html#ixzz1CMjbGpRL
Eso seria un server. Para el client usaria hK 2.0, tmb mio, pero no lo tengo aqui. Si esta noche no puedo en mi casa, mañana te lo cuelgo y te mando un PM...
Recuerda retocar el code un poquito

El hK 2.0 tiene una funcion de escucha y otra de conex. Lo puedes usar para los dos:
Código:
./hK vl 31337
./hK vc 127.0.0.1 31337

Van perfectamente, aunque si en uno cierras la conex. el otro queda abierto hasta que mandas un mensaje :P


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Destro- en 29 Enero 2011, 16:52 pm
gracias a todos  ;),voy a usar socket


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Garfield07 en 29 Enero 2011, 20:28 pm
Perdona la tardanza... Tienes ante ti el hK 2.0, ni más ni menos jeje...
¿Y que estabas usando? Claro que tienes que usar sockets!

Código
  1. #include <time.h> //Hora
  2. #include <stdio.h> //Funciones basicas entrada/salida
  3. #include <string.h> //StrCmp
  4. #include <stdlib.h> //Exit y otras
  5. #include <arpa/inet.h> //struct sockaddr_in
  6. #include <sys/socket.h> //Socket, Connect...
  7.  
  8. struct sockaddr_in host, client; //Declaraciones
  9.  
  10. int a=sizeof (struct sockaddr);
  11. char buffer [1024];
  12. int cont = 2;
  13. int newsock;
  14. int sockfd;
  15. int k = 0;
  16.  
  17. int listens (int port); //Funciones
  18. int conect (char *IP, int port);
  19. int scan (char *IP);
  20.  
  21. int times () //Esta funcion escribira la fecha en pantalla...
  22. {
  23. time_t now=time (0);
  24. struct tm *ahora;
  25. char buffer [40];
  26. ahora= (struct tm*)localtime ((const time_t*)&now);
  27. strftime (buffer, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  28. printf ("%s   ", buffer);
  29. return 0;
  30. }
  31.  
  32. int finalizar (int state) //Esta funcion detendra el programa en caso de problemas...
  33.  
  34. {
  35. close (sockfd);
  36.  
  37. if (state == 1)
  38. {
  39. printf ("Ocurrio un error en tiempo de ejecucion...\n");
  40.  
  41. exit (1);
  42. }
  43. else exit (0);
  44.  
  45. }
  46.  
  47. int help () //Mal uso de la linea de comandos...
  48. {
  49. printf ("Use:\thK <options> <target_ip // port> [port]\n");
  50. printf ("Options:+d // +v [Daemon // Verbose]\n");
  51. printf ("\t+l // +c // +z [Listen // Connect // Scan]\n\n");
  52. printf ("\t./hK -l <port>\n");
  53. printf ("\t./hK -c <target_ip> <port>\n");
  54. printf ("\t./hK -z <target_ip>\n\n");
  55. printf ("Exp:\t./hK dl 31337\n");
  56. printf ("\t./hK vc 192.168.0.1 31337\n");
  57. printf ("\t./hK vz 192.168.0.1\n\n");
  58. exit (1);
  59. }
  60.  
  61. int main (int argc, char *argv [])
  62. {
  63. time_t now=time (0);
  64. struct tm *ahora;
  65. char hora [40];
  66. ahora=localtime ((const time_t*)&now);
  67. strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  68. printf ("hK 2.0 - By Sagrini (2010) - %s\n", hora); //Esto escribira la presentacion.
  69.  
  70. if (argc < 3 || argc > 4) help (); //Diferentes opciones y mal uso de linea de comandos.
  71. if (strlen (argv [1]) != 2) help ();
  72.  
  73. if (argv [1][0] != 'd' && argv [1][0]!= 'v') help ();
  74. if (argv [1][1] != 'l' && argv [1][1]!= 'c' && argv [1][1]!= 'z') help ();
  75.  
  76. if (argv [1][1] == 'l' && argv [2] == NULL) help ();
  77. if (argv [1][1] == 'c' && argv [3] == NULL) help ();
  78. if (argv [1][1] == 'z' && argv [2] == NULL) help ();
  79.  
  80. if (argv [1][0] == 'd') daemon (1, 0);
  81. if (argv [1][1] == 'l') listens (atoi (argv [2]));
  82. if (argv [1][1] == 'c') conect (argv [2], atoi (argv [3]));
  83. if (argv [1][1] == 'z') scan (argv [2]);
  84.  
  85. return 0;
  86. }
  87.  
  88. int listens (int port) //Esta funcion esperara una conexion y printara todo lo que reciba...
  89. {
  90. if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
  91. host.sin_port=htons(port);
  92.  
  93. host.sin_family=AF_INET;
  94.  
  95. host.sin_addr.s_addr=0;
  96.  
  97. memset (host.sin_zero, 0, 8);
  98.  
  99.  
  100. if(bind(sockfd,(struct sockaddr*)&host,sizeof(host))==-1) finalizar (1);
  101.  
  102. if(listen(sockfd,5)==-1) finalizar (1);
  103.  
  104. if((newsock=accept(sockfd, (struct sockaddr*)&client, &a))==-1) finalizar (1);
  105. times ();
  106. printf ("Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  107.  
  108. close (sockfd);
  109.  
  110. int ID = fork ();
  111. if (ID != 0)
  112. {
  113. do
  114. {
  115. fgets (buffer, 1024, stdin);
  116. cont=send (newsock, &buffer, strlen (buffer), 0);
  117. }
  118. while (cont>1);
  119. close (newsock);
  120. close (sockfd);
  121. printf ("Finishing connection with %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  122. }
  123. else
  124. {
  125. cont=recv (newsock, &buffer, 1024, 0);
  126. while (cont>1)
  127. {
  128. buffer [cont-1]='\0';
  129. printf ("RECV %d bytes: %s \n", cont, buffer);
  130. cont=recv (newsock, &buffer, 1024, 0);
  131. }
  132. close (newsock);
  133. close (sockfd);
  134. }
  135. finalizar (0);
  136.  
  137. }
  138.  
  139. int conect (char *IP, int port) //Se conecta a una IP y a su puerto determinado para mandar datos...
  140. {
  141.  
  142. if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
  143. host.sin_port=htons(port);
  144.  
  145. host.sin_family=AF_INET;
  146.  
  147. host.sin_addr.s_addr=inet_addr (IP);
  148.  
  149. memset (host.sin_zero, 0, 8);
  150.  
  151. if((connect (sockfd, (struct sockaddr*)&host, sizeof (host)))==-1) finalizar (1);
  152. times ();
  153. printf ("Got connection with %s:%d\n", inet_ntoa (host.sin_addr), ntohs (host.sin_port));
  154.  
  155. int ID = fork ();
  156. if (ID != 0)
  157. {
  158. do
  159. {
  160. fgets (buffer, 1024, stdin);
  161. cont=send (sockfd, &buffer, strlen (buffer), 0);
  162. }
  163. while (cont>1);
  164. close (sockfd);
  165. printf ("Finishing connection with %s:%d\n\n", inet_ntoa (host.sin_addr), ntohs (host.sin_port));
  166. }
  167. else
  168. {
  169. cont=recv (sockfd, &buffer, 1024, 0);
  170. while (cont>1)
  171. {
  172. buffer [cont-1]='\0';
  173. printf ("RECV %d bytes: %s \n", cont, buffer);
  174. cont=recv (sockfd, &buffer, 1024, 0);
  175. }
  176. close (sockfd);
  177. }
  178. finalizar (0);
  179.  
  180. }
  181.  
  182. int scan (char *IP) //Sencillo escaner de puertos.
  183. {
  184. host.sin_addr.s_addr=inet_addr (IP);
  185. host.sin_family=AF_INET;
  186.  
  187. memset (host.sin_zero, 0, 8);
  188. for (cont = 0; cont < 65535; cont ++)
  189. {
  190. if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
  191. host.sin_port = htons (cont);
  192.  
  193. if (connect (sockfd, (struct sockaddr*)&host, sizeof (host)) != -1)
  194. {
  195. printf ("Port %d open\n", cont);
  196. k++;
  197. }
  198. close (sockfd);
  199. }
  200. if (k == 0) printf ("All ports closed...\n");
  201. else printf ("%d port(s) open(s)...\n", k);
  202.  
  203. finalizar (0);
  204.  
  205. }
  206.  


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Destro- en 29 Enero 2011, 20:49 pm
jeje xD,lastima q no me va a funcionar de 1 :P,fork no funca en dev c++ :S,pero me va a servir para mejorar lo q tengo (Y).
+Karma


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Garfield07 en 29 Enero 2011, 21:24 pm
El otro dia lo pase a Windows, para tu suerte. Voy a ver si no lo borre jeje... Suerte que tienes!!!
Bueno, cabe decir que como lo pase en unos minutos le tuve que borrar unas cosillas. Ahora si escuchas no puedes mandar mensajes, solo recibir. Igual, si te conectas no recibes. Esto se debe a que le tengo que hacer un arreglillo para que pueda abrir dos procesos y subdivir el programa...

Evidentemente tienes mucho que retocar, pero sigue funcionando asi:
Código:
hK vl 31337
y
hK vc 127.0.0.1 31337
Código
  1. #include <time.h>       //Hora
  2.  
  3. #include <stdio.h>      //Funciones basicas entrada/salida
  4.  
  5. #include <string.h>     //StrCmp
  6.  
  7. #include <stdlib.h>     //Exit y otras
  8.  
  9. #include <winsock2.h> //Socket, Connect...
  10.  
  11.  
  12.  
  13. struct sockaddr_in host, client;//Declaraciones
  14.  
  15. int a=sizeof (struct sockaddr);
  16.  
  17. char buffer [1024];
  18.  
  19. int cont = 2;
  20.  
  21. SOCKET newsock;
  22.  
  23. SOCKET sockfd;
  24.  
  25. int k = 0;
  26.  
  27.  
  28.  
  29. int listens (int port); //Funciones
  30.  
  31. int conect (char *IP, int port);
  32.  
  33. int scan (char *IP);
  34.  
  35.  
  36.  
  37. int times ()    //Esta funcion escribira la fecha en pantalla...
  38.  
  39. {
  40.  
  41. time_t now=time (0);
  42.  
  43. struct tm *ahora;
  44.  
  45. char buffer [40];
  46.  
  47. ahora= (struct tm*)localtime ((const time_t*)&now);
  48.  
  49. strftime (buffer, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  50.  
  51. printf ("%s   ", buffer);
  52.  
  53. return 0;
  54.  
  55. }
  56.  
  57.  
  58.  
  59. int finalizar (int state)       //Esta funcion detendra el programa en caso de problemas...
  60.  
  61. {
  62.  
  63. closesocket (sockfd);
  64.  
  65. if (state == 1)
  66.  
  67. {
  68.  
  69. printf ("Ocurrio un error en tiempo de ejecucion...\n");
  70.  
  71. exit (1);
  72.  
  73. }
  74.  
  75. else exit (0);
  76.  
  77. }
  78.  
  79.  
  80.  
  81. int help ()     //Mal uso de la linea de comandos...
  82.  
  83. {
  84.  
  85. printf ("Use:\thK <options> <target_ip // port> [port]\n");
  86.  
  87. printf ("\t-l // -c // -z [Listen // Connect // Scan]\n\n");
  88.  
  89. printf ("\t./hK -l <port>\n");
  90.  
  91. printf ("\t./hK -c <target_ip> <port>\n");
  92.  
  93. printf ("\t./hK -z <target_ip>\n\n");
  94.  
  95. printf ("Exp:\t./hK -l 31337\n");
  96.  
  97. printf ("\t./hK -c 192.168.0.1 31337\n");
  98.  
  99. printf ("\t./hK -z 192.168.0.1\n\n");
  100.  
  101. exit (1);
  102.  
  103. }
  104.  
  105.  
  106.  
  107. int main (int argc, char *argv [])
  108.  
  109. {
  110.  
  111. WSADATA wsa;
  112.  
  113. WSAStartup(MAKEWORD(2,2),&wsa);
  114.  
  115. time_t now=time (0);
  116.  
  117. struct tm *ahora;
  118.  
  119. char hora [40];
  120.  
  121. ahora=localtime ((const time_t*)&now);
  122.  
  123. strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  124.  
  125. printf ("hK 2.0 - By Sagrini (2010) - %s\n", hora);     //Esto escribira la presentacion.
  126.  
  127.  
  128.  
  129. if (argc < 3 || argc > 4) help ();      //Diferentes opciones y mal uso de linea de comandos.
  130.  
  131. if (strlen (argv [1]) != 2) help ();
  132.  
  133. if (argv [1][0] != '-') help ();
  134.  
  135.  
  136.  
  137. if (argv [1][1] == 'l' && argv [2] == NULL) help ();
  138.  
  139. if (argv [1][1] == 'c' && argv [3] == NULL) help ();
  140.  
  141. if (argv [1][1] == 'z' && argv [2] == NULL) help ();
  142.  
  143.  
  144.  
  145. if (argv [1][1] == 'l' && argv [3] != NULL) help ();
  146.  
  147. if (argv [1][1] == 'z' && argv [3] != NULL) help ();
  148.  
  149.  
  150.  
  151. if (argv [1][1] == 'l') listens (atoi (argv [2]));
  152.  
  153. if (argv [1][1] == 'c') conect (argv [2], atoi (argv [3]));
  154.  
  155. if (argv [1][1] == 'z') scan (argv [2]);
  156.  
  157.  
  158.  
  159. return 0;
  160.  
  161. }
  162.  
  163.  
  164.  
  165. int listens (int port) //Esta funcion esperara una conexion y printara todo lo que reciba...
  166.  
  167. {
  168.  
  169. if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
  170.  
  171. host.sin_port=htons(port);
  172.  
  173. host.sin_family=AF_INET;
  174.  
  175. host.sin_addr.s_addr=0;
  176.  
  177. memset (host.sin_zero, 0, 8);
  178.  
  179.  
  180. if(bind(sockfd,(struct sockaddr*)&host,sizeof(host))==-1) finalizar (1);
  181.  
  182. if(listen(sockfd,5)==-1) finalizar (1);
  183.  
  184.  
  185.  
  186. if((newsock=accept(sockfd, (struct sockaddr*)&client, &a))==-1) finalizar (1);
  187.  
  188. times ();
  189.  
  190. printf ("Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  191.  
  192. closesocket (sockfd);
  193.  
  194.  
  195.  
  196. cont=recv (newsock, buffer, 1024, 0);
  197.  
  198. while (cont>1)
  199.  
  200. {
  201.  
  202. buffer [cont-1]='\0';
  203.  
  204. printf ("RECV %d bytes: %s \n", cont, buffer);
  205.  
  206. cont=recv (newsock, buffer, 1024, 0);
  207.  
  208. }
  209.  
  210. closesocket (newsock);
  211.  
  212. closesocket (sockfd);
  213.  
  214.  
  215.  
  216. printf ("Finishing connection with %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  217.  
  218. finalizar (0);
  219.  
  220. }
  221.  
  222.  
  223.  
  224. int conect (char *IP, int port) //Se conecta a una IP y a su puerto determinado para mandar datos...
  225.  
  226. {
  227.  
  228. if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
  229.  
  230. host.sin_port=htons(port);
  231.  
  232. host.sin_family=AF_INET;
  233.  
  234. host.sin_addr.s_addr=inet_addr (IP);
  235.  
  236. memset (host.sin_zero, 0, 8);
  237.  
  238.  
  239.  
  240. if((connect (sockfd, (struct sockaddr*)&host, sizeof (host)))==-1) finalizar (1);
  241.  
  242. times ();
  243.  
  244. printf ("Got connection with %s:%d\n", inet_ntoa (host.sin_addr), ntohs (host.sin_port));
  245.  
  246.  
  247.  
  248. do
  249.  
  250. {
  251.  
  252. fgets (buffer, 1024, stdin);
  253.  
  254. cont=send (sockfd, buffer, strlen (buffer), 0);
  255.  
  256. }
  257.  
  258. while (cont>1);
  259.  
  260. closesocket (sockfd);
  261.  
  262. printf ("Finishing connection with %s:%d\n\n", inet_ntoa (host.sin_addr), ntohs (host.sin_port));
  263.  
  264. finalizar (0);
  265.  
  266. }
  267.  
  268.  
  269.  
  270. int scan (char *IP) //Sencillo escaner de puertos.
  271.  
  272. {
  273.  
  274. host.sin_addr.s_addr=inet_addr (IP);
  275.  
  276. host.sin_family=AF_INET;
  277.  
  278. memset (host.sin_zero, 0, 8);
  279.  
  280. for (cont = 0; cont < 65535; cont ++)
  281.  
  282. {
  283.  
  284. if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
  285.  
  286. host.sin_port = htons (cont);
  287.  
  288. if (connect (sockfd, (struct sockaddr*)&host, sizeof (host)) != -1)
  289.  
  290. {
  291.  
  292. printf ("Port %d open\n", cont);
  293.  
  294. k++;
  295.  
  296. }
  297.  
  298. else printf ("Port %d close\n", cont);
  299.  
  300. closesocket (sockfd);
  301.  
  302. }
  303.  
  304. if (k == 0) printf ("All ports closed...\n");
  305.  
  306. else printf ("%d port(s) open(s)...\n", k);
  307.  
  308. finalizar (0);
  309.  
  310. }
  311.  
  312.  

Suerte!
Recuerda que el code es mio, y se presento en un concurso...


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Destro- en 30 Enero 2011, 16:34 pm
Grax :),ya casi termino lo que toy haciendo xD.


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: leogtz en 31 Enero 2011, 00:10 am
Sagrini, creo que antes de gastar memoria deberías de checar si hay o no argumentos.
Código
  1. int main (int argc, char *argv [])
  2. {
  3. time_t now=time (0);
  4. struct tm *ahora;
  5. char hora [40];
  6. ahora=localtime ((const time_t*)&now);
  7. strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  8. printf ("hK 2.0 - By Sagrini (2010) - %s\n", hora);


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Garfield07 en 31 Enero 2011, 19:15 pm
Bah, eso ultimo se soluciona asi:
Código
  1. int main (int argc, char *argv [])
  2. {
  3. printf ("hK 2.0 - By Sagrini (2010)\n");
  4.  
Lo pongo de presentacion jeje, era un programa de concurso ;) para que quedase bonito :P aunque gracias por el detallito  ;D


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Nork en 31 Enero 2011, 21:14 pm
¿Qué es exactamente lo que quieres enviar a otro proceso (datos, notificación de eventos...)?
Si quieres comunicar dos procesos que están en el mismo PC, bajo mi punto de vista, usar sockets es lo más largo y pesado. Utilizaria pipes, signals... dependiendo de lo que quieras enviar claro.


Título: Re: Comunicar una aplicacion con otra(en la misma pc)?
Publicado por: Garfield07 en 31 Enero 2011, 22:08 pm
Si no quieres usar sockets siempre te quedan las pipes, pero bueno...
 Pipes  (http://tldp.org/LDP/lpg/node11.html)

Suerte!