|
Home > Archive > Unix Programming > February 2007 > Error in Socket program
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 Socket program
|
|
| 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
| |
| 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???
Since the server doesn't use connect(), it's probably either the
client connect() that failed or server's listen()/accept(). The things
wrong with your code:
- no need for perror() if the call succeeded
- bind() doesn't return a socket file descriptor, but rather 0 or -1
- you pass the bind()'s return value to listen(), which of course
won't work
- the original descriptor is passed to accept(), but it wasn't
bind()ed
So, clean up your code first (there is need for only one socket file
descriptor), then it should somewhat work (I didn't check the code in
detail). I also suggest you read Richard Steven's excellent book
titled "Unix Network Programming" and/or check "Beej's Guide to
Network Programming" at http://beej.us/guide/bgnet/output/h...gle/bgnet.html.
--
WYCIWYG - what you C is what you get
|
|
|
|
|