src/vfer_test_server.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 2005, 2006, Internet2
00003  * Legal conditions are in file LICENSE
00004  * (MD5 = c434f2e53b8089d8b4d0172c7ce07360).
00005  */
00006 /**
00007  * @file   vfer_test_server.c
00008  * @author Ivan Beschastnikh
00009  * @brief  Test: A listening, always accepting server
00010  *
00011  * This test tests the server.c functionality and the associated api
00012  * in api.c to set up a listening socket, start listening on the
00013  * socket and accept incoming connections over that socket. This
00014  * server is a daemon, so it never stops listening and it never closes
00015  * any of its accepted or listening [one] sockets. It does not read
00016  * from the sockets.
00017  *
00018  * -     07/30/05        ivan            Added to the CVS tree (old test_server.c retired)
00019  */
00020 
00021 #include <stdio.h>
00022 #include "vfer_tests.h"
00023 #include "vfer_api.h"
00024 
00025 
00026 vfer_fd listen_skt;
00027 int port;
00028 
00029 
00030 int main(int argc, char** argv) {
00031         struct sockaddr_in sa;
00032         struct sockaddr_in sa_accepted;
00033         socklen_t accepted_len;
00034         vfer_fd skt_accepted;
00035         int port = 2005;
00036         char buf[INET_ADDRSTRLEN];
00037         int ret;
00038 
00039         if (argc != 2) {
00040                 printf("usage: %s [port]\n", argv[0]);
00041                 return -1;
00042         }
00043 
00044         TEST_INIT("TEST_SERVER", "test_server.c", stdout, stdout);
00045         vfer_debug(stdout, stdout, "scap");
00046 
00047         port = atoi(argv[1]);
00048 
00049         bzero((char*)(&sa), sizeof(sa));
00050         sa.sin_family = AF_INET;
00051         sa.sin_addr.s_addr = htonl(INADDR_ANY);
00052         sa.sin_port = htons(port);
00053 
00054         listen_skt = vfer_socket (SOCK_DGRAM);
00055         if (listen_skt < 0) {
00056                 TEST_PRINT("main", "vfer_socket failed");
00057                 return 1;
00058         }
00059 
00060         if ((ret = vfer_bind  (listen_skt, (struct sockaddr*) &sa, sizeof(sa))) < 0) {
00061                 TEST_PRINT("main", "vfer_bind failed");
00062                 vfer_errortext(ret);
00063                 return 1;
00064         }
00065         if ((ret = vfer_listen (listen_skt, 3)) < 0) {
00066                 TEST_PRINT("main", "vfer_listen failed");
00067                 TEST_PRINT("main", "%s", vfer_errortext(ret));
00068                 return 1;
00069         }
00070 
00071         while (1) {
00072                 if ((skt_accepted = vfer_accept(listen_skt, (SA*)(&sa_accepted), &accepted_len)) < 0) {
00073                         TEST_PRINT("main", "test_server2.c: vfer_accept failed");
00074                         vfer_errortext(skt_accepted);
00075                 } else {
00076                         TEST_PRINT("main", "vfer_accept successfull");
00077                         TEST_PRINT("main", "accepted new connection from: %s.%d",
00078                                inet_ntop(AF_INET, &(sa_accepted.sin_addr),buf, INET_ADDRSTRLEN),
00079                                ntohs(sa_accepted.sin_port));
00080                 }
00081         }
00082 
00083         vfer_close(listen_skt);
00084         return 0;
00085 } /* main() */

Generated on Tue Aug 8 16:07:19 2006 for VFER by  doxygen 1.4.7