⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat.c

📁 ipx协议聊天程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	      textbackground(0);
	      textcolor(0);
	      clreol(); /* display operation information && iron out cursor display */
	      normvideo();
	      active=1;
	      while((ky=in_key())!=KEY_ESC)       /* select a active window */
	       {
		  if(ky==KEY_TAB)
		      active=active==0 ?1 :0;
		  switch(active){
				  case 0:
					  receive_win(1,&receive_buf[0]);
					  break;
				  case 1:
					  send_win(1,&send_buf[0]);
					  break;
				}
	       }
	}

	       /*  file transfer program segment  */
   int FTspeed_scrollbar(int digit,unsigned long sum,int oldper_average)
	{
	   int j;             /* File send spend show ProgressgBar */
	   unsigned long i;
	   float count,newper_average;

	     if(sum%500)
		   count=sum/500+1;
	      else
		    count=sum/500;
	     newper_average=(100/count)*digit;
	     if(digit==count||newper_average>=100||oldper_average>=100)
		{
		  textbackground(3);
		  window(16,15,65,15);
		  clrscr();
		  highvideo();
		  textbackground(1);
		  textcolor(1);
		  window(16,15,65,15);
		  clrscr();
		  gotoxy(24,1);
		  textcolor(15);
		  cprintf("100%%");
		  normvideo();
		  return 100;
		}
	     for(j=oldper_average;j<=newper_average;j++)
		{
		   if(j==1){
			    textbackground(3);
			    textcolor(3);
			    window(39,15,42,15);
			    textcolor(0);
			    cprintf("1%%");
			   }
		   if(j>1&&j<=53)
			  {
			    textbackground(1);
			    textcolor(1);
			    if(j<=47)
			     {
			      window(16,15,j/2+15,15);
			      clrscr();
			     }
			    else
			     if(j>47){
				       window(39,15,40,15);
				       textcolor(15);
				       if(j==48||j==49)
					 cprintf("4");
				       if(j==50||j==51)
					{
					 window(39,15,41,15);
					 textcolor(15);
					 cprintf("%d",j);
					}
				       if(j==52||j==53)
					{
					  window(39,15,42,15);
					  textcolor(15);
					  cprintf("%d%%",j);
					}
				     }

			     textbackground(3);
			     textcolor(3);
			     if(j>47&&j<=53)
			      {
				window(40,15,42,15);
				textcolor(0);
				 if(j==48)
				  cprintf("8%%");
				 if(j==49)
				  cprintf("9%%");
				 if(j==50||j==51)
				  {
				   window(41,15,42,15);
				   textcolor(0);
				   cprintf("%%");
				  }
				 if(j==52||j==53)
				  {
				   textbackground(3);
				   textcolor(3);
				   window(42,15,43,15);
				   clrscr();
				  }
				 for(i=1;i<10000;i++);
			       }
			    else{
				 window(39,15,42,15);    /* display progress count && iron out cursor display */
				 highvideo();
				 textcolor(0);
				 cprintf("%d%%",j);
				 for(i=1;i<10000;i++);
				}
			  }
		     else if(j>53){
				    textbackground(1);
				    textcolor(1);
				    window(42,15,j/2+15,15);
				    clrscr();

				   if(j==100)
				      window(39,15,43,15);
				   else
				      window(39,15,42,15);
				    highvideo();
				    textcolor(15);
				    cprintf("%d%%",j);
				    normvideo();
				    for(i=1;i<10000;i++);
				  }
	       }
	    return newper_average;
	}

    void send_file()
	{
	  FILE *fp;
	  int ch,ch1,nbytes,digit,key1,key2,oldper_average;
	  char fname_buf[80],username_buf[43],key;
	  unsigned int my_socket,dest_connect_num,connection_id,i,delay;
	  unsigned long  ftotal;
	  unsigned char work_buffer[534],send_buffer[534];

	    key1=input_filename(&fname_buf[0]);
	    if(key1==0)
		    return;
	     else
		if((fp=fopen(fname_buf,"rb"))==NULL)
		     {
		       information_win(" ERROR: FILE NOT OPEN!");
		       getch();
		       return;
		     }
	    key2=input_username(&username_buf[0],1);
	    if(key2==0){
			fclose(fp);
			return;
		       }
	     else
	       if(Initialize()==0){
				   information_win(" ERROR: SPX NOT INSTALL.");
				   getch();
				   fclose(fp);
				   cls_screen4();
				   printf(" ERROR: spx not install.");
				   exit(0);
				  }
	     if((my_socket=OpenSocket(send_socket))==0)
				{
				  information_win(" ERROR: NOT SOCKET.");
				  getch();
				  fclose(fp);
				  cls_screen3();
				  printf(" ERROR: not socket. ");
				  exit(0);
				}
	     ListenForSequencePacket(&work_buffer[0],534,my_socket);
	     while((dest_connect_num=GetConnectionNum(&username_buf[0]))==0)
			       {
				  information_win(" ERROR: NOT LOGGED ON.");
				  key=getch();
				  if(key==27){
					      fclose(fp);
					      CloseSocket(my_socket);
					      cls_screen2();
					      printf(" ERROR: not logged on.");
					      exit(0);
					     }
				  input_username(&username_buf[0],1);
			       }
	     connection_id=EstablishConnection(dest_connect_num,my_socket);
	     while(call_ECB.in_use)
		 {
		   information_win("The file is connectioning.");
		   information_win("The file is connectioning...");
		   information_win("The file is connectioning......");
		   for(i=1;i<10000;i++) ;
		 }
	     if(call_ECB.completion_code!=0x00)
	      {
	       information_win("The Establish_connection routine is failure!");
	       getch();
	       fclose(fp);
	       CloseSocket(my_socket);
	       cls_screen1();
	       printf("The Establish_connection routine is failure!");
	       exit(0);
	      }
	     ftotal=0;
	     while(getc(fp)!=EOF) ftotal++;
	     if(ftotal<=0){
			    information_win(" This file is empty !");
			    getch();
			    fclose(fp);
			    CloseSocket(my_socket);
			    return ;
			  }
	     for(i=0;i<24&&fname_buf[i]!='\0';i++)
		       send_buffer[i]=fname_buf[i];
	      if(fname_buf[i]=='\0')
			 send_buffer[i]='\0';
	       else{
		     information_win(" Filename too long!");
		     getch();
		     fclose(fp);
		     CloseSocket(my_socket);
		     return ;
		   }
	     digit=0;          /* Initialize and Show file send ProgressBar */
	     oldper_average=1;
	     textbackground(7);
	     textcolor(7);
	     window(1,1,25,80);
	     clrscr();                 /* iron out original screen */

	     textbackground(0);    /* Create black shadow area */
	     window(17,13,68,19);
	     clrscr();
	     textbackground(14);  /* Create yellow background */
	     textcolor(0);
	     window(15,12,66,18);
	     clrscr();
	     draw_border(14,11,65,17);
	     textcolor(15);
	     gotoxy(3,2);
	     cprintf("File Transfer ProgressBar :");
	     textbackground(3);
	     textcolor(3);
	     window(16,15,65,15);
	     clrscr();    /* ProgressBar yellow background & white foreground */
	     textcolor(0);
	     gotoxy(24,1);
	     cprintf("0%%");
	     rewind(fp);
	     while(1) {
			for(i=25,nbytes=1;(nbytes<=500)&&(ch1=getc(fp))!=EOF;i++,nbytes++)
			   send_buffer[i]=ch1;
			if(feof(fp)){
				     send_buffer[i]='$';  /* create file end_flag */
				     send_buffer[i+1]='@';
				     send_buffer[i+2]='#';
				     send_buffer[i+3]='_';
				    }
			SendPacket(&send_buffer[0],534,connection_id);
			while(send_ECB.in_use){
					      information_win("The file is sending.");
					      information_win("The file is sending...");
					      information_win("The file is sending......");
					      for(i=1;i<20000;i++);
					       if(bioskey(1)!=0)
						 {
						   if(bioskey(0)==KEY_ESC)
						     {
						       fclose(fp);
						       CloseSocket(my_socket);
						       return ;
						     }
						 }

					      }
			for(delay=0;delay<4;delay++)
			   for(i=0;i<65535;i++);     /* delay send_file time */
			digit++;
			oldper_average=FTspeed_scrollbar(digit,ftotal,oldper_average);
			if(ch1==EOF)
			   break;
		      }
	     fclose(fp);
	     information_win(" Send file successful!");
	     getch();
	     TerminateConnection(connection_id);
	     while(term_ECB.in_use)
		      {
			information_win(" Terminate connectioning.");
			information_win(" Terminate connectioning...");
			information_win(" Terminate connectioning......");
			for(i=1;i<10000;i++);
		      }
	     CloseSocket(my_socket);
	     information_win(" File send over !");
       }

    void receive_file()
      {
	FILE *fp;
	int ch1;
	char fname_buf[80],ch;
	unsigned int my_socket,i;
	unsigned char receive_buffer[534];

	     if(Initialize()==0) {
				   information_win(" ERROR: SPX NOT INSTALL.");
				   getch();
				   cls_screen4();
				   printf(" ERROR: SPX NOT INSTALL.");
				   exit(0);
				 }
	     if((my_socket=OpenSocket(receive_socket))==0)
				{
				   information_win(" ERROR: NOT SOCKET.");
				   getch();
				   cls_screen3();
				   printf("\n ERROR: NOT SOCKET. \n\r");
				   exit(0);
				}
	     for(i=0;i<=4;i++)
		   ListenForPacket(&receive_buffer[0],534,my_socket,i);
	    ListenForConnection(my_socket);
	    while(listen_ECB.in_use)
		 {
		   information_win("The receive is listening.");
		   information_win("The receive is listening...");
		   information_win("The receive is listening.......");
		   for(i=1;i<10000;i++) ;
		   if(bioskey(1)!=0)
			 {
			   if(bioskey(0)==KEY_ESC){
						    CloseSocket(my_socket);
						    cls_screen2();
						    printf("\nThis ListenForConnection routine is failure!\n\r");
						    exit(0);
						   }
			 }
		 }
	    information_win("The file is receiving......");
	    ListenForPacket(&receive_buffer[0],534,my_socket,5);
	    while(receive_ECB_list[5].in_use)
	     {
	       if(bioskey(1)!=0){
				  if(bioskey(0)==KEY_ESC)
				   {
				     CloseSocket(my_socket);
				     return ;
				   }
				}
	     }
	    for(i=0;i<24&&receive_buffer[i]!='\0';i++)
		      fname_buf[i]=receive_buffer[i];
	    if(receive_buffer[i]=='\0')
		    fname_buf[i]='\0';
	     else{
		    information_win(" Filename too long!");
		    getch();
		    CloseSocket(my_socket);
		    return ;
		 }
	    if((fp=fopen(fname_buf,"wb"))==NULL)
	       {
		information_win(" ERROR: FILE NOT CREATE!");
		getch();
		CloseSocket(my_socket);
		return ;
	       }
	    information_win(" Writing to file......");
	    for(i=25;i<525;i++)
	       {
		if((ch1=receive_buffer[i])=='$'&&receive_buffer[i+1]=='@'&&receive_buffer[i+2]=='#'&&receive_buffer[i+3]=='_')
		  {
		    information_win(" Receive file successful!");
		    fclose(fp);
		    CloseSocket(my_socket);
		    getch();
		    return ;          /* received file end_flag with quit */
		  }
		 else
		    fputc(ch1,fp);   /* write data into file */
	       }
	    while(1){
		       fflush(fp);
		       information_win("The file is receiving......");
		       ListenForPacket(&receive_buffer[0],534,my_socket,5);
		       while(receive_ECB_list[5].in_use)
			      {
				if(bioskey(1)!=0){
						  if(bioskey(0)==KEY_ESC)
						    {
						      fclose(fp);
						      CloseSocket(my_socket);
						      return ;
						    }
						 }
			      }
		      information_win(" Writing to file......");
		      for(i=25;i<525;i++)
		       {
			if((ch1=receive_buffer[i])=='$'&&receive_buffer[i+1]=='@'&&receive_buffer[i+2]=='#'&&receive_buffer[i+3]=='_')
			   {
			       information_win(" Receive file successful!");
			       fclose(fp);
			       CloseSocket(my_socket);
			       getch();
			       return ;    /* received file end_flag with exit circle */
			   }
			  else
			       fputc(ch1,fp);  /* write data into file */
		       }
		    }
      }

   void file_T()
      {
	int key, curx1,curx2;
	char ch1,ch2;

	 while(1) {
		   textbackground(7);
		   window(1,1,80,25);
		   clrscr();
		   textbackground(0);
		   window(31,12,53,16);
		   clrscr();
		   textbackground(1);
		   textcolor(15);
		   window(29,11,51,15);      /* blue background & white foreground */
		   clrscr();
		   gotoxy(2,2);
		   cprintf(" Press ESC to exit");
		   lowvideo();
		   gotoxy(2,3);
		   cprintf("send file [Y/N]: ");
		   curx1=wherex();
		   gotoxy(13,3);
		   textcolor(14);
		   cprintf("Y/N");
		   gotoxy(2,4);
		   textcolor(WHITE);
		   lowvideo();
		   cprintf("receive file [Y/N]: ");
		   curx2=wherex();
		   gotoxy(16,4);
		   textcolor(14);
		   cprintf("Y/N");
		   normvideo();
		   while(1) {
			      gotoxy(curx1,3);
			      ch1=getch();
			      if(ch1==27)
					break;
			      if(ch1=='Y'||ch1=='y')
				     {
					send_file();
					break;
				     }
			      else   {
				       gotoxy(curx2,4);
				       ch2=getch();
				       if(ch2==27)
						 break;
				       if(ch2=='Y'||ch2=='y')
					      {
						 receive_file();
						 break;
					      }
				     }
			    }
		   if(ch1==27||ch2==27)
		      break;

		}
      }

  void PlaySound() /* Play two tager music  */
  {
    int i;

     for(i=0;i<32;i++)
     {
       sound(mFreq[i]);
       delay(mDelay[i]*80);
     }
     nosound();
  }

  /* Input connection computer login username */
   int input_username(char *buf1,int flags)
     {
       int ky;

       textbackground(12);          /* light red background & white foreground */
       textcolor(WHITE);
       window(1,24,80,25);
	gotoxy(2,1);
	cprintf("please input the receiver user_name[<43]:\n\r");
       if (flags==0){
		     textbackground(0);
		     window(1,25,7,25);
		     clrscr();
		     textbackground(15);
		     textcolor(0);
		     window(8,25,78,25);
		     clrscr();
		    }
	else {
	       textbackground(7);
	       window(1,25,7,25);
	       clrscr();
	       textbackground(2);
	       textcolor(0);
	       window(8,25,78,25);
	       clrscr();
	     }
       gotoxy(1,1);
       ky=win_gets(8,78,&buf1[0]);  /* accept string rule */
       clrscr();
       return ky;
     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -