///////////////////////////////////////////////////////// // Example #1: Simple Client ///////////////////////////////////////////////////////// #include #include #include #include #include #include void SendData(int fdout,char buf[],int size); int DisplayData(int fdin); int Init_Sock(); int Hastoberead(int fdin); void clean_Sock(); ///////////////////////////////////////////////////////// //#define DATA "Hier ben ik . . .\n" #define LOCALHOST "localhost" #define DATA "123456789" #ifndef MAXPATH /* schijnt niet overal te zijn gedefinieerd */ #define MAXPATH 1024 #endif struct timeval timeout; int main(void) { int sd; struct sockaddr_in sad; int cl; struct sockaddr_in clt; int cl_length; u_long answ; char hostname[MAXPATH]; int i=1,readselect; // fd_set read_sock,write_sock,bound_sock ; /*structure for select*/ // struct timeval timeout; struct hostent *hp ; /* beschrijft host ip e.d. */ /* only by winsock */ if (Init_Sock()!=0) { perror("Socket Init Failed "); return 1; } if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) // if((sd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("Socket object Failed Creation"); return 1; } /* weet niet wat doet is maar werkt hier niet !! if(setsockopt(sd,IPPROTO_TCP,TCP_NODELAY,(char *)&i,sizeof(i))<0) { perror("setSocketopt Failed "); return 1; } */ memset(&sad,0,sizeof(sad)); /* clear server adress structure */ sad.sin_family = AF_INET; /* dit zoekt naar een bestaande localhost niet bestaat vraagd het om de hostname (als die bestaat) en zoek daarvan het hostadres je kunt ook zelf een hostname geven dan hoeft dit gezoek niet */ /* if ((hp=gethostbyname(LOCALHOST))==NULL) { gethostname(hostname,MAXPATH); if((hp=gethostbyname(hostname))==NULL) { perror("getHostbyname Failed "); return 1; } } memcpy(&sad.sin_addr,hp->h_addr,hp->h_length); */ /* feitelijk is onderstaand een local host */ sad.sin_addr.s_addr = inet_addr("127.0.0.1"); // inet_addr local host /* feitelijk is onderstaand een server host */ // sad.sin_addr.s_addr = inet_addr("129.125.22.164"); // inet_addr host machine /* port server type = 6000 */ sad.sin_port = htons(6000); printf("Try to Connected to Server IP %s\n",inet_ntoa(sad.sin_addr)); if(connect(sd,(struct sockaddr *)&sad,sizeof(sad))==-1) { perror("Connection Failed"); return 1; } printf("Connected to Server IP %s\n",inet_ntoa(sad.sin_addr)); /* lijkt alleen informatief */ cl_length = sizeof(clt); if(getsockname(sd,(struct sockaddr *)&clt, &cl_length)==-1) { perror("getsockname Failed"); return 1; } printf("Client IP = %S\n",inet_ntoa(clt.sin_addr)); /*is leeg*/ printf("Client Port = %hu\n",ntohs(clt.sin_port)); /* einde info */ /* try select() */ /* FD_ZERO(&read_sock); FD_ZERO(&write_sock); FD_ZERO(&bound_sock); FD_SET(sd,&read_sock); FD_SET(sd,&write_sock); FD_SET(sd,&bound_sock); timeout.tv_sec=1; timeout.tv_usec=0; do { readselect = select(1,&read_sock,&write_sock,&bound_sock,&timeout); if (readselect<0) { perror("select Failed"); return 1; } if (readselect==0) printf("nothing to do wait\n"); } while(readselect<=0); printf("readsel %d ",readselect); printf("is set read %d ",FD_ISSET(sd,&read_sock)); printf("is set write %d ",FD_ISSET(sd,&write_sock)); printf("is set bound %d\n",FD_ISSET(sd,&bound_sock)); */ SendData(sd,DATA,sizeof(DATA)); for(;;){ readselect=Hastoberead(sd); if (readselect<0) {printf("connection clossed\n");break;} if (readselect>0) if(DisplayData(sd)<=0) break; else printf(" has to wait\n"); } printf("l call %d\n",Hastoberead(sd)); printf("l call %d\n",Hastoberead(sd)); printf("data = %d\n",DisplayData(sd)); printf("l call %d\n",Hastoberead(sd)); printf("l call %d\n",Hastoberead(sd)); printf("data = %d\n",DisplayData(sd)); closesocket(sd); printf("last call %d\n",Hastoberead(sd)); clean_Sock(); return 0; } ///////////////////////////////////////////////////////// int Hastoberead(int fdin) { int readselect; fd_set read_sock,write_sock,bound_sock ; /*structure for select*/ // struct timeval timeout; timeout.tv_sec=0; timeout.tv_usec=0; /* try select() */ FD_ZERO(&read_sock); FD_ZERO(&write_sock); FD_ZERO(&bound_sock); FD_SET(fdin,&read_sock); FD_SET(fdin,&write_sock); FD_SET(fdin,&bound_sock); readselect = select(FD_SETSIZE,&read_sock,&write_sock,&bound_sock,&timeout); if (readselect<0) return -1; if (readselect==0) return 0; printf("is set write %d ",FD_ISSET(fdin,&write_sock)); printf("is set bound %d ",FD_ISSET(fdin,&bound_sock)); printf("read %d\n",FD_ISSET(fdin,&read_sock)); return (int)FD_ISSET(fdin,&read_sock); } ///////////////////////////////////////////////////////// int DisplayData(int fdin) { char buf[50]; int ret; memset(&buf,0,sizeof(buf));/* terminate a char string */ // if((ret=read(fdin,buf,sizeof(buf)-1))<=0) { ret=recv(fdin,buf,sizeof(buf)-1,0); if(ret==0) printf("Rec EOF\n");/*closesocket()*/ else if (ret<0) printf("rec unable\n");/*no socket connect */ else write(1,buf,ret); return ret; } ///////////////////////////////////////////////////////// void SendData(int fdout,char buf[],int size) { int i,ret; for(i=0;i