For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > February 2008 > why Gcc unknows the struct ?









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 why Gcc unknows the struct ?
Erfan

2008-02-17, 5:02 am

hi,i am now reading this code,and complie it with my PC.
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>
#include<unistd.h>
#define MAXLINE 20

int main(int argc,char **argv)
{
int sockfd,n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;

if(argc!=2)
printf("wrong parameters\n");

if((sockfd=socket(AF_INET,SOCK_STREAM,0)
)<0)
printf("socket error\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13);
if(connect(sockfd,(struct sockaddr *)
&servaddr,sizeof(servaddr))<0)
printf("connect error\n");

while((n=read(sockfd,recvline,MAXLINE))>0)
{
recvline[n]=0;
if(fputs(recvline,stdout)==EOF)
printf("fputs_error\n");
}
if(n<0)
printf("read error\n");
exit(0);
}
-----------------------
It`s a example that used to get the current data from the server.
however,when i use gcc to complie it ,there comes one error,that
means the size of "servaddr" is unkown. why and how ? as far as
i know,the defination of "servaddr" is in the headers above,we don`t
need
to limit or rewrite it when we just want to use it.
i try to google,one suggestion is to try "apt-get install libsctp-
dev",when i install the lib,nothing happen. : (
Best wishes
Ian Collins

2008-02-17, 5:02 am

Erfan wrote:
> hi,i am now reading this code,and complie it with my PC.
> #include<sys/types.h>
> #include<sys/socket.h>
> #include<stdio.h>
> #include<sys/un.h>
> #include<unistd.h>


Missing <stdlib.h> and <strings.h>

> #define MAXLINE 20
>
> int main(int argc,char **argv)
> {
> int sockfd,n;
> char recvline[MAXLINE+1];
> struct sockaddr_in servaddr;
>
> if(argc!=2)
> printf("wrong parameters\n");
>
> if((sockfd=socket(AF_INET,SOCK_STREAM,0)
)<0)
> printf("socket error\n");
> bzero(&servaddr,sizeof(servaddr));
> servaddr.sin_family=AF_INET;
> servaddr.sin_port=htons(13);
> if(connect(sockfd,(struct sockaddr *)
> &servaddr,sizeof(servaddr))<0)
> printf("connect error\n");
>
> while((n=read(sockfd,recvline,MAXLINE))>0)
> {
> recvline[n]=0;
> if(fputs(recvline,stdout)==EOF)
> printf("fputs_error\n");
> }
> if(n<0)
> printf("read error\n");
> exit(0);
> }
> -----------------------
> It`s a example that used to get the current data from the server.
> however,when i use gcc to complie it ,there comes one error,that
> means the size of "servaddr" is unkown. why and how ? as far as
> i know,the defination of "servaddr" is in the headers above,we don`t
> need


On which platform? The code compiles fine on Solaris.

--
Ian Collins.
Erfan

2008-02-17, 5:02 am

On 2=D4=C217=C8=D5, =CF=C2=CE=E73=CA=B134=B7=D6, Ian Collins <ian-n...@hotma=
il.com> wrote:
> Erfan wrote:
>
> Missing <stdlib.h> and <strings.h>
>
>
>
>
>
>
>
>
> On which platform? The code compiles fine on Solaris.
>
> --
> Ian Collins.


Ubuntu7.04.
i lost <netinet/in.h>, : )
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com