Error in Accept
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Error in Accept




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Error in Accept  
sethukr@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 12:22 PM

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






[ Post a follow-up to this message ]



    Re: Error in Accept  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 12:22 PM

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





[ Post a follow-up to this message ]



    Re: Error in Accept  
sethukr@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 12:22 PM

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






[ Post a follow-up to this message ]



    Re: Error in Accept  
Rainer Weikusat


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 12:22 PM

"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.





[ Post a follow-up to this message ]



    Re: Error in Accept  
Bin Chen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 06: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







[ Post a follow-up to this message ]



    Re: Error in Accept  
matevzb


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 06: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






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:43 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register