Some basic examples of C network programming practiced after this morning
Client1
/* Make the necessary between des and set up the variables. */# include
# include
# include
# include
# include
# include
int main () {int sockfd; int Len; struct sockaddr_un address; int result; char CH = 'a';/* Create a socket for the client. */sockfd = socket (af_unix, sock_stream, 0);/* Name the socket, as agreed with the server. */address. sun_family = af_unix; strcpy (address. sun_path, "server_socket"); Len = sizeof (Address);/* Now connect our socket to the server's socket. */result = connect (sockfd, (struct sockaddr *) & Address, Len); If (result =-1) {perror ("Oops: Client1 "); exit (1);}/* We can now read/write via sockfd. */write (sockfd, & Ch, 1); read (sockfd, & Ch, 1); printf ("char from server = % C \ n", CH ); close (sockfd); exit (0) ;}
server1.c
/* Make the necessary between des and set up the variables. */# include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <sys/UN. h> # include <unistd. h> # include <stdlib. h> int main () {int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_un server_address; struct sockaddr_un client_address;/* remove any old socket and create an unnamed socket for the server. */unlink ("server_socket"); server_sockfd = socket (af_unix, sock_stream, 0);/* Name the socket. */server_address.sun_family = af_unix; strcpy (Clerk, "server_socket"); server_len = sizeof (server_address); BIND (server_sockfd, (struct sockaddr *) & server_address, server_len ); /* create a connection queue and wait for clients. */Listen (server_sockfd, 5); While (1) {char ch; printf ("server waiting \ n");/* accept a connection. */client_len = sizeof (client_address); client_sockfd = accept (server_sockfd, (struct sockaddr *) & client_address, & client_len);/* We can now read/write to client on client_sockfd. */read (client_sockfd, & Ch, 1); ch ++; write (client_sockfd, & Ch, 1); close (client_sockfd );}}
Example of the second Server Client:
Client
/* Make the necessary between des and set up the variables. */# include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <stdlib. h> int main () {int sockfd; int Len; struct sockaddr_in address; int result; char CH = 'a';/* Create a socket for the client. */sockfd = socket (af_inet, sock_stream, 0);/* Name the socket, as agreed with the server. */address. sin_family = af_inet; address. sin_addr.s_addr = inet_addr ("127.0.0.1"); Address. sin_port = htons (9734); Len = sizeof (Address);/* Now connect our socket to the server's socket. */result = connect (sockfd, (struct sockaddr *) & Address, Len); If (result =-1) {perror ("Oops: client3 "); exit (1);}/* We can now read/write via sockfd. */write (sockfd, & Ch, 1); read (sockfd, & Ch, 1); printf ("char from server = % C \ n", CH ); close (sockfd); exit (0 );}
server
/* Make the necessary between des and set up the variables. */# include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <stdlib. h> int main () {int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address;/* remove any old socket and create an unnamed socket for the server. */server_sockfd = socket (af_inet, sock_stream, 0);/* Name the socket. */outputs = af_inet; outputs = htonl (inaddr_any); outputs = htons (9734); server_len = sizeof (server_address); BIND (server_sockfd, (struct sockaddr *) & server_address, server_len);/* create a connection queue and wait for clients. */Listen (server_sockfd, 5); While (1) {char ch; printf ("server waiting \ n");/* accept a connection. */client_len = sizeof (client_address); client_sockfd = accept (server_sockfd, (struct sockaddr *) & client_address, & client_len);/* We can now read/write to client on client_sockfd. */read (client_sockfd, & Ch, 1); ch ++; write (client_sockfd, & Ch, 1); close (client_sockfd );}}
/* As usual, make the appropriate primary des and declare the variables. */# include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <netdb. h> # include <stdio. h> # include <stdlib. h> int main (INT argc, char * argv []) {char * Host, ** names, ** addrs; struct hostent * hostinfo; /* set the host in question to the argument supplied with the getname call, or default to the user's machine. */If (argc = 1) {char myname [256]; gethostname (myname, 255); host = myname;} else host = argv [1]; /* Make the call to gethostbyname and report an error if no information is found. */hostinfo = gethostbyname (host); If (! Hostinfo) {fprintf (stderr, "cannot get info for host: % s \ n", host); exit (1 );} /* display the hostname and any aliases it may have. */printf ("results for host % s: \ n", host); printf ("Name: % s \ n", hostinfo-> h_name); printf ("aliases: "); names = hostinfo-> h_aliases; while (* names) {printf (" % s ", * names); names ++;} printf (" \ n "); /* warn and exit if the host in question isn't an IP host. */If (hosti Nfo-> h_addrtype! = Af_inet) {fprintf (stderr, "not an IP host! \ N "); exit (1);}/* Otherwise, display the IP address (es ). */addrs = hostinfo-> h_addr_list; while (* addrs) {printf ("% s", inet_ntoa (* (struct in_addr *) * addrs); addrs ++ ;} printf ("\ n"); exit (0 );}
/* Start with the usual includes and declarations. */# include <sys/socket. h> # include <netinet/in. h> # include <netdb. h> # include <stdio. h> # include <unistd. h> # include <stdlib. h> int main (INT argc, char * argv []) {char * Host; int sockfd; int Len, result; struct sockaddr_in address; struct hostent * hostinfo; struct servent * servinfo; char buffer [128]; If (argc = 1) host = "localhost"; else host = argv [1];/ * Find the host address and report an error if none is found. */hostinfo = gethostbyname (host); If (! Hostinfo) {fprintf (stderr, "no host: % s \ n", host); exit (1) ;}/ * check that the daytime service exists on the host. */servinfo = getservbyname ("Daytime", "TCP"); If (! Servinfo) {fprintf (stderr, "No daytime service \ n"); exit (1);} printf ("Daytime port is % d \ n ", ntohs (servinfo-> s_port);/* Create a socket. */sockfd = socket (af_inet, sock_stream, 0);/* construct the address for use with connect... */address. sin_family = af_inet; address. sin_port = servinfo-> s_port; address. sin_addr = * (struct in_addr *) * hostinfo-> h_addr_list; Len = sizeof (Address );/*... then connect and get the information. */result = connect (sockfd, (struct sockaddr *) & Address, Len); If (result =-1) {perror ("Oops: getdate "); exit (1);} result = read (sockfd, buffer, sizeof (buffer); buffer [Result] = '\ 0'; printf ("read % d Bytes: % s ", result, buffer); close (sockfd); exit (0 );}
getdate-udp
/* Start with the usual includes and declarations. */# include <sys/socket. h> # include <netinet/in. h> # include <netdb. h> # include <stdio. h> # include <unistd. h> # include <stdlib. h> int main (INT argc, char * argv []) {char * Host; int sockfd; int Len, result; struct sockaddr_in address; struct hostent * hostinfo; struct servent * servinfo; char buffer [128]; If (argc = 1) host = "localhost"; else host = argv [1];/ * Find the host address and report an error if none is found. */hostinfo = gethostbyname (host); If (! Hostinfo) {fprintf (stderr, "no host: % s \ n", host); exit (1) ;}/ * check that the daytime service exists on the host. */servinfo = getservbyname ("Daytime", "UDP"); If (! Servinfo) {fprintf (stderr, "No daytime service \ n"); exit (1);} printf ("Daytime port is % d \ n ", ntohs (servinfo-> s_port);/* Create a UDP socket. */sockfd = socket (af_inet, sock_dgram, 0);/* construct the address for use with sendto/recvfrom... */address. sin_family = af_inet; address. sin_port = servinfo-> s_port; address. sin_addr = * (struct in_addr *) * hostinfo-> h_addr_list; Len = sizeof (Address); Result = sendto (sockfd, buffer, 1, 0, (struct sockaddr *) & Address, Len); Result = recvfrom (sockfd, buffer, sizeof (buffer), 0, (struct sockaddr *) & Address, & Len ); buffer [Result] = '\ 0'; printf ("read % d Bytes: % s", result, buffer); close (sockfd); exit (0 );}