Thread: Connecting to Server

  1. #16
    Registered User
    Join Date
    May 2004
    Posts
    215
    I think timestamp is the time the user logs on.

    in the server code, here's how timestamp() is defined. its actually a structure.

    Code:
    char *  timestamp()  
       {  
            char *ctime();  
            static char buf[32]; 
            time_t t, time(); 
            t= time( (time_t *)(0)); 
            strcpy(buf,ctime(&t));  
            buf[24]= '\0'; /* get rid of newline */ 
            return(buf); 
       }

    whoami is is basically the address coming in. we initialize it by doing this:

    Code:
     /* Process one connection */ 
                    len = sizeof(cliaddr); 
                    if( (connfd = accept(listenfd, (SA *) &cliaddr, &len)) < 0) 
                       {  
                            if(errno == EINTR) continue; 
                            exit(-1); 
                       }  
                       // WHOAMI INITIALIZED BELOW
                    strcpy(whoami, inet_ntop(AF_INET,&cliaddr.sin_addr,buff,sizeof(buff)));

    and line is what is equal to the stuff coming in, like the interface, password, version, etc.

    so it writes all of that to stplog.

    the thing is, the server is being run on UNIX, so i dont know howd id do that, unless Id have to go back and make a copy of the server.c program, convert that to make it run on windows and then see what happens. I dont know how else i could get access to stplog.

  2. #17
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It looks like fgets doesn't work in this situation. I'm looking at it.

  3. #18
    Registered User
    Join Date
    May 2004
    Posts
    215
    I just noticed that on the putline function, it gets written, but when it does fflush, its returning -1.

    here's the function:
    Code:
    void ws_putline(char *str)
       {
    	fwrite(str,1,strlen(str),ws_fdw); // writes 23 characters
    	fflush(ws_fdw); // returns -1
       }

  4. #19
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes, I've replicated your error. I don't think writing through the stream functions is going to work. I think you'll have to get rid of of the _open_osfhandle() and fdopen() calls and use the raw socket functions. I'm sorry for wasting your time.

    Here is the new version of ws_putline():
    Code:
    void ws_putline(char *str)
       {
    	if (send(ws_sockfd, str, strlen(str), 0) == SOCKET_ERROR)
    	{
    		printf("send() failed with error %d\n", WSAGetLastError());
    	}
       }

  5. #20
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Similarly ws_putdata() becomes:
    Code:
    int ws_putdata(int *buf, int ndata)
       {
                 int n = send(ws_sockfd, buf, ndata, 0);
    
                 if (n == SOCKET_ERROR)
                {
    		printf("send() failed with error %d\n", WSAGetLastError());
                }
                 return(n);
        }

  6. #21
    Registered User
    Join Date
    May 2004
    Posts
    215
    You didnt waste my time, dont worry about it. How do I used raw socket functions. I put in the new putline, it still didnt work, ill use the debugger to check out whats going wrong. I just dont know what raw sockets functions are.

  7. #22
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    ws_getdata() becomes:
    Code:
            ws_getdata(int *buf, int ndata)
                {
                 int n = recv(ws_sockfd, buf, ndata, 0);
    
                 if (n == SOCKET_ERROR)
                {
    		printf("recv() failed with error %d\n", WSAGetLastError());
                }
                 return(n);
                }

  8. #23
    Registered User
    Join Date
    May 2004
    Posts
    215
    for getline here's what i wrote, but i still got the same thing.

    Code:
    int ws_getline(char *str, int maxlen)
       {
    	char *p;
    //	p= fgets(str,maxlen,ws_fdr);
    //	if(p == NULL) return(EOF);
    //	return(strlen(p));
    
    	int ret;
    
    	ret = recv(ws_sockfd,p,maxlen,0);
    	str=p;
    	return ret;
       }
    return is still -1.

    for ws_putline(), i did pretty much what you said, I changed it a little.

    Code:
    void ws_putline(char *str)
       {
    	//fwrite(str,1,strlen(str),ws_fdw); // writes 23 characters
     	//fflush(ws_fdw);// returns EOF error. returns -1 value
    
    	int n=0;
    	if (send(ws_sockfd, str, strlen(str), 0) == SOCKET_ERROR)
    	{
    		printf("send() failed with error %d\n", WSAGetLastError());
    	}
    	else
    	{
    	n=	send(ws_sockfd, str, strlen(str), 0);
    	n;
    	str;
    
    	}
    
    
       }
    n is equal to 23 when this is said and done with

  9. #24
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is an implementation of ws_getline():
    Code:
    int ws_getline(char *str, int maxlen)
    {
    	int total = 0;
    
    	while (total < maxlen - 1)
    	{
    		/* Get a character */
    		int ret = recv(ws_sockfd, str, 1, 0);
    
    		if (ret == SOCKET_ERROR)
    		{
    			printf("recv() failed with error %d\n", WSAGetLastError());
    			break;
    		}
    		else if (ret > 0)
    		{
    			/* Increment received character count */
    			total++;
    			/* Check if we are finished this line */
    			if (*str == '\n') break;
    			/* Move to next character in buffer */
    			str++;
    		}
    	}
    
    	*str = '\0';
    
    	return total;
    }

  10. #25
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    No, your ws_putline is calling send() twice. If you need the return value from send() use:
    Code:
    void ws_putline(char *str)
       {
    	int n = send(ws_sockfd, str, strlen(str), 0);
    
    	if (n == SOCKET_ERROR)
    	{
    		printf("send() failed with error %d\n", WSAGetLastError());
    	}
       }

  11. #26
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Once you have replaced the functions it should work. You can delete these lines entirely:
    Code:
                  fd_rd = _open_osfhandle(ws_sockfd, _O_RDWR);
             
                     if (fd_rd == -1) exit(-1);
             
                     ws_fdr= fdopen(fd_rd,"r");  
                 
                     ws_fdw= fdopen(fd_rd,"w"); 
    00640:           
                
                 
             
                   if( ws_fdr == NULL || ws_fdw == NULL)
                     err(FATAL,"STP: cannot open ws_fdw/ws_fdr\n");

  12. #27
    Registered User
    Join Date
    May 2004
    Posts
    215
    I tried that, it made my program freeze at the line

    int ret = recv(ws_sockfd, str, 1, 0);

  13. #28
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    That means it is waiting to receive something. Could you post the new code?

  14. #29
    Registered User
    Join Date
    May 2004
    Posts
    215
    I will, but should it take this long to receive, i mean its been sitting here for a while

  15. #30
    Registered User
    Join Date
    May 2004
    Posts
    215
    I was also thinking, you know how I do have the ws_fdr and ws_fdw in the server, but i wont be using it in this, is that going to affect how this thing logs on or gets the password, version, interface, etc.?

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, 12: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, 01:54 PM