guys, ive copied a code about socket programming just to see how it runs. problem is i cant compile it everytime i compile client.c it shows and error. here is the error

client.c: In function ‘error’:
client.c:9: warning: incompatible implicit declaration of built-in function ‘exit’
client.c: In function ‘main’:
client.c:19: warning: incompatible implicit declaration of built-in function ‘exit’
client.c:28: warning: incompatible implicit declaration of built-in function ‘exit’
client.c:30: warning: incompatible implicit declaration of built-in function ‘bzero’
client.c:32: warning: incompatible implicit declaration of built-in function ‘bcopy’
client.c:37: warning: passing argument 2 of ‘connect’ from incompatible pointer type
client.c:42: warning: incompatible implicit declaration of built-in function ‘strlen’

here is the code for client.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}

can anybody please clarify how to properly compile this program? thanks

I think you need to #include <stdlib.h>

Good Luck!

I think you need to #include <stdlib.h>

Good Luck!

i've inclued <stdlib.h> now it produces a new error. here the error

client.c: In function ‘error’:
client.c:9: warning: incompatible implicit declaration of built-in function ‘exit’
client.c: In function ‘main’:
client.c:19: warning: incompatible implicit declaration of built-in function ‘exit’
client.c:28: warning: incompatible implicit declaration of built-in function ‘exit’
client.c:30: warning: incompatible implicit declaration of built-in function ‘bzero’
client.c:32: warning: incompatible implicit declaration of built-in function ‘bcopy’
client.c:37: warning: passing argument 2 of ‘connect’ from incompatible pointer type
client.c:42: warning: incompatible implicit declaration of built-in function ‘strlen’

Try including <string.h> to fix the bzero and strlen .etc

The exit(int) function is defined in stdlib.h, so it should be fine. Weird error there.

You should explicitly cast the sockaddr_in to a sockaddr for connect.

Try including <string.h> to fix the bzero and strlen .etc

The exit(int) function is defined in stdlib.h, so it should be fine. Weird error there.

You should explicitly cast the sockaddr_in to a sockaddr for connect.

what do you mean by explicitly cast sockaddr_in to a sockaddr for connect? how would i do that..im just a newbie in socket programming and just starting to study this code.thanks

Here's a quick and ugly workaround.

Replace your connect statement with:

struct sockaddr *to_cast = (struct sockaddr *)serv_addr;
if (connect(sockfd,to_cast,sizeof(serv_addr)) < 0)

Basically, connect takes a struct sockaddr * as an argument, but you were passing it a struct sockaddr_in *. Luckily, it's safe to cast between those two types, as they were designed to be compatible.

Oh, and BTW, you need to #include <stdlib.h> BEFORE all other headers. That should fix all those weird errors. : )

Here's a quick and ugly workaround.

Replace your connect statement with:

struct sockaddr *to_cast = (struct sockaddr *)serv_addr;
if (connect(sockfd,to_cast,sizeof(serv_addr)) < 0)

Basically, connect takes a struct sockaddr * as an argument, but you were passing it a struct sockaddr_in *. Luckily, it's safe to cast between those two types, as they were designed to be compatible.

Oh, and BTW, you need to #include <stdlib.h> BEFORE all other headers. That should fix all those weird errors. : )

i've inserted the code as you said..now it produces a new error. heres the error:

In function ‘main’:
client.c:45: warning: passing argument 2 of ‘connect’ from incompatible pointer type

phew!!socket program is not that easy after all...what wrong with this code? thanks ^_^

Please post your newest code.

1.
     /* struct sockaddr *to_cast = (struct sockaddr *)serv_addr; */
   2.
      if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))) < 0)

I forgot the "&" in my last post, sorry about that!
Also, consider making the struct sockaddr * into a const struct sockaddr * (the official 2nd argument type to connect())

As shown in a prior post, you can combine these two lines into one line as well, but IMO separating the statements out can add to clarity. (Sometimes.)

Try this out:

const struct sockaddr *to_cast = (struct sockaddr *)&serv_addr;
if (connect(sockfd,to_cast,sizeof(serv_addr)) < 0)

Here is a page describing the arguments to connect() in detail :
http://www.opengroup.org/onlinepubs/000095399/functions/connect.html

include appropriate header files..
bcopy and bzero is in string.h
exit is in stdlib.h
so if u include it wont be a problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.