Unix Programming - Error in Accept

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2007 > Error in Accept





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Error in Accept
sethukr@gmail.com

2007-02-26, 7:22 am

Hi everybody,
My server program shows "Invalid argument" in connect() call.

Server Code :

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#define MYPORT 7777
#define backlog 5

int main()
{
int sid,sockfd,sin_size,new_fd,n;
struct sockaddr_in own;
struct sockaddr_in their_addr;
char msg[256];
socklen_t len;
sid=socket(AF_INET,SOCK_STREAM,0);
perror("Socket creation");

own.sin_family=AF_INET;
own.sin_port=htons(MYPORT);
own.sin_addr.s_addr=htonl(INADDR_ANY);
memset(&their_addr,0,sizeof(their_addr));
sockfd= bind(sid,(struct sockaddr *)&own,sizeof(struct
sockaddr_in));
perror("Bind ");

listen(sockfd,backlog);
perror("Listen ");
len=sizeof(struct sockaddr_in);
new_fd=accept(sid,(struct sockaddr *)&their_addr,&len);
perror("Accept ");

while(1) {
memset(msg,0,sizeof(msg));
n=read(new_fd,msg,255);
printf("\n Read bytes : %d \n",n);
sleep(1);
if(n > 0)
printf("\n Received message : %s ",msg);
}
close(new_fd);
close(sid);
}


Can anybody help me on this???

Thanks in advance,
-Sethu

Rainer Temme

2007-02-26, 7:22 am

sethukr@gmail.com wrote:
> Hi everybody,
> My server program shows "Invalid argument" in connect() call.


Hmmm, your code doesn't contain a connect() call.


> Server Code :
>
> #include<string.h>
> #include<stdio.h>
> #include<stdlib.h>
> #include<sys/types.h>
> #include<sys/socket.h>
> #include<netinet/in.h>
> #include<arpa/inet.h>
>
> #define MYPORT 7777
> #define backlog 5
>
> int main()
> {
> int sid,sockfd,sin_size,new_fd,n;
> struct sockaddr_in own;
> struct sockaddr_in their_addr;
> char msg[256];
> socklen_t len;
> sid=socket(AF_INET,SOCK_STREAM,0);
> perror("Socket creation");
>


Always a good idea to memset(&own,0,sizeof(own));

> own.sin_family=AF_INET;
> own.sin_port=htons(MYPORT);
> own.sin_addr.s_addr=htonl(INADDR_ANY);
> memset(&their_addr,0,sizeof(their_addr));
> sockfd= bind(sid,(struct sockaddr *)&own,sizeof(struct
> sockaddr_in));


NO NO NO
bind() doesn't return a socket-fd ... it just returns
error or success ... man 2 bind

> perror("Bind ");
>
> listen(sockfd,backlog);


So, this should read listen(sid,backlog);

> perror("Listen ");
> len=sizeof(struct sockaddr_in);
> new_fd=accept(sid,(struct sockaddr *)&their_addr,&len);


This call is causing an error, because the listen() call
was made on an invalid filedescriptor.

.... rest of code zapped.

Rainer
sethukr@gmail.com

2007-02-26, 7:22 am

On Feb 26, 5:50 pm, Rainer Temme <Rainer_Te...@nospam.hotmail_dot_com>
wrote:
> seth...@gmail.com wrote:
>
> Hmmm, your code doesn't contain a connect() call.
>
>
>
>
>
>
>
> Always a good idea to memset(&own,0,sizeof(own));
>
>
> NO NO NO
> bind() doesn't return a socket-fd ... it just returns
> error or success ... man 2 bind
>
>
>
> So, this should read listen(sid,backlog);
>
>
> This call is causing an error, because the listen() call
> was made on an invalid filedescriptor.
>
> ... rest of code zapped.
>
> Rainer


oh. thank you very much
-Sethu

Rainer Weikusat

2007-02-26, 7:22 am

"sethukr@gmail.com" <sethukr@gmail.com> writes:

[...]

> int main()
> {
> int sid,sockfd,sin_size,new_fd,n;
> struct sockaddr_in own;
> struct sockaddr_in their_addr;
> char msg[256];
> socklen_t len;
> sid=socket(AF_INET,SOCK_STREAM,0);
> perror("Socket creation");
>
> own.sin_family=AF_INET;
> own.sin_port=htons(MYPORT);
> own.sin_addr.s_addr=htonl(INADDR_ANY);


> memset(&their_addr,0,sizeof(their_addr));


The memset has no technical purpose.

> sockfd= bind(sid,(struct sockaddr *)&own,sizeof(struct
> sockaddr_in));
> perror("Bind ");
>
> listen(sockfd,backlog);
> perror("Listen ");
> len=sizeof(struct sockaddr_in);
> new_fd=accept(sid,(struct sockaddr *)&their_addr,&len);
> perror("Accept ");
>
> while(1) {
> memset(msg,0,sizeof(msg));


And this memset hasn't, either.
Bin Chen

2007-02-26, 1:18 pm

On 2=D4=C226=C8=D5, =CF=C2=CE=E78=CA=B135=B7=D6, "seth...@gmail.com" <seth.=
..=2E@gmail.com> wrote:
> Hi everybody,
> My server program shows "Invalid argument" in connect() call.
>
> Server Code :
>
> #include<string.h>
> #include<stdio.h>
> #include<stdlib.h>
> #include<sys/types.h>
> #include<sys/socket.h>
> #include<netinet/in.h>
> #include<arpa/inet.h>
>
> #define MYPORT 7777
> #define backlog 5
>
> int main()
> {
> int sid,sockfd,sin_size,new_fd,n;
> struct sockaddr_in own;
> struct sockaddr_in their_addr;
> char msg[256];
> socklen_t len;
> sid=3Dsocket(AF_INET,SOCK_STREAM,0);
> perror("Socket creation");
>
> own.sin_family=3DAF_INET;
> own.sin_port=3Dhtons(MYPORT);
> own.sin_addr.s_addr=3Dhtonl(INADDR_ANY);
> memset(&their_addr,0,sizeof(their_addr));
> sockfd=3D bind(sid,(struct sockaddr *)&own,sizeof(struct
> sockaddr_in));


Bind's meaning is just like its name. It establishes a map between the
machine-entry(IP + PORT/UNIX domain socket path) and your process
using the fd returned from socket(). So only applications that need
input data need to do bind().
> perror("Bind ");
>
> listen(sockfd,backlog);
> perror("Listen ");
> len=3Dsizeof(struct sockaddr_in);
> new_fd=3Daccept(sid,(struct sockaddr *)&their_addr,&len);
> perror("Accept ");
>
> while(1) {
> memset(msg,0,sizeof(msg));
> n=3Dread(new_fd,msg,255);
> printf("\n Read bytes : %d \n",n);
> sleep(1);
> if(n > 0)
> printf("\n Received message : %s ",msg);
> }
> close(new_fd);
> close(sid);
>
> }
>
> Can anybody help me on this???
>
> Thanks in advance,
> -Sethu



matevzb

2007-02-26, 1:18 pm

On Feb 26, 1:35 pm, "seth...@gmail.com" <seth...@gmail.com> wrote:
> Hi everybody,
> My server program shows "Invalid argument" in connect() call.
>
> Server Code :
>
> #include<string.h>
> #include<stdio.h>
> #include<stdlib.h>
> #include<sys/types.h>
> #include<sys/socket.h>
> #include<netinet/in.h>
> #include<arpa/inet.h>
>
> #define MYPORT 7777
> #define backlog 5
>
> int main()
> {
> int sid,sockfd,sin_size,new_fd,n;
> struct sockaddr_in own;
> struct sockaddr_in their_addr;
> char msg[256];
> socklen_t len;
> sid=socket(AF_INET,SOCK_STREAM,0);
> perror("Socket creation");
>
> own.sin_family=AF_INET;
> own.sin_port=htons(MYPORT);
> own.sin_addr.s_addr=htonl(INADDR_ANY);
> memset(&their_addr,0,sizeof(their_addr));
> sockfd= bind(sid,(struct sockaddr *)&own,sizeof(struct
> sockaddr_in));
> perror("Bind ");
>
> listen(sockfd,backlog);
> perror("Listen ");
> len=sizeof(struct sockaddr_in);
> new_fd=accept(sid,(struct sockaddr *)&their_addr,&len);
> perror("Accept ");
>
> while(1) {
> memset(msg,0,sizeof(msg));
> n=read(new_fd,msg,255);
> printf("\n Read bytes : %d \n",n);
> sleep(1);
> if(n > 0)
> printf("\n Received message : %s ",msg);
> }
> close(new_fd);
> close(sid);
>
> }
>
> Can anybody help me on this???

Why did you post this twice under different subjects ("Error in Socket
program") if I may ask?
--
WYCIWYG - what you C is what you get

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com