Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-25-2007, 03:17 PM
robdapeepschamp robdapeepschamp is offline
Newbie
 
Join Date: Sep 2007
Posts: 3
Rep Power: 0
robdapeepschamp is on a distinguished road
Send a message via AIM to robdapeepschamp
Default Not letting sockaddr(s) go out of scope

I'm working on a program using static libraries & sockets with a server, and I thought I had it implemented correctly. I went to have it reviewed, since it isn't due for a week. The instructor said my sockaddr's could go out of scope and that I needed "some type of storage that maps file descriptor to the sockaddr pointer". He put a comment by the two lines in 'mysock.c' where it apparently will go out of scope. He mentioned something about creating a static struct to hold the separate data???

Can someone point me in the right direction/show me what he means? here are my .h/.c files.

mysock.h
Code:
#ifndef MYSOCK_H
#define MYSOCK_H

int tcp_init_server( const int port );
int tcp_server_accept( const int sd );
int tcp_client( const int server_port, const char * host, int * sd );

int fdprintf( const int fd, char * fmt, ... );
int fdgets(char *s, int size, const int fd );
int fdclose( const int fd );

#endif
mysock.c
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* for protobyname */
#include <netdb.h>
/* for socket, bind */
#include <sys/types.h>
#include <sys/socket.h>
/* fd io */
#include <unistd.h>
#include <stdarg.h>

int tcp_init_server(const int port)
{
    int proto_num = getprotobyname ( "tcp" )->p_proto;
    int sd;
    int rc;
    struct sockaddr_in serv; /* this goes out of scope */

    sd = socket( PF_INET, SOCK_STREAM, proto_num );
    if ( sd < 0 )
    {
        exit(1);
    }


    /*
     * setup the sockaddr_in
     */
    serv.sin_family = PF_INET;
    serv.sin_addr.s_addr = htonl ( INADDR_ANY );
    serv.sin_port = htons ( port );

    rc = bind( sd, (struct sockaddr *)&serv, sizeof(serv) );
    if ( rc < 0 )
    {
        exit(1);
    }

    rc = listen( sd, 10 );

    return sd;
}

int tcp_server_accept(const int sd)
{
    int sd2;
    struct sockaddr_in cli; /* this goes out of scope */
    socklen_t cli_len = sizeof(cli);

    sd2 = accept( sd, (struct sockaddr *)&cli, &cli_len );
    if ( sd2 <= 0 )
    {
        exit(1);
    }

    return sd2;
}

/*
 * input: a file descriptor along with printf style format string
 *          and arguements to match
 * output: processes the format string and arguments then writes the 
 *          data to the input file descriptor
 * return: the number of bytes written
 *
 * notes: hopefully getting identical behavior to do printf
 */
int fdprintf( const int fd, char * fmt, ... )
{
    char * buf = NULL;
    int bytes;
    va_list ap;

    va_start( ap, fmt );

    /* get the number of bytes needed */
    bytes = vsnprintf ( NULL, 0, fmt, ap );
    /* its actually one short */
    ++bytes;
    buf = ( char * ) malloc( sizeof(char) * ( bytes + 1 ) );
    bytes = vsnprintf ( buf, bytes, fmt, ap );

    /* write the data */
    write ( fd, buf, bytes );

    /* free the data and the va_list */
    va_end( ap );
    free ( buf );

    return bytes;
}

int fdgets(char *s, int size, const int fd )
{
	int r = read(fd, s, size);
	s[r] = 0;
	return r;
}

int fdclose( const int fd )
{
	return close (fd);
}
Thanks in advance!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-25-2007, 09:37 PM
limo limo is offline
Newbie
 
Join Date: Oct 2007
Location: Australia
Posts: 14
Rep Power: 0
limo is on a distinguished road
Default

Code:
int tcp_init_server(const int port)
Rewrite this to

Code:
int tcp_init_server(const int port, struct* sockaddr_in serv)
then in your tcp_init_server, remove the line
Code:
struct sockaddr_in serv; /* this goes out of scope */
You'll also have to change the dot operator in this segment of code to use the -> operator instead
Code:
serv.sin_family = PF_INET;
serv.sin_addr.s_addr = htonl ( INADDR_ANY );
serv.sin_port = htons ( port );
rc = bind( sd, (struct sockaddr *)&serv, sizeof(serv) );
to
Code:
serv->sin_family = PF_INET;
...
and
Code:
rc = bind( sd, serv, sizeof(serv) );
then modify int tcp_server_accept(const int sd) to take in cli as a pointer as well
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-26-2007, 06:31 PM
robdapeepschamp robdapeepschamp is offline
Newbie
 
Join Date: Sep 2007
Posts: 3
Rep Power: 0
robdapeepschamp is on a distinguished road
Send a message via AIM to robdapeepschamp
Default

Thanks for the input. I tried implementing those changes, and I get undeclared variable errors for 'serv' and 'cli'??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-27-2007, 10:31 AM
limo limo is offline
Newbie
 
Join Date: Oct 2007
Location: Australia
Posts: 14
Rep Power: 0
limo is on a distinguished road
Default

Quote:
Originally Posted by robdapeepschamp View Post
Thanks for the input. I tried implementing those changes, and I get undeclared variable errors for 'serv' and 'cli'??
Did you define the classes for sockaddr_in and include the definitions in a header file??

i.e.
struct sockaddr_in
{
//blah blah
};
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


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

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads