///////////////////////////////////////////////////////// // Example #2: Simple Server ///////////////////////////////////////////////////////// #include #include #include #include #include #include void SendData(int fdout,char buf[],int size); void DisplayData(int fdin); void HTTP_header(int fdin); int Init_Sock(); char eofsign = EOF; ///////////////////////////////////////////////////////// int main(void) { int i,sd,sad_length; struct sockaddr_in sad; long int answ; /* alleen by winsock */ if (Init_Sock()!=0) { perror("Socket Init Failed "); return 1; } // sd=socket(AF_INET,SOCK_STREAM,0); sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(sd==-1) { perror("Socket object Failed Creation"); return 1; } // Initialize sockaddr_in structure sad_length = sizeof(sad); memset(&sad,0,sad_length); sad.sin_family = AF_INET; // sad.sin_addr.s_addr = inet_addr("129.125.22.164"); sad.sin_addr.s_addr = htonl(INADDR_ANY); // sad.sin_port = htons(8000); sad.sin_port = htons(6000); if(bind(sd,(struct sockaddr *)&sad,sad_length )==-1) { perror("Unable to bind address"); return 0; } /* doet hier niets if (getsockname(sd,(struct sockaddr *)&sad,&sad_length )==-1) { perror("Unable to getsockname"); return 0; } */ listen(sd,10); // Queue at most 10 connections printf("Server started\n"); printf("Server IP %s\n",inet_ntoa(sad.sin_addr)); printf("Server Port: %d\n",(int)ntohs(sad.sin_port) ); for(;;) { struct sockaddr_in cliaddr; int size=sizeof(cliaddr); int clisd; if((clisd=accept(sd,(struct sockaddr *)&cliaddr,&size))==-1) { continue; } // Client Has Connected // communicate using a clisd printf("Remote Port: %d\n",(int)ntohs(cliaddr.sin_port) ); printf("Remote IP : %s\n",inet_ntoa(cliaddr.sin_addr) ); // Communicate with client /* wacht tot er een buffer gearriveerd is */ do {if(ioctlsocket(clisd,FIONREAD,&answ)!=0) { perror("ioctsocket Failed"); return 1; } // printf(" length = %d\n",answ); }while(answ==0); DisplayData(clisd); printf("Send file\n"); SendData(clisd,"HTTP/1.1 200 OK\r\n\r\n",19); // SendData(clisd,"Content-Type: text/plain\r\n\r\n",28); SendData(clisd,"\n",7); SendData(clisd,"\n",7); SendData(clisd," Janto \n",23); SendData(clisd,"\n",8); SendData(clisd,"\n",7); SendData(clisd,"Hier is Menno kompjuter\n",24); SendData(clisd,"
",4); SendData(clisd,"
",37); SendData(clisd,"Password: ",46); SendData(clisd,"
",4); SendData(clisd,"
",4); SendData(clisd,"Send me more info.",74); SendData(clisd,"

Hier is Menno kompjuter

\n",35); SendData(clisd,"
",4); SendData(clisd,"",41); SendData(clisd,"
",7); SendData(clisd,"\n",8); SendData(clisd,"\r\n",9); /* unix maybe linux this is close() */ closesocket(clisd); printf("Socket closed\n"); } closesocket(sd); /* close server socket */ return 0; } ///////////////////////////////////////////////////////// void DisplayData(int fdin) { char buf[10]; int ret;long int answ; do { /* winsock uses recv() else read() */ if(ioctlsocket(fdin,FIONREAD,&answ)!=0) { perror("ioctsocket Failed"); abort(1); } // printf("\ndata length = %d\n",answ); /* winsock gebruik recv() inplaats van read() */ // memset(&buf,0,sizeof(buf));/* terminate a char string */ /*prevent from blocking */ if(answ==0){ printf("Recieve EOF by prevent blocking\n"); break;} // if((ret=read(fdin,buf,100))<=0) if((ret=recv(fdin,buf,sizeof(buf),0))<=0) { if(ret==0) printf("Server recieved EOF\n"); else printf("Server Unable to recieve\n"); break; } write(1,buf,ret); } while(ret==sizeof(buf));/* het hangt als laaste buffer evenlang buf is en geen nadere informatie meer komt */ printf("End Reading\n"); } ///////////////////////////////////////////////////////// void SendData(int fdout,char buf[],int size) { int i,ret; for(i=0;i