Link to home
Start Free TrialLog in
Avatar of angelos-constantinides
angelos-constantinidesFlag for United Kingdom of Great Britain and Northern Ireland

asked on

programming socket in c

I am new in programming socket and i try to copy an example of server and client.The problem is that appear a lot of wanrning and the code is not running.Because i am in confuse can you help me on this???

server code
--------------
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
static void serve(int);
#define PORT 6060
main( int argc, char **argv )
{
int lsd; /* Listening socket */
struct sockaddr_in sin; /* Binding struct */

int sin_size=sizeof(sin);
int sd; /* Socket to accept new connexion */

/* Create listening socket */

if ( (lsd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
fprintf(stderr, "%s: cannot create listening socket: ",
argv[0]);
perror(0);
exit(1);
}

sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if ( bind(lsd, &sin, sin_size) < 0 ) {
fprintf(stderr, "%s: cannot bind listening socket: ",
argv[0]);
perror(0);
exit(1);
}

/* Initiate a listen queue */
if ( listen(lsd, 5) < 0 ) {
fprintf(stderr, "%s: cannot listen on socket: ", argv[0]);
perror(0);
exit(1);
}
/* Take care of the SIGPIPE signal - ignore it */
signal(SIGPIPE, SIG_IGN);
while ( 1 ) {
if ( (sd=accept(lsd, &sin, &sin_size)) < 0 )
exit(errno);
serve(sd);
shutdown(sd, 2);
close(sd);
}
}


void serve(int sd)
{
time_t local_time;
char *time_string;
time(&local_time);
time_string = ctime(&local_time);
write(sd, time_string, strlen(time_string));
return;
}

with warning:
server.c: In function 'main':
server.c:28: warning: incompatible implicit declaration of built-in function 'exit'
server.c:36: warning: passing argument 2 of 'bind' from incompatible pointer type
server.c:39: warning: incompatible implicit declaration of built-in function 'exit'
server.c:47: warning: incompatible implicit declaration of built-in function 'exit'
server.c:55: warning: passing argument 2 of 'accept' from incompatible pointer type
server.c:55: warning: pointer targets in passing argument 3 of 'accept' differ in signedness
server.c:56: warning: incompatible implicit declaration of built-in function 'exit'
server.c: In function 'serve':
server.c:69: warning: assignment makes pointer from integer without a cast
server.c:70: warning: incompatible implicit declaration of built-in function 'strlen'


and client.c
---------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define BUFSIZE 1024
#define SERVER_PORT 6060

main( int argc, char **argv )
{
int sd; /* Socket descriptor */
struct sockaddr_in server; /* Server to connect */
struct hostent *server_host; /* Host info */
char buf[BUFSIZE];
int nbytes;

/* Create socket */
if ( (sd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
fprintf(stderr, "%s: cannot create socket: ", argv[0]);
perror(0);
exit(1);
}

/* Get info on host */
if ( (server_host=gethostbyname(argv[1])) == NULL ) {
fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]);
exit(1);
}

/* Set up struct sockaddr_in */
server.sin_family = AF_INET;
server.sin_port = SERVER_PORT;
bcopy((char*)server_host->h_addr, (char*)&server.sin_addr,
server_host->h_length);

/* Connect */
if ( connect(sd, &server, sizeof(server)) < 0 ) {
fprintf(stderr, "%s: cannot connect to server: ", argv[0]);
perror(0);
exit(1);
}

/* Get date */
if ( (nbytes=read(sd, buf, BUFSIZE-1)) <= 0 ) {
fprintf(stderr, "%s: read failed: ", argv[0]);
perror(0);
exit(1);
}

buf[nbytes] = 0;
printf("Date on host %s is: %s\n", argv[1], buf);
close(sd);
exit(0);
}


with warning:




client.c: In function 'main':
client.c:21: warning: incompatible implicit declaration of built-in function 'exit'
client.c:27: warning: incompatible implicit declaration of built-in function 'exit'
client.c:33: warning: incompatible implicit declaration of built-in function 'bcopy'
client.c:37: warning: passing argument 2 of 'connect' from incompatible pointer type
client.c:40: warning: incompatible implicit declaration of built-in function 'exit'
client.c:47: warning: incompatible implicit declaration of built-in function 'exit'
client.c:53: warning: incompatible implicit declaration of built-in function 'exit'

 
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

exit is declared in stdlib.h do include that header. The more serious message is the message for connect please check that and if need be and you are sure you variable is fine you can cast to the proper datatyp.
Yeah that need is there you have to cast to (struct sockaddr *) &server,

Regards
Friedrich
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I can really recommend this socket programming tutorial btw :

        http://beej.us/guide/bgnet/
Avatar of angelos-constantinides

ASKER

I have done all the change and  when i compile the code all is ok (no warnings).When i try to run code for  server looks to work but code for client appear segmentation fault.
Also i run the code on the same computer (not a network).Is it make problem this?

code for client:


#include <stdio.h>

#include <stdlib.h>
#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>
#include <string.h>
#define BUFSIZE 1024

#define SERVER_PORT 6060



main( int argc, char **argv )

{

int sd; /* Socket descriptor */

struct sockaddr_in server; /* Server to connect */

struct hostent *server_host; /* Host info */

char buf[BUFSIZE];

int nbytes;



/* Create socket */

if ( (sd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {

fprintf(stderr, "%s: cannot create socket: ", argv[0]);

perror(0);

exit(1);

}



/* Get info on host */

if ( (server_host=gethostbyname(argv[1])) == NULL ) {

fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]);

exit(1);

}



/* Set up struct sockaddr_in */

server.sin_family = AF_INET;

server.sin_port = SERVER_PORT;

bcopy((char*)server_host->h_addr, (char*)&server.sin_addr,
server_host->h_length);



/* Connect */

if ( connect(sd, (struct sockaddr*)&server, sizeof(server)) < 0 ) {

fprintf(stderr, "%s: cannot connect to server: ", argv[0]);

perror(0);

exit(1);

}



/* Get date */

if ( (nbytes=read(sd, buf, BUFSIZE-1)) <= 0 ) {

fprintf(stderr, "%s: read failed: ", argv[0]);

perror(0);

exit(1);

}



buf[nbytes] = 0;

printf("Date on host %s is: %s\n", argv[1], buf);

close(sd);

exit(0);

}


 
>> Also i run the code on the same computer (not a network).Is it make problem this?

That shouldn't be a problem (as long as you specify the IP address for that computer)


>> When i try to run code for  server looks to work but code for client appear segmentation fault.

Instead of this :

        bcopy((char*)server_host->h_addr, (char*)&server.sin_addr, server_host->h_length);

try this :

        server.sin_addr = *((struct in_addr*) server_host->h_addr);
        memset(server.sin_zero, '\0', sizeof(server.sin_zero));

If that doesn't help, find out at what line the segmentation fault occurs.