00001
00090 #include <arpa/inet.h>
00091 #include <cstdio>
00092 #include <cstdlib>
00093 #include <cstring>
00094 #include <errno.h>
00095 #include <fcntl.h>
00096 #include <map>
00097 #include <netdb.h>
00098 #include <netinet/in.h>
00099 #include <string>
00100 #include <sys/socket.h>
00101 #include <sys/stat.h>
00102 #include <sys/types.h>
00103 #include <unistd.h>
00104 #include "ZSO-SPNFS_hooks.hpp"
00105 #include "ZSO-SPNFS_network.hpp"
00106
00107 void service_client(int client_socket);
00108
00109 int main (int argc, char **argv) {
00110 int sock;
00111 int status;
00112 int i;
00113 struct sockaddr_in clientname;
00114 socklen_t socklen;
00115 size_t size;
00116 int portnum;
00117
00118 if(argc<=1){
00119 printf("Server usage:\n%s port\n",
00120 "\n"
00121 "When server runs, you can use client, telnet and webbrowser as well\n",
00122 argv[0]);
00123 return 0;
00124 };
00125
00126
00127
00128 portnum = atoi(argv[1]);
00129 printf("Port = %d\n", portnum);
00130
00131
00132 sock = make_socket (portnum);
00133 if (listen (sock, 1) < 0) {
00134 perror ("listen");
00135 exit (EXIT_FAILURE);
00136 }
00137
00138 set_TCP_NODELAY(sock);
00139
00140
00141 while (1) {
00142 if ( (status = accept (sock, (struct sockaddr *) &clientname, &socklen)) < 0) {
00143 perror ("accept");
00144 exit (EXIT_FAILURE);
00145 }
00146
00147 fprintf (stderr, "Server: connect from host %s, port %hd.\n", inet_ntoa (clientname.sin_addr), ntohs (clientname.sin_port));
00148
00149 if( fork()==0 ){
00150 service_client(status);
00151 return 0;
00152 };
00153
00154 close(status);
00155 };
00156 return 0;
00157 }
00158
00159 int logining(int);
00160
00162 void service_client(int client_socket){
00163 char ch;
00164 hooks_container hc;
00165
00166 if(logining(client_socket)<0){
00167 write(client_socket,make_csv_string(-1)+make_csv_string("login failed"));
00168 close(client_socket);
00169 return;
00170 };
00171
00172 init_hooks(hc);
00173
00174
00175 while(1){
00176
00177 hc << client_socket;
00178 write(client_socket, "\n- - - - - - - - - - - - -\n");
00179 };
00180 close(client_socket);
00181 };
00182
00183 int logining(int fd){
00184 if(read_csv_string(fd)=="login" &&
00185 read_csv_string(fd)=="ala" &&
00186 read_csv_string(fd)=="kota" ){
00187 write(fd,make_csv_string(0)+make_csv_string("logged in successfully"));
00188 return 0;
00189 };
00190 return -1;
00191 };
00192