Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

urgent help with sockets

hi,
can anyone please check the code and tell me the remedy for the segregation error i have when i execute this code....

server.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#if defined(_AIX) || defined(sun)
#include
#endif

#ifdef _AIX
#define socklen_t ssize_t
#endif

#ifdef sun
#define socklen_t int
#endif

#define NO_OF_MSG 5
#define SRVIPADDR INADDR_ANY
#define PORT 2007
#define LISTENQ 5
#define SA struct sockaddr
#define SA_IN struct sockaddr_in


static char *string[1000] = { "SERVER WELCOMES YOU TO POP3 SERVICE....
",
"ARE YOU A REGISTERED USER OR A NEW USER
"
"Please press 1, if already registered..
"
"Please press 2, if new user............
"
"Please press 3, if want to end.........
"
};


int main()
{

FILE *fp;
int i, s, ns, k;
SA_IN srvaddr;
int *ans;
int answer;
ans=&answer;
int reuse=1;
if((s=socket(AF_INET, SOCK_STREAM, 0))<0)
{
perror("Server socket error
");
exit(1);
}
bzero(&srvaddr, sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(PORT);
srvaddr.sin_addr.s_addr = htonl(SRVIPADDR);
if (setsockopt(s,SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int))==-1)
{
perror("Cant reuse the Server port/n");
exit(0);
}
if( (bind (s, (SA *)&srvaddr, sizeof(srvaddr) ) ) <0)
{
perror("Bind error
");
exit(0);
}
if( (listen(s, LISTENQ)) < 0)
{
perror("listen error");
exit(1);

}
while(1)
{
if((ns=(accept(s, (SA *)NULL, NULL)))<0)
{
perror("Accept error
");
exit(1);
}
/*write to client*/
fp=fdopen(ns, "r+");

for(i=0; i<NO_OF_MSG+1; i++)
{
send(ns,string[i],strlen(string[i]),MSG_DONTWAIT);
}
}
close(s);
exit(0);
}

Comments

  • gustavosserragustavosserra Member Posts: 201
    What do you mean with segregation error? Also, to keep indentation and improve readability, use the "code" formatting mode.

    : hi,
    : can anyone please check the code and tell me the remedy for the
    : segregation error i have when i execute this code....
    :
    : server.c
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : #if defined(_AIX) || defined(sun)
    : #include
    : #endif
    :
    : #ifdef _AIX
    : #define socklen_t ssize_t
    : #endif
    :
    : #ifdef sun
    : #define socklen_t int
    : #endif
    :
    : #define NO_OF_MSG 5
    : #define SRVIPADDR INADDR_ANY
    : #define PORT 2007
    : #define LISTENQ 5
    : #define SA struct sockaddr
    : #define SA_IN struct sockaddr_in
    :
    :
    : static char *string[1000] = { "SERVER WELCOMES YOU TO POP3
    : SERVICE....
    ",
    : "ARE YOU A REGISTERED USER OR A NEW
    : USER
    "
    : "Please press 1, if already
    : registered..
    "
    : "Please press 2, if new
    : user............
    "
    : "Please press 3, if want to
    : end.........
    "
    : };
    :
    :
    : int main()
    : {
    :
    : FILE *fp;
    : int i, s, ns, k;
    : SA_IN srvaddr;
    : int *ans;
    : int answer;
    : ans=&answer;
    : int reuse=1;
    : if((s=socket(AF_INET, SOCK_STREAM, 0))<0)
    : {
    : perror("Server socket error
    ");
    : exit(1);
    : }
    : bzero(&srvaddr, sizeof(srvaddr));
    : srvaddr.sin_family = AF_INET;
    : srvaddr.sin_port = htons(PORT);
    : srvaddr.sin_addr.s_addr = htonl(SRVIPADDR);
    : if (setsockopt(s,SOL_SOCKET, SO_REUSEADDR, &reuse,
    : sizeof(int))==-1)
    : {
    : perror("Cant reuse the Server port/n");
    : exit(0);
    : }
    : if( (bind (s, (SA *)&srvaddr, sizeof(srvaddr) ) ) <0)
    : {
    : perror("Bind error
    ");
    : exit(0);
    : }
    : if( (listen(s, LISTENQ)) < 0)
    : {
    : perror("listen error");
    : exit(1);
    :
    : }
    : while(1)
    : {
    : if((ns=(accept(s, (SA *)NULL, NULL)))<0)
    : {
    : perror("Accept error
    ");
    : exit(1);
    : }
    : /*write to client*/
    : fp=fdopen(ns, "r+");
    :
    : for(i=0; i<NO_OF_MSG+1; i++)
    : {
    :
    : send(ns,string[i],strlen(string[i]),MSG_DONTWAIT);
    : }
    : }
    : close(s);
    : exit(0);
    : }
    :
  • ndixonndixon Member Posts: 5
    : hi,
    : can anyone please check the code and tell me the remedy for the
    : segregation error i have when i execute this code....

    [italic](deleted)[/italic]
    :
    : static char *string[1000] = { "SERVER WELCOMES YOU TO POP3 SERVICE....
    ",
    : "ARE YOU A REGISTERED USER OR A NEW USER[color=Red]
    "[/color]
    : "Please press 1, if already registered..[color=Red]
    "[/color]
    : "Please press 2, if new user............[color=Red]
    "[/color]
    : "Please press 3, if want to end.........[color=Red]
    "[/color]
    : };
    :
    :
    : int main()
    : {
    :
    : FILE *fp;
    : int i, s, ns, k;
    : SA_IN srvaddr;
    : int *ans;
    : int answer;
    : ans=&answer;
    : int reuse=1;
    : if((s=socket(AF_INET, SOCK_STREAM, 0))<0)
    : {
    : perror("Server socket error
    ");
    : exit(1);
    : }
    : bzero(&srvaddr, sizeof(srvaddr));
    : srvaddr.sin_family = AF_INET;
    : srvaddr.sin_port = htons(PORT);
    : srvaddr.sin_addr.s_addr = htonl(SRVIPADDR);
    : if (setsockopt(s,SOL_SOCKET, SO_REUSEADDR, &reuse,
    : sizeof(int))==-1)
    : {
    : perror("Cant reuse the Server port/n");
    : exit(0);
    : }
    : if( (bind (s, (SA *)&srvaddr, sizeof(srvaddr) ) ) <0)
    : {
    : perror("Bind error
    ");
    : exit(0);
    : }
    : if( (listen(s, LISTENQ)) < 0)
    : {
    : perror("listen error");
    : exit(1);
    :
    : }
    : while(1)
    : {
    : if((ns=(accept(s, (SA *)NULL, NULL)))<0)
    : {
    : perror("Accept error
    ");
    : exit(1);
    : }
    : /*write to client*/
    : fp=fdopen(ns, "r+");
    :
    : for(i=0; i<NO_OF_MSG[color=Red]+[/color]1; i++)
    : {
    :
    : send(ns,string[i],strlen(string[i]),MSG_DONTWAIT);
    : }
    : }
    : close(s);
    : exit(0);
    : }
    :

    If you compile it with debugging enabled, and run it in a debugger, you'll see that it fails on line 87, because strlen() is trying to get the length of a NULL string when i==2.

    The reason for string[2] (and string[3] ...) being NULL is that you forgot commas in your declaration of string: string[0] is (correctly) set to "SERVER WELCOMES YOU...", but string[1] is set to the concatenation of "ARE YOU A REGISTERED USER..." and the "PLEASE PRESS ..." strings which follow it, because there are no commas between those strings.

    Therefore string[2] .. string[999] are all NULL, and your code was bound to fall over eventually, because there's no check for a NULL string.
    You should at least explicitly set a NULL terminating entry at the end of the string[] declaration, or even use memset() or bzero() to clear the remaining 995 pointers.
    E.g.
    [code]
    static char *string[1000] = { "SERVER WELCOMES YOU TO POP3 SERVICE....
    ",
    "ARE YOU A REGISTERED USER OR A NEW USER
    ",
    "Please press 1, if already registered..
    ",
    "Please press 2, if new user............
    ",
    "Please press 3, if want to end.........
    ",
    NULL
    };
    [/code]
    And if you want to iterate over the entire array, the loop boundary condition should be [bold]i<=NO_OF_MSG-1[/bold]

    The loop as you have written it would overstep the end of the array by 2 elements.

    But a better way would be something which just loops until it reaches the terminating entry:
    [code]
    for(i=0; i<NO_OF_MSG-1 && string[i] != NULL; i++)
    {
    send(ns,string[i],strlen(string[i]),MSG_DONTWAIT);
    }

    [/code]
    That avoid any unnecessary looping, and prevents any NULL string causing a crash.

    Oh, and that's a Segmentation Fault.


  • unixfreakunixfreak Member Posts: 9
    : hi,
    : can anyone please check the code and tell me the remedy for the
    : segregation error i have when i execute this code....
    :
    : server.c
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : #if defined(_AIX) || defined(sun)
    : #include
    : #endif
    :
    : #ifdef _AIX
    : #define socklen_t ssize_t
    : #endif
    :
    : #ifdef sun
    : #define socklen_t int
    : #endif
    :
    : #define NO_OF_MSG 5
    : #define SRVIPADDR INADDR_ANY
    : #define PORT 2007
    : #define LISTENQ 5
    : #define SA struct sockaddr
    : #define SA_IN struct sockaddr_in
    :
    :
    : static char *string[1000] = { "SERVER WELCOMES YOU TO POP3
    : SERVICE....
    ",
    : "ARE YOU A REGISTERED USER OR A NEW
    : USER
    "
    : "Please press 1, if already
    : registered..
    "
    : "Please press 2, if new
    : user............
    "
    : "Please press 3, if want to
    : end.........
    "
    : };
    :
    :
    : int main()
    : {
    :
    : FILE *fp;
    : int i, s, ns, k;
    : SA_IN srvaddr;
    : int *ans;
    : int answer;
    : ans=&answer;
    : int reuse=1;
    : if((s=socket(AF_INET, SOCK_STREAM, 0))<0)
    : {
    : perror("Server socket error
    ");
    : exit(1);
    : }
    : bzero(&srvaddr, sizeof(srvaddr));
    : srvaddr.sin_family = AF_INET;
    : srvaddr.sin_port = htons(PORT);
    : srvaddr.sin_addr.s_addr = htonl(SRVIPADDR);
    : if (setsockopt(s,SOL_SOCKET, SO_REUSEADDR, &reuse,
    : sizeof(int))==-1)
    : {
    : perror("Cant reuse the Server port/n");
    : exit(0);
    : }
    : if( (bind (s, (SA *)&srvaddr, sizeof(srvaddr) ) ) <0)
    : {
    : perror("Bind error
    ");
    : exit(0);
    : }
    : if( (listen(s, LISTENQ)) < 0)
    : {
    : perror("listen error");
    : exit(1);
    :
    : }
    : while(1)
    : {
    : if((ns=(accept(s, (SA *)NULL, NULL)))<0)
    : {
    : perror("Accept error
    ");
    : exit(1);
    : }
    : /*write to client*/
    : fp=fdopen(ns, "r+");
    :
    : for(i=0; i<NO_OF_MSG+1; i++)
    : {
    :
    : send(ns,string[i],strlen(string[i]),MSG_DONTWAIT);
    : }
    : }
    : close(s);
    : exit(0);
    : }
    :
    thanks my friend for a such an honest and effortful solution
Sign In or Register to comment.