Vous êtes sur la page 1sur 6

/* A simple server in the internet domain using TCP The port number is passed as an argument */ #include <stdio.

h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(const char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr, "ERROR, no port provided\n"); exit(1); } // Create a TCP socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof (serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); // Assign a name/port number to socket if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) error("ERROR on binding"); // Establish a queue for connections listen(sockfd, 5); clilen = sizeof (cli_addr); // Extract a connection from the queue newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); bzero(buffer, 256); // Read/write n = read(newsockfd, buffer, 255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n", buffer); n = write(newsockfd, "I got your message", 18); if (n < 0) error("ERROR writing to socket"); close(newsockfd); close(sockfd); return 0; }

///////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// server.c C Syntax (Toggle Plain Text) #include <stdlib.h> #include <stdio.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> /* for sockaddr_un struct */ #define DEFAULT_PROTOCOL 0

/* POSIX renames "Unix domain" as "local IPC." Not all systems define AF_LOCAL and PF_LOCAL (yet). */ #ifndef AF_LOCAL #define AF_LOCAL AF_UNIX #endif #ifndef PF_LOCAL #define PF_LOCAL PF_UNIX #endif

/****************************************************************/ main () { int serverFd, clientFd, serverLen, clientLen; struct sockaddr_un serverAddress;/* Server address */ struct sockaddr_un clientAddress; /* Client address */ struct sockaddr* serverSockAddrPtr; /* Ptr to server address */

struct sockaddr* clientSockAddrPtr; /* Ptr to client address */

/* Ignore death-of-child signals to prevent zombies */ signal (SIGCHLD, SIG_IGN);

serverSockAddrPtr = (struct sockaddr*) &serverAddress; serverLen = sizeof (serverAddress);

clientSockAddrPtr = (struct sockaddr*) &clientAddress; clientLen = sizeof (clientAddress);

/* Create a socket, bidirectional, default protocol */ serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL); serverAddress.sun_family = AF_LOCAL; /* Set domain type */ strcpy (serverAddress.sun_path, "Countries"); /* Set name */ unlink ("Countries"); /* Remove file if it already exists */ bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */ listen (serverFd, 5); /* Maximum pending connection length */

while (1) /* Loop forever */ { /* Accept a client connection */ clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

if (fork () == 0) /* Create child to send countries */ { writeCountries (clientFd); /* Send the countries */ close (clientFd); /* Close the socket */ exit (/* EXIT_SUCCESS */ 0); /* Terminate */ }

else close (clientFd); /* Close the client descriptor */ } }

/****************************************************************/ client.c C Syntax (Toggle Plain Text) #include <stdlib.h> #include <stdio.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> /* for sockaddr_un struct*/ #define DEFAULT_PROTOCOL 0

/* POSIX renames "Unix domain" as "local IPC." Not all systems define AF_LOCAL and PF_LOCAL (yet). */ #ifndef AF_LOCAL #define AF_LOCAL AF_UNIX #endif #ifndef PF_LOCAL #define PF_LOCAL PF_UNIX #endif

/****************************************************************/ main () {

int clientFd, serverLen, result; struct sockaddr_un serverAddress; struct sockaddr* serverSockAddrPtr; serverSockAddrPtr = (struct sockaddr*) &serverAddress; serverLen = sizeof (serverAddress);

/* Create a socket, bidirectional, default protocol */ clientFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL); serverAddress.sun_family = AF_LOCAL; /* Server domain */ strcpy (serverAddress.sun_path, "country"); /* Server name */ do /* Loop until a connection is made with the server */ { result = connect (clientFd, serverSockAddrPtr, serverLen); if (result == -1) sleep (1); /* Wait and then try again */ } while (result == -1); readCountry (clientFd); /* Read the country */ close (clientFd); /* Close the socket */ exit (/* EXIT_SUCCESS */ 0); /* Done */ } /**************************************************************/

readCountries (fd) int fd; { char str[200];

while (readLine (fd, str)) /* Read lines until end-of-input */ printf ("%s\n", str); /* Echo line from socket */ }

/**************************************************************/

Vous aimerez peut-être aussi