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

📄 chat.c

📁 ipx协议聊天程序
💻 C
📖 第 1 页 / 共 4 页
字号:
			/* SPX Communication Program */
   # include <stdio.h>
   # include <process.h>
   # include <stdlib.h>
   # include <mem.h>
   # include <string.h>
   # include <dos.h>
   # include <bios.h>
   # include <conio.h>
   # include <graphics.h>
   # include "key.h"
   # include "spx.h"

   # define TRUE 1
   # define FALSE 0
   int mouse_installed=FALSE;

   int mDelay[32]=   /* Two tager music */
   {
     5,5,5,5,5,
     5,5,5,5,5,
     10,5,5,10,
     2,2,2,2,5,5,
     2,2,2,2,5,5,
     5,5,10,5,5,10
   };
   int mFreq[32]=
   {
     262,294,330,262,262,
     294,330,262,330,349,
     392,330,349,392,392,
     440,392,349,330,262,
     392,440,392,349,330,
     262,294,196,262,294,
     196,262
   };

  void scroll_screen(int startx,int starty,int endx,int endy,int k,int m);
  void cls_screen1();
  void cls_screen2();
  void cls_screen3();
  void cls_screen4();
  int in_key();
  int input_username(char *buf1,int flags);
  int input_filename(char *buf2);
  int cursor_upline(int curx,int cury);
  int cursor_downline(int curx,int cury,int starty,int endy);
  void cursor_backspace(int curx,int cury,int startx,int endx);
  void information_win(char *buf3);
  void goto_xy(int x,int y);
  void draw_border(int startx,int starty,int endx,int endy);
  void window_limit(int startx,int starty,int endx,int endy,int ch);
  void win_limit(int startx,int endx,int ch);
  int  window_gets(int startx,int starty,int endx,int endy,unsigned char *s);
  int  win_gets(int startx,int endx,unsigned char *s);
  SPXHEADER listen_header_list[5],receive_header_list[5],listen_header,send_header, call_header, term_header;
  SPXECB listen_ECB_list[5],receive_ECB_list[5],send_ECB,listen_ECB,call_ECB, term_ECB;

 /* SPX(Sepuence Packet eXchange) protocol schedule event of NOVELL network */
   unsigned int Initialize()
	   {
	     union REGS inregs,outregs;
		   inregs.x.bx=SPX_Initialize;
		   inregs.h.al=0;
		   int86(0x7a,&inregs,&outregs);
		   outregs.h.ah=0;
		   return outregs.x.ax;
	   }

   unsigned int OpenSocket(unsigned int socket)
	   {
	     union REGS inregs,outregs;
		   inregs.x.dx=socket;
		   inregs.x.bx=SPX_OpenSocket;
		   inregs.x.ax=0;
		   int86(0x7a,&inregs,&outregs);
		   return outregs.x.dx;
	   }

   void CloseSocket(unsigned int socket)
	   {
	     union REGS inregs,outregs;
	      inregs.x.dx=socket;
	      inregs.x.bx=SPX_CloseSocket;
	      inregs.x.ax=0;
	      int86(0x7a,&inregs,&outregs);
	   }

   void ListenForPacket(char *pointer, unsigned int length,unsigned int source_socket,unsigned int i)
	   {
	     union REGS inregs, outregs;
	     struct SREGS segregs;
	      memset( &receive_ECB_list[i],0,sizeof(SPXECB));
	      memset( &receive_header_list[i],0,sizeof(SPXHEADER));
	      receive_ECB_list[i].socket_number=source_socket;
	      receive_ECB_list[i].packet_count=2;
	      receive_ECB_list[i].packet[0].address=&receive_header_list[i];
	      receive_ECB_list[i].packet[0].length=sizeof(SPXHEADER);
	      receive_ECB_list[i].packet[1].address=pointer;
	      receive_ECB_list[i].packet[1].length=length;

	      segregs.es=FP_SEG((void far *)&receive_ECB_list[i]);
	      inregs.x.si=FP_OFF((void far *)&receive_ECB_list[i]);
	      inregs.x.bx=SPX_ListenForPacket;
	      int86x(0x7a,&inregs,&outregs,&segregs);
	   }

   void  ListenForConnection(unsigned int mysocket)
	   {
	     union REGS inregs,outregs;
	     struct SREGS segregs;
	       memset( &listen_ECB,0,sizeof(SPXECB));
	       memset( &listen_header,0,sizeof(SPXHEADER));
	       listen_ECB.socket_number=mysocket;
	       listen_ECB.packet_count=1;
	       listen_ECB.packet[0].address=&listen_header;
	       listen_ECB.packet[0].length=sizeof(SPXHEADER);

	       segregs.es=FP_SEG((void far *)&listen_ECB);
	       inregs.x.si=FP_OFF((void far *)&listen_ECB);
	       inregs.x.bx=SPX_ListenForConnection;
	       inregs.h.al=0x10;
	       inregs.h.ah=0;
	       int86x(0x7a,&inregs,&outregs,&segregs);
	   }

   unsigned int GetConnectionNum(char *user_name)
	   {
	     union REGS inregs,outregs;
	     struct SREGS segregs;
	     struct {
		     unsigned int length;
		     unsigned char buffer_type;
		     unsigned int object_type;
		     unsigned char name_len;
		     unsigned char name[47];
		    }request_buffer;
	     struct {
		     unsigned int length;
		     unsigned char number_connections;
		     unsigned char connection_num[100];
		    }reply_buffer;
	       request_buffer.length=51;
	       request_buffer.buffer_type=0x15;
	       request_buffer.object_type=0x100;
	       request_buffer.name_len=(unsigned char)strlen(user_name);
	       strcpy(request_buffer.name,user_name);
	       reply_buffer.length=101;

	       segregs.ds=FP_SEG((void far *)&request_buffer);
	       inregs.x.si=FP_OFF((void far *)&request_buffer);
	       segregs.es=FP_SEG(( void far *)&reply_buffer);
	       inregs.x.di=FP_OFF((void far *)&reply_buffer);
	       inregs.h.ah=0xe3;
	       int86x(0x21,&inregs,&outregs,&segregs);

	       if(outregs.h.al!=0) return 0;
	       if(reply_buffer.number_connections==0) return 0;
	       outregs.h.ah=0;
	       outregs.h.al=reply_buffer.connection_num[0];
	       return outregs.x.ax;
	   }

  unsigned int GetNetworkAddress(char connection_number,char *network_number,char *physical_address)
	   {
	     union REGS inregs,outregs;
	     struct SREGS segregs;
	     struct {
		       unsigned int length;
		       unsigned char buffer_type;
		       unsigned char connection_number;
		    }request_buffer;
	     struct{
		      unsigned int length;
		      unsigned char network_number[4];
		      unsigned char physical_address[6];
		      unsigned int server_socket;
		    }reply_buffer;
	      request_buffer.length=2;
	      request_buffer.buffer_type=0x13;
	      request_buffer.connection_number=connection_number;
	      reply_buffer.length=12;

	      segregs.ds=FP_SEG((void far *)&request_buffer);
	      inregs.x.si=FP_OFF((void far*)&request_buffer);
	      segregs.es=FP_SEG((void far *)&reply_buffer);
	      inregs.x.di=FP_OFF((void far *)&reply_buffer);
	      inregs.h.ah=0xe3;
	      int86x(0x21,&inregs,&outregs,&segregs);
	      memcpy(network_number,reply_buffer.network_number,4);
	      memcpy(physical_address,reply_buffer.physical_address,6);
	      outregs.h.ah=0;
	      return outregs.x.ax;
	   }

  void ListenForSequencePacket( char *pointer,unsigned int length,unsigned int source_socket)
	   {
	     unsigned int i;
	     union REGS inregs,outregs;
	     struct SREGS segregs;
	      for(i=0;i<5;i++) {
				 memset(&listen_ECB_list[i],0,sizeof(SPXECB));
				 memset(&listen_header_list[i],0,sizeof(SPXHEADER));
				 listen_ECB_list[i].socket_number=source_socket;
				 listen_ECB_list[i].packet_count=2;
				 listen_ECB_list[i].packet[0].address=&listen_header_list[i];
				 listen_ECB_list[i].packet[0].length=sizeof(SPXHEADER);
				 listen_ECB_list[i].packet[1].address=pointer;
				 listen_ECB_list[i].packet[1].length=length;
				 segregs.es=FP_SEG((void far *)&listen_ECB_list[i]);
				 inregs.x.si=FP_OFF((void far *)&listen_ECB_list[i]);
				 inregs.x.bx=SPX_ListenForPacket;
				 int86x(0x7a,&inregs,&outregs,&segregs);
			       }
	   }

  unsigned int EstablishConnection(unsigned int dest_connect_num,unsigned int source_socket)
	   {
	      union REGS inregs ,outregs;
	      struct SREGS segregs;
	      char network_number[4],physical_node[6];
	       memset(&call_ECB,0,sizeof(SPXECB));
	       memset(&call_header,0,sizeof(SPXHEADER));
	       GetNetworkAddress(dest_connect_num,&network_number[0],&physical_node[0]);
	       memcpy(call_header.dest_network_number,network_number,4);
	       memcpy(call_header.dest_network_node,physical_node,6);
	       call_header.dest_network_socket=receive_socket;
	       call_ECB.socket_number=source_socket;
	       call_ECB.packet_count=1;
	       call_ECB.packet[0].address=&call_header;
	       call_ECB.packet[0].length=sizeof(SPXHEADER);
	       segregs.es=FP_SEG((void far *)&call_ECB);
	       inregs.x.si=FP_OFF((void far *)&call_ECB);
	       inregs.x.bx=SPX_EstablishConnection;
	       inregs.h.ah=0;
	       inregs.h.al=0x5;
	       int86x(0x7a,&inregs,&outregs,&segregs);
	       return outregs.x.dx;
	    }

    void SendPacket(char *pointer,unsigned int length,unsigned int connection_id)
	   {
	       union REGS inregs,outregs;
	       struct SREGS segregs;
		memset(&send_ECB,0,sizeof(SPXECB));
		memset(&send_header,0,sizeof(SPXHEADER));
		send_ECB.packet_count=2;
		send_ECB.packet[0].address=&send_header;
		send_ECB.packet[0].length=sizeof(SPXHEADER);
		send_ECB.packet[1].address=pointer;
		send_ECB.packet[1].length=length;

		segregs.es=FP_SEG((void far *)&send_ECB);
		inregs.x.si=FP_OFF((void far *)&send_ECB);
		inregs.x.bx=SPX_SendPacket;
		inregs.x.dx=connection_id;
		int86x(0x7a,&inregs,&outregs,&segregs);
	  }

     void TerminateConnection(unsigned int connection_id)
	  {
	       union REGS inregs,outregs;
	       struct SREGS segregs;
	       unsigned int i;
		memset(&term_ECB,0,sizeof(SPXECB));
		send_ECB.packet_count=1;
		send_ECB.packet[0].address=&term_header;
		send_ECB.packet[0].length=sizeof(SPXHEADER);

		segregs.es=FP_SEG((void far *)&term_ECB);
		inregs.x.si=FP_OFF((void far *)&term_ECB);
		inregs.x.bx=SPX_TerminateConnection;
		inregs.x.dx=connection_id;
		int86x(0x7a,&inregs,&outregs,&segregs);
	   }

	      /*  Relay chat  program segment  */
     int receive_chat(unsigned char buf[3][BUFSIZE])
	   {
	     unsigned int my_socket,i,page,ch; /* Receive chat string function */

	       if(Initialize()==0){
				    information_win(" ERROR: SPX NOT INSTALL.");
				    getch();
				    cls_screen2();
				    printf(" ERROR:SPX NOT INSTALL.");
				    exit(0);
				   }
	       if((my_socket=OpenSocket(receive_socket))==0)
				   {
				      information_win(" ERROR: NOT SOCKET. ");
				      getch();
				      cls_screen1();
				      printf(" ERROR: NOT SOCKET. ");
				      exit(0);
				   }
		for(i=0;i<4;i++)
		      ListenForPacket(&buf[0][0],534,my_socket,i);
		ListenForConnection(my_socket);
		while(listen_ECB.in_use)
		     {
		       information_win("The chat is listening.");
		       information_win("The chat is listening...");
		       information_win("The chat is listening......");
		       for(i=1;i<20000;i++) ;
		       if(bioskey(1)!=0){
					 if(bioskey(0)==KEY_ESC)
					   {
					     CloseSocket(my_socket);
					     return 0;
					   }
					}
		     }
		page=0;
		while(1) {
			   ListenForPacket(&buf[page][0],534,my_socket,5);
			   while(receive_ECB_list[5].in_use)
				{
				   information_win("The chat is receiving.");
				   information_win("The chat is receiving...");
				   information_win("The chat is receiving.......");
				   for(i=1;i<10000;i++);
				   if(bioskey(1)!=0){
						      if(bioskey(0)==KEY_ESC)
							{
							  CloseSocket(my_socket);
							  return 0;
							}
						     }
				 }
			    for(ch=0;buf[page][ch]!='\0'&&ch<534;ch++);
			    if(ch<534)
				 break;
			    else
				 page++;
			  }
		if(receive_ECB_list[5].completion_code==0x00)
		       information_win("This receive is successful!");
		 else
		       information_win("This receive is failure!");
		CloseSocket(my_socket);
		return 1;
	   }

       /* Send chat string function */
    void send_chat(unsigned char buf[3][BUFSIZE])
	   {
	       unsigned  int my_socket,dest_connect_num,connection_id,page,ch,i;
	       unsigned  char work_buffer[534];
	       char username_buf[43],key1;
	       int key;
		 key=input_username(&username_buf[0],0);
		 if(key==0)
			 return;
		  else
		    if(Initialize()==0){
					information_win(" ERROR: SPX NOT INSTALL.");
					getch();
					cls_screen4();
					printf("ERROR: SPX NOT INSTALL.");
					exit(0);
				       }
		  if((my_socket=OpenSocket(send_socket))==0)
				     {
					information_win(" ERROR: NOT SOCKET.");
					getch();
					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.");
				      key1=getch();
				      if(key1==27){
						   cls_screen2();
						   printf("ERROR: not logged on.");
						   CloseSocket(my_socket);
						   exit(0);
						  }
				      input_username(&username_buf[0],0);
				     }
		    connection_id=EstablishConnection(dest_connect_num,my_socket);
		    while(call_ECB.in_use)
			 {
			   information_win("The chat is connectioning.");
			   information_win("The chat is connectioning...");
			   information_win("The chat is connectioning......");
			   for(i=1;i<10000;i++) ;
			 }
		    if( call_ECB.completion_code!=0x00)
		     {
		      information_win("The Establish_connection routine is failure!");
		      getch();
		      CloseSocket(my_socket);
		      cls_screen1();
		      printf("The Establish_connection routine is failure!");
		      exit(0);
		     }
		     page=0;
		     while(1)
			  {
			    for(ch=0;buf[page][ch]!='\0'&& ch<534;ch++);
			    if(ch<534){
				       SendPacket(&buf[page][0],534,connection_id);
				       while(send_ECB.in_use)
					 {
					   information_win("The chat is sending.");
					   information_win("The chat is sending...");
					   information_win("The chat is sending.......");
					   for(i=1;i<18000;i++) ;
					 }
					break;
				      }
			     else  {
				     SendPacket(&buf[page][0],534,connection_id);
				     while(send_ECB.in_use){
							   information_win("The chat is sending.");
							   information_win("The chat is sending...");
							   information_win("The chat is sending......");
							   for(i=1;i<18000;i++) ;
							   if(bioskey(1)!=0)
							    {
							     if(bioskey(0)==KEY_ESC)
							      {
							       CloseSocket(my_socket);
							       return;
							      }
							    }
							   }
				     page++;
				   }
			  }
		     information_win(" The send is successful!");
		     TerminateConnection(connection_id);
		      while(term_ECB.in_use);
		     CloseSocket(my_socket);
	    }

      /* Send chat string message window */
    void send_win(int active,unsigned char buf[3][BUFSIZE])
	   {
	     int key;
	     if(active==1) {
			     textbackground(5);
			     textcolor(15);
			     window(4,3,77,12);
			     draw_border(3,3,76,12);
			     gotoxy(31,2);
			     cprintf(" SEND MESSAGE \r\n");
			     textbackground(1);
			     textcolor(15);      /* blue background & white foreground */
			     window(5,5,76,12);
			     clrscr();
			     key=window_gets(5,5,76,12,&buf[0][0]); /* Send window accept string rule */
			     if(key==0)
				  {
				    clrscr();
				    return;
				  }
			       else{
				    send_chat(&buf[0]);
				   }
			  }
	    }

       /* Receive chat string message window */
    void receive_win(int active,unsigned char buf[3][BUFSIZE])
	    {
	      unsigned char *s;
	      int key;
	      if(active==1){    /* Action flag equal 1 is show receive message window */
			      textbackground(5);
			      textcolor(15);
			      window(4,13,77,22);
			      draw_border(3,13,76,22);
			      gotoxy(29,2);
			       cprintf(" RECEIVE  MESSAGE \r\n");
			      textbackground(1);
			      textcolor(15);
			      window(5,15,76,22);
			      clrscr();           /* blue background & white foreground */
			      key=receive_chat(&buf[0]);
			      if(key==0)
				 return;
			      textbackground(1);
			      textcolor(15);
			      window(5,15,76,22);
			      clrscr();
			      gotoxy(1,1);
			      s=&buf[0][0];
			      while(1){
				       if(*s=='\n')
					{
					  if(wherey()==8){
							   s++;
							   gotoxy(72,wherey());
							 }
					    else{
						  cputs("\n");
						  s++;
						  gotoxy(1,wherey());
						}
					}
					 else{
					      if(wherey()==8 && wherex()==72)
						{
						  getch();
						  cprintf(" ");
						}
					      cprintf("%c",*s);
					      s++;
					     }
				       if(*s=='\0')
					  break;
				      }
			   }
	   }

    void relay_chat()
	{  int active,ky;
	   unsigned char send_buf[3][BUFSIZE],receive_buf[3][BUFSIZE];
	      cls_screen1();
	      highvideo();
	      textattr(WHITE+(GREEN<<4)+BLINK); /* green background & white foreground */
	      window(1,1,80,2);
	      gotoxy(3,1);
	      cprintf("Press TAB key to [ send window <--> receive window ] with Press ESC to exit");
	      gotoxy(17,2);
	      cprintf("Press '@' key to end && send_message to start!");

⌨️ 快捷键说明

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