1. server socket 's life cycle:
1) creation a connection-style socket;
2) binding an address to its socket
3) placing a call to listen that enables connections to the socket
4)placting calls to acception incoming connections
5) then closing the socket
2. Client socket's life cycle
1)connecting to server socket
2)close socket
Examples:
1. Local Sockets
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
/**/ /* Read text from the socket and print it out. Continue until the
socket closes. Return nonzero if the client sent a “quit”
message, zero otherwise. */
int server (int client_socket)
... {
while (1) ...{
int length;
char* text;
/**//* First, read the length of the text message from the socket. If
read returns zero, the client closed the connection. */
if (read (client_socket, &length, sizeof (length)) == 0)
return 0;
/**//* Allocate a buffer to hold the text. */
text = (char*) malloc (length);
/**//* Read the text itself, and print it. */
read (client_socket, text, length);
printf ("%s ", text);
/**//* Free the buffer. */
// free (text);
if (!strcmp (text, "quit"))
...{
free(text);
return 1;
}
free(text);
}
}
int main (int argc, char* const argv[])
... {
const char* const socket_name = argv[1];
int socket_fd;
struct sockaddr_un name;
int client_sent_quit_message;
/**//* Create the socket. */
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
/**//* Indicate that this is a server. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
bind (socket_fd, &name, SUN_LEN(&name));
/**//* Listen for connections. */
listen (socket_fd, 5);
/**//* Repeatedly accept connections, spinning off one server() to deal
with each client. Continue until a client sends a “quit” message. */
do ...{
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
/**//* Accept a connection. */
client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
/**//* Handle the connection. */
client_sent_quit_message = server (client_socket_fd);
/**//* Close our end of the connection. */
close (client_socket_fd);
}
while (!client_sent_quit_message);
/**//* Remove the socket file. */
close (socket_fd);
unlink (socket_name);
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
/**/ /* Read text from the socket and print it out. Continue until the
socket closes. Return nonzero if the client sent a “quit”
message, zero otherwise. */
int server (int client_socket)
... {
while (1) ...{
int length;
char* text;
/**//* First, read the length of the text message from the socket. If
read returns zero, the client closed the connection. */
if (read (client_socket, &length, sizeof (length)) == 0)
return 0;
/**//* Allocate a buffer to hold the text. */
text = (char*) malloc (length);
/**//* Read the text itself, and print it. */
read (client_socket, text, length);
printf ("%s ", text);
/**//* Free the buffer. */
// free (text);
if (!strcmp (text, "quit"))
...{
free(text);
return 1;
}
free(text);
}
}
int main (int argc, char* const argv[])
... {
const char* const socket_name = argv[1];
int socket_fd;
struct sockaddr_un name;
int client_sent_quit_message;
/**//* Create the socket. */
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
/**//* Indicate that this is a server. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
bind (socket_fd, &name, SUN_LEN(&name));
/**//* Listen for connections. */
listen (socket_fd, 5);
/**//* Repeatedly accept connections, spinning off one server() to deal
with each client. Continue until a client sends a “quit” message. */
do ...{
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
/**//* Accept a connection. */
client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
/**//* Handle the connection. */
client_sent_quit_message = server (client_socket_fd);
/**//* Close our end of the connection. */
close (client_socket_fd);
}
while (!client_sent_quit_message);
/**//* Remove the socket file. */
close (socket_fd);
unlink (socket_name);
return 0;
}
2. Internet-Domain Socket
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
/**/ /* Print the contents of the home page for the server’s socket.
Return an indication of success. */
void get_home_page (int socket_fd)
... {
char buffer[10000];
ssize_t number_characters_read;
/**//* Send the HTTP GET command for the home page. */
sprintf (buffer, "Get/ /n");
write (socket_fd, buffer, strlen (buffer));
/**//* Read from the socket. The call to read may not
return all the data at one time, so keep
trying until we run out. */
while (1) ...{
number_characters_read = read (socket_fd, buffer, 10000);
if (number_characters_read == 0)
return;
/**//* Write the data to standard output. */
fwrite (buffer, sizeof (char), number_characters_read, stdout);
}
}
int main (int argc, char* const argv[])
... {
int socket_fd;
struct sockaddr_in name;
struct hostent* hostinfo;
/**//* Create the socket. */
socket_fd = socket (PF_INET, SOCK_STREAM, 0);
/**//* Store the server’s name in the socket address. */
name.sin_family = AF_INET;
/**//* Convert from strings to numbers. */
hostinfo = gethostbyname (argv[1]);
if (hostinfo == NULL)
return 1;
else
name.sin_addr = *((struct in_addr *) hostinfo->h_addr);
/**//* Web servers use port 80. */
name.sin_port = htons (80);
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
/**/ /* Print the contents of the home page for the server’s socket.
Return an indication of success. */
void get_home_page (int socket_fd)
... {
char buffer[10000];
ssize_t number_characters_read;
/**//* Send the HTTP GET command for the home page. */
sprintf (buffer, "Get/ /n");
write (socket_fd, buffer, strlen (buffer));
/**//* Read from the socket. The call to read may not
return all the data at one time, so keep
trying until we run out. */
while (1) ...{
number_characters_read = read (socket_fd, buffer, 10000);
if (number_characters_read == 0)
return;
/**//* Write the data to standard output. */
fwrite (buffer, sizeof (char), number_characters_read, stdout);
}
}
int main (int argc, char* const argv[])
... {
int socket_fd;
struct sockaddr_in name;
struct hostent* hostinfo;
/**//* Create the socket. */
socket_fd = socket (PF_INET, SOCK_STREAM, 0);
/**//* Store the server’s name in the socket address. */
name.sin_family = AF_INET;
/**//* Convert from strings to numbers. */
hostinfo = gethostbyname (argv[1]);
if (hostinfo == NULL)
return 1;
else
name.sin_addr = *((struct in_addr *) hostinfo->h_addr);
/**//* Web servers use port 80. */
name.sin_port = htons (80);