Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   reverse connection (http://www.programmingforums.org/showthread.php?t=15701)

CPU Apr 26th, 2008 9:24 AM

reverse connection
 
In reverse connection server connects clients.(client is listening)So how clients sends its commands through to server?

kruptof Apr 26th, 2008 10:49 AM

Re: reverse connection
 
If the client is listening and accepting connections wouldn't that make it a server?

CPU Apr 26th, 2008 12:31 PM

Re: reverse connection
 
in reverse connection server acts like client and client acts like server

Freaky Chris Apr 26th, 2008 12:41 PM

Re: reverse connection
 
so the client remains as full client in which it sends commands to the server, the server then proccess these or whatever forwards them to another client etc.

And the server waits for commands from clients for it to process, but it is contantly trying to establish connections with listening clients?

This is a bit confusing but never the less it should be exactly the same to send messages to the server. As when in listening mode when it recieves a connection that connection is then bound and can be treated the same as if you were connecting to a listening server. No?


Chris

CPU Apr 26th, 2008 2:13 PM

Re: reverse connection
 
Finally i solve.
server sourcecodes:
:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <winsock.h>
  4. #include <string.h>
  5.  
  6. #define MAXPENDING 5
  7. #define BUFFER_SIZE 1024
  8. #define EXIT_CALL "exit"
  9.  
  10. int main()
  11. {
  12.         int local_socket = 0, remote_socket = 0, message_length = 0, remote_length = 0;
  13.         unsigned short local_port = 0, remote_port = 0;
  14.         struct sockaddr_in local_address, remote_address;
  15.         WSADATA wsa_data;
  16.         char message[BUFFER_SIZE], remote_ip[32];
  17.  
  18.         printf("Enter local port to use: ");
  19.         scanf("%d", &local_port);
  20.         fflush(stdin);
  21.  
  22.         if(WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0){
  23.                 fprintf(stderr, "WSAStartup() failed.\n");
  24.                 return (1);
  25.         }
  26.  
  27.         if((local_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
  28.                 fprintf(stderr, "socket() failed.\n");
  29.                 return(1);
  30.         }
  31.  
  32.         memset(&local_address, 0, sizeof(local_address));
  33.         local_address.sin_family = AF_INET;
  34.         local_address.sin_addr.s_addr = htonl(INADDR_ANY);
  35.         local_address.sin_port = htons(local_port);
  36.  
  37.         if(bind(local_socket, (struct sockaddr *) &local_address, sizeof(local_address)) < 0){
  38.                 fprintf(stderr, "bind() failed.\n");
  39.                 return(1);
  40.         }
  41.  
  42.         printf("Enter client IP: ");
  43.         scanf("%s", remote_ip);
  44.         fflush(stdin);
  45.  
  46.         printf("Enter client Port: ");
  47.         scanf("%d", &remote_port);
  48.         fflush(stdin);
  49.  
  50.         if((remote_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
  51.                 fprintf(stderr, "socket() failed.\n");
  52.                 return(1);
  53.         }
  54.  
  55.         memset(&remote_address, 0, sizeof(remote_address));
  56.         remote_address.sin_family = AF_INET;
  57.         remote_address.sin_addr.s_addr = inet_addr(remote_ip);
  58.         remote_address.sin_port = htons(remote_port);
  59.  
  60.         if(connect(remote_socket, (struct sockaddr *) &remote_address, sizeof(remote_address)) < 0){
  61.                 fprintf(stderr, "connect() failed.\n");
  62.                 return(1);
  63.         }
  64.  
  65.         if(listen(local_socket, MAXPENDING) < 0){
  66.                 fprintf(stderr, "listen() failed\n");
  67.                 return(1);
  68.         }
  69.  
  70.         do{
  71.                 memset(&message, 0, BUFFER_SIZE);
  72.                 if((message_length = recv(remote_socket, message, BUFFER_SIZE, 0)) < 0)
  73.                 {
  74.                         fprintf(stderr, "recv() failed.\n");
  75.                         return(1);
  76.                 }
  77.  
  78.                 printf("From client %s: %s\n", inet_ntoa(remote_address.sin_addr), message);
  79.         }while(strcmp(message, EXIT_CALL));
  80.  
  81.         WSACleanup();
  82.         closesocket(local_socket);
  83.         closesocket(remote_socket);
  84.         return(0);
  85. }


client source code:
:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <winsock.h>
  4. #include <string.h>
  5.  
  6. #define MAXPENDING 5
  7. #define BUFFER_SIZE 1024
  8. #define EXIT_CALL "exit"
  9.  
  10. int main()
  11. {
  12.         int local_socket = 0, remote_socket = 0, message_length = 0, remote_length = 0;
  13.         unsigned short local_port = 0, remote_port = 0;
  14.         struct sockaddr_in local_address, remote_address;
  15.         WSADATA wsa_data;
  16.         char message[BUFFER_SIZE];
  17.  
  18.         printf("Enter local port to use: ");
  19.         scanf("%d", &local_port);
  20.         fflush(stdin);
  21.  
  22.         if(WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0){
  23.                 fprintf(stderr, "WSAStartup() failed.\n");
  24.                 return (1);
  25.         }
  26.  
  27.         if((local_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
  28.                 fprintf(stderr, "socket() failed.\n");
  29.                 return(1);
  30.         }
  31.  
  32.         memset(&local_address, 0, sizeof(local_address));
  33.         local_address.sin_family = AF_INET;
  34.         local_address.sin_addr.s_addr = htonl(INADDR_ANY);
  35.         local_address.sin_port = htons(local_port);
  36.  
  37.         if(bind(local_socket, (struct sockaddr *) &local_address, sizeof(local_address)) < 0){
  38.                 fprintf(stderr, "bind() failed.\n");
  39.                 return(1);
  40.         }
  41.  
  42.         if(listen(local_socket, MAXPENDING) < 0){
  43.                 fprintf(stderr, "listen() failed\n");
  44.                 return(1);
  45.         }
  46.  
  47.         remote_length = sizeof(remote_address);
  48.  
  49.         for(;;){
  50.                 if((remote_socket = accept(local_socket, (struct sockaddr *) &remote_address, &remote_length) ) < 0){
  51.                         fprintf(stderr, "accept() failed.\n");
  52.                         return(1);
  53.                 }
  54.                 else{
  55.                         break;
  56.                 }
  57.         }
  58.  
  59.         do{
  60.                 memset(&message, 0, BUFFER_SIZE);
  61.                 printf("Enter message to send: ");
  62.                 gets(message);
  63.                 fflush(stdin);
  64.  
  65.                 message_length = (strlen(message));
  66.  
  67.                 if (send(remote_socket, message, message_length, 0) != message_length){
  68.                         fprintf(stderr, "send() failed.\n");
  69.                         return(1);
  70.                 }
  71.         }while(strcmp(message, EXIT_CALL));
  72.  
  73.         WSACleanup();
  74.         closesocket(local_socket);
  75.         closesocket(remote_socket);
  76.         return(0);
  77. }



All times are GMT -5. The time now is 4:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC