Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2008, 8:24 AM   #1
CPU
Newbie
 
Join Date: Apr 2008
Posts: 15
Rep Power: 0 CPU is on a distinguished road
reverse connection

In reverse connection server connects clients.(client is listening)So how clients sends its commands through to server?
CPU is offline   Reply With Quote
Old Apr 26th, 2008, 9:49 AM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
Re: reverse connection

If the client is listening and accepting connections wouldn't that make it a server?
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Apr 26th, 2008, 11:31 AM   #3
CPU
Newbie
 
Join Date: Apr 2008
Posts: 15
Rep Power: 0 CPU is on a distinguished road
Re: reverse connection

in reverse connection server acts like client and client acts like server
CPU is offline   Reply With Quote
Old Apr 26th, 2008, 11:41 AM   #4
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old Apr 26th, 2008, 1:13 PM   #5
CPU
Newbie
 
Join Date: Apr 2008
Posts: 15
Rep Power: 0 CPU is on a distinguished road
Re: reverse connection

Finally i solve.
server sourcecodes:
C Syntax (Toggle Plain Text)
  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:
C Syntax (Toggle Plain Text)
  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. }
CPU is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to figure out the best way to confirm a VNC connection... diablo75 Visual Basic .NET 8 Nov 4th, 2007 10:27 PM
SMTPlib - Connection reset by peer Sane Python 2 Jun 20th, 2006 5:40 PM
reverse string copy codylee270 C 23 Dec 3rd, 2005 8:41 PM
Connection Pooling java_roshan Other Web Development Languages 2 Nov 28th, 2005 3:24 PM
connection reset by peer / bad file descriptor opcis Perl 0 Feb 10th, 2005 3:29 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 PM.

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