Connecting to Server

This is a discussion on Connecting to Server within the Networking/Device Communication forums, part of the General Programming Boards category; I'm working on this client program in C, but it can never connect to the server. The Server is coded ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    Connecting to Server

    I'm working on this client program in C, but it can never connect to the server. The Server is coded in C as well, but it is in UNIX, this is for my work. Basically they want me to code the client program in Windows so they can have a windows version running, however, I can never get it to get authorization from the server. The part where it rejects me is below

    Code:

    Code:
    // server.c in UNIX
    
     /* The client makes a simple identification */ 
                    if(fgets(line,MAXLINE,fdr) == NULL) goto finish; 
                    /*if(sscanf(line,"%s %s %s",who,passwd,version) != 3) goto finish;*/ 
                    strcpy(interface, "stpc"); 
                    if(sscanf(line, "%s %s %s %s",who,passwd,version,interface) < 3) goto finish; 
                    // this printf ensures that line has an interface 
                    // for the logs 
                    sprintf(line, "%s %s %s %s\n",who,passwd,version,interface); 
                    if(strcmp(passwd,PASSWD) != 0) goto finish; 
                    fprintf(stplog," %s %s %s",timestamp(),whoami,line); 
                    fflush(stplog);  
      
                    /* Let the client know we are connected */ 
                    fprintf(fdw,"CONNECTED\n");  
                    fflush(fdw);  
    
    
    that was the server portion, and now below is the one I am trying to code, which is in windows.
    Code:
    Code:
    // client.c in windows  
    
      fd_rd = _open_osfhandle(ws_sockfd, 0);
    
            if (fd_rd == -1) exit(-1);
    
    
    	    ws_fdr= fdopen(fd_rd,"r"); 
    	
    	    ws_fdw= fdopen(fd_rd,"w");
    		
       
    	  
    
    	  if( ws_fdr == NULL || ws_fdw == NULL)
    	    err(FATAL,"STP: cannot open ws_fdw/ws_fdr\n");
    	  
    	  /* send authorization */
    	 
          printf("Sending authorization!\n");
    	  sprintf(line,"%s %s %s %s\n","STP",PASSWD,VERSION,"stpc"); // sets line to the ip, password, version, and interface
    	 
    	  ws_putline(line);
    	  fflush(ws_fdw);
    	  printf("Authorization sent, awaiting confirmation!\n");
    	  
    	  /* get connection acknowledgement */
    
    	  /*
    
    		Below is where we get our
    		Connection failures
    
    
          */
    	  ws_getline(line,MAXLINE);
    	  if(strcmp(line,"CONNECTED\n") != 0) {
    	    err(WARN,"STP: Connection rejected by waveserver - %s\n", line);
    	    continue;
    	  }
    
    
    
    int ws_getline(char *str, int maxlen)
       {
    	char *p, *fgets();
    	p= fgets(str,maxlen,ws_fdr);
    	if(p == NULL) return(EOF);
    	return(strlen(p));
       }
    
    
    void ws_putline(char *str)
       {
    	fwrite(str,1,strlen(str),ws_fdw);
    	fflush(ws_fdw);
       }
    Basically it's trying to send the server the info, which are the password, version, interface and STP. and then the server should verify it and then say OK, and copy "CONNECTED\n" into line, and the we should be connected. On UNIX, both the client program and Server work fine. Please help, thanks.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
      fd_rd = _open_osfhandle(ws_sockfd, _O_RDONLY);
    
            if (fd_rd == -1) exit(-1);
    
    
    	    ws_fdr= fdopen(fd_rd,"r"); 
    	
    	    ws_fdw= fdopen(fd_rd,"w");
    What is wrong here? You are trying to open the read-only fd returned by _open_osfhandle() to write on.
    Try:
    Code:
      fd_rd = _open_osfhandle(ws_sockfd, _O_RDWR);
    You need to include <fcntl.h> for these constants.

    Also, your code is a buffer overrun waiting to happen:
    Code:
                    if(sscanf(line, "%s %s %s %s",who,passwd,version,interface) < 3) goto finish; 
                    // this printf ensures that line has an interface 
                    // for the logs 
                    sprintf(line, "%s %s %s %s\n",who,passwd,version,interface);
    If you need further help, could you outline what your program is doing currently? What does the server receive? Where exactly does the client stop? How does it stop, is it waiting for a reply from the server?

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    215
    Thanks, i changed what you said to change. I was instructed not to change the server file, this is for my work, im interning at this place, and they want me to make it run on windows. But as far as what the server is getting, the first parameter is who the user is i think, and its just STP, and then the password and version are sent in, password "stpisgreat" and version is "1.4" and then the interface, which is just a string "stpc". I dont know what the point to those are but thats the way they designed it. There a site where I can post both the client and server programs on so you can see the entire code, which so ill send you those right now. And thanks a lot for all the help, I really appreciate it, Ive been

    Heres the client.c program:

    http://sourcepost.sytes.net/sourcevi...ource_id=14705

    And here's the server program:

    http://sourcepost.sytes.net/sourcevi...ource_id=14706

    On the client.c program, the lines in question are between
    00630 to 00670

    and for the server program: 00210 to 00230.

    Thanks for all your help. I definitely need it. I hope you can help me solving this problem, Ive spent since last thursday trying to figure it out.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to tell me how it is failing. Can I assume it is failing here:
    Code:
                  ws_getline(line,MAXLINE);
                   if(strcmp(line,"CONNECTED\n") != 0) {
                     err(WARN,"STP: Connection rejected by waveserver - %s\n", line);
                     continue;
                   }
    If so, what is the server returning? What is the contents of line at this point?

    EDIT:
    Also try changing to:
    Code:
    	    ws_fdr= fdopen(fd_rd,"rb"); 
    	
    	    ws_fdw= fdopen(fd_rd,"wb");
    This will open the streams as binary and avoid the line ending issue between unix/windows, although I doubt this is the problem.
    Last edited by anonytmouse; 06-08-2004 at 01:23 AM.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    215
    Oh ok, well I do believe that is where it is failing, for some reason, it is not equal to CONNECTED, however, on UNIX, that function works and the server connects. I checked on the debugger, line is equal to STP stpisgreat 1.4 stpc. I dont understand why it would work on UNIX but not on windows

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
                   sprintf(line,"%s %s %s %s\n","STP",PASSWD,VERSION,"stpc");
                   ws_putline(line);
                   fflush(ws_fdw);
                   
                   /* get connection acknowledgement */
                   ws_getline(line,MAXLINE);
                   if(strcmp(line,"CONNECTED\n") != 0) {
                     err(WARN,"STP: Connection rejected by waveserver - %s\n", line);
                     continue;
                   }
    >> line is equal to "STP stpisgreat 1.4 stpc" <<
    This is what you put into the line buffer with the call to sprintf(). So now we know that the ws_getline() call is failing (otherwise it would replace the contents of the line buffer). ws_getline() could be failing because ws_putline() is failing(ie. nothing has been sent to the server). You need to check the return value of fwrite() in ws_putline(). And did you try the binary mode I suggested in the previous post?

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    215
    Yeah I just tried th binary right now, still didnt work. I'll work on this later tomorrow, I have work in the morning, i did try checking the ws_getline and ws_putline functions in the debugger, but line was still the same. Ill work on it tomorrow. thanks for all the help. ill get back to you tomorrow about this.

    gnite

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    215
    ok, i tried looking through the debugger. I noticed that when it would assign the values of the password, interface, and version, etc. it would have this other character in the end, so to test it out I used strcpy(line, "STP stpisgreat 1.4 stpc"), but I still got the same error. I also noticed that in the putline function, fwrite returns 19 characters, which is right, since the string is from 0-19. Here is where I think the error is occurring. In the ws_getline() function.

    below is the code:

    Code:
    int ws_getline(char *str, int maxlen)
       {
    	char *p, *fgets();
    	p= fgets(str,maxlen,ws_fdr);
    	if(p == NULL) return(EOF);
    	return(strlen(p));
       }
    if p returns NULL, then there was an error. And that is exactly what is happening. I just noticed this using the debugger. So there is an error occuring within this function, but i dont know why that is.

    Also, one more thing. If you want to run my actual program, just copy and paste the client.c program, the server program is running on the net, so you can actually try to connect to it. I'm not suppose to change anything on the server.c program, they just let me take a look at it since I was having trouble with it.

    Hope you can help me solve this problem. Thanks.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> it would have this other character in the end <<
    That is the new line character('\n').

    Code:
    	char *p, *fgets();
    What is this? Remove the highlighted bit and tell me if it works.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    215
    Did that, it didnt work.

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I count 23 characters in "STP stpisgreat 1.4 stpc\n", not 19!

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    215
    Youre right, the reason why it was 19 for me, and now back to 23 is because in the code, you can take out the stpc part and just insert the first three items. The server will automatically put the stpc in if im not mistaken. But Ive put back the stpc in, and now its 23 again.

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
                            if(fgets(line,MAXLINE,fdr) == NULL) goto finish;
    Is it possible to tell me what the server is receiving? That is, the contents of line.

  14. #14
    Registered User
    Join Date
    May 2004
    Posts
    215
    I thought the server was receiving in the ws_putline function. Here's how it looks

    Code:
    void ws_putline(char *str)
       {
    	fwrite(str,1,strlen(str),ws_fdw);
    	fflush(ws_fdw);
       }
    and we do call the ws_putline function, and then afterwards the getline function.

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    But is the server actually receiving what we have sent?
    Code:
                         fprintf(stplog," %s %s %s",timestamp(),whoami,line);
    You should be able to look in the stplog to see if you are getting to that point.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. access violation when connecting to server
    By happyclown in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-10-2009, 01:46 AM
  2. connecting to server with wrong port
    By liri in forum Networking/Device Communication
    Replies: 3
    Last Post: 11-13-2007, 03:52 AM
  3. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21