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

📄 connect.c

📁 网络打印机及文件共享 C程序源代码 用于网络通信
💻 C
字号:
# include <stdio.h>
# include <dos.h>

 union  REGS    ipregs;
 union  REGS    opregs;
 struct SREGS   spregs;

struct DoubleWord
{
   unsigned int HiWord;
   unsigned int LoWord;
};
union  LongToWord
{
   long    dw;
   struct  DoubleWord w;
};


int  IntSwap();
long LongSwap();
int AttachToFileServer();
int DetachFromFileServer();
int EnterLoginArea();
int GetConnectionInformation();
int GetConnectionNumber();
int GetInternetAddress();
int GetObjectConnectionNumbers();
int LoginToFileServer();
void logout();
void LogoutFromFileServer();

void main()
{char object_name[48];
 unsigned long objectID;
 int object_type,return_code,i;
 int connectionID;
 char password[128],loginTime[7],ch;
 int  numberOfConnections,connectionNumber;
 char connectionList[100];
 char networkNumber[4];
 char physicalNodeAddress[6];
 char socketNumber[2];

 clrscr();
 printf("\n\n\n                      The example for connection DOS call\n\n");

 printf("\n\n\n1.Login to file server \n\n");
 printf("Please input the following datas: ");
 printf("\n\nobject_name:");
 scanf("%s",object_name);
 printf("\nobject_type(1-user/2-group):");
 scanf("%d",&object_type);
 printf("\npassword:");
 scanf("%s",password);
 return_code=LoginToFileServer(object_name, object_type, password);
 if (return_code==0)
	printf("\nAlready login to file server !");
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n2.Attach to another file server \n\n");
 printf("Please input the following datas: ");
 printf("\n\nconnectionID(2-8):");
 scanf("%d",&connectionID);
 return_code=AttachToFileServer(connectionID);
 if ((return_code==0)||(return_code==0xF8))
    printf("\nAlready attach to server %d !",connectionID);
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n3.Get connection number\n\n");
 connectionNumber=GetConnectionNumber();
 printf("The connecttion number of this station is %d.",connectionNumber);
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n4.Get object connection number\n\n");
 printf("Please input the following datas: ");
 printf("\n\nobject_name:");
 scanf("%s",object_name);
 printf("\nobject_type(1-user/2-group):");
 scanf("%d",&object_type);
 return_code=GetObjectConnectionNumbers(object_name, object_type, &numberOfConnections,
                               connectionList);
 if (return_code==0)
    {printf("\nThe connecttion number is :");
     for(i=0;i<numberOfConnections;i++)
	 printf("   %d",connectionList[i]);
    }
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n5.Get connection information\n\n");
 printf("Please input the following datas: ");
 printf("\n\nconnection_number:");
 scanf("%d",&connectionNumber);
 return_code=GetConnectionInformation(connectionNumber, object_name,
					&object_type,&objectID, loginTime);
 if (return_code==0)
    {printf("\nThe connecttion information : ");
     printf("\n\nobject_name:%s",object_name);
     printf("\nobject_type(1-user/2-group):%d",object_type);
     printf("\nobjectID:%lx",objectID);
     printf("\nlogintime:");
     printf("%d.%d.%d  %d:%d:%d",loginTime[0],loginTime[1],loginTime[2],
	    loginTime[3],loginTime[4],loginTime[5]);
     }
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n 6.Get internet address\n\n");
 printf("Please input the following datas: ");
 printf("\n\nconnection_number:");
 scanf("%d",&connectionNumber);
 return_code=GetInternetAddress(connectionNumber, networkNumber, physicalNodeAddress,
		       socketNumber);
 if (return_code==0)
    {printf("\n\nThe network number:");
     for(i=0;i<4;i++)
     printf("%x",networkNumber[i]);
     printf("\nThe node address:");
     for(i=0;i<6;i++)
     printf("%x",physicalNodeAddress[i]);
     printf("\nThe socket number:");
     for(i=0;i<2;i++)
     printf("%x",socketNumber[i]);
    }
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n7.logout from one file server \n\n");
 printf("Please input the following datas: ");
 printf("\n\nconnectionID(1-8):");
 scanf("%d",&connectionID);
 LogoutFromFileServer(connectionID);
 printf("\nAlready logout from file server %d!",connectionID);
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n8.detach from one file server \n\n");
 printf("Please input the following datas: ");
 printf("\n\nconnectionID(2-8):");
 scanf("%D",&connectionID);
 return_code=DetachFromFileServer(connectionID);
 if (return_code==0)
    printf("\nAlready detach from file server %d!",connectionID);
 printf("\n\nPress any key to continue!");
 ch=getch();

 printf("\n\n\n9.Logout from all file server \n\n");
 logout();
 printf("\nAlready logout from all file server! \n");
 printf("\n\nThis is the end of the example,press any key to quit!");
 ch=getch();

}

int  IntSwap(unsigned int  SwapInt)
{

   unsigned int mask1,mask2;

   mask1 = SwapInt << 8;
   mask2 = SwapInt >> 8;
   return (mask1 | mask2);

}

long LongSwap(long SwapLong)
{

   unsigned int mask1,mask2;
   union LongToWord tmp;

   tmp.dw = SwapLong;
   mask1 = IntSwap(tmp.w.HiWord);
   mask2 = IntSwap(tmp.w.LoWord);
   tmp.w.HiWord = mask1;
   tmp.w.LoWord = mask2;
   return (long)(tmp.dw);
}

int AttachToFileServer (connectionID)
int connectionID;
{
   ipregs.h.ah = 0xF1;
   ipregs.h.al = 0x00;
   ipregs.h.dl = (char)connectionID;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int DetachFromFileServer(connectionID)
int  connectionID;
{

   ipregs.h.ah = 0xF1;
   ipregs.h.al = 0x01;
   ipregs.h.dl = (char)connectionID;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int EnterLoginArea(loginSubdirectoryName, numberOfLocalDrives)
char *loginSubdirectoryName;
int numberOfLocalDrives;
{

   char  requestBuf[260];
   char  replyBuf[2];

   requestBuf[2] = 10;
   requestBuf[3] = (char)numberOfLocalDrives;

   if (loginSubdirectoryName != (char *)0)
   {
      requestBuf[4] = (char)((char)strlen(loginSubdirectoryName));
      strcpy(requestBuf + 5, loginSubdirectoryName);
   }
   else
      requestBuf[4] = 0;

   *((int *)requestBuf) = 3 + *(char *)(requestBuf + 4);
   *((int *)replyBuf) = 0;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE3;
   intdosx(&ipregs,&opregs,&spregs);

   return (opregs.h.al);
}

int GetConnectionInformation(connectionNumber, objectName, objectType,
                             objectID, loginTime)
int  connectionNumber;
char *objectName;
int  *objectType;
long *objectID;
char *loginTime;
{
   char requestBuf[4], replyBuf[63];
   int ccode;

   requestBuf[2] = 0x16;
   requestBuf[3] = (char)connectionNumber;
   *((int *)requestBuf) = 2;

   *((int *)replyBuf) = 0x61;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE3;
   intdosx(&ipregs,&opregs,&spregs);

   if (opregs.h.al)
      return(opregs.h.al);

   if (objectID != (long *)0)
      *objectID = LongSwap(*((long *)(replyBuf + 2)));
   if (objectType != (int *)0)
      *objectType = IntSwap(*((int *)(replyBuf + 6)));
   if (objectName != (char *)0)
      strcpy(objectName, replyBuf + 8);
   if (loginTime != (char *)0)
      memmove(loginTime, replyBuf + 56, (int)7);

   return(0);
}

int GetConnectionNumber()
{

   ipregs.h.ah = 0xDC;
   intdosx(&ipregs,&opregs,&spregs);

   return(opregs.h.al);
}

int GetInternetAddress(connectionNumber, networkNumber, physicalNodeAddress,
                       socketNumber)

int  connectionNumber;
char *networkNumber;
char *physicalNodeAddress;
char *socketNumber;
{
   char    requestBuf[4], replyBuf[14];

   requestBuf[2] = 0x13;
   requestBuf[3] = (char)connectionNumber;
   *((int *)requestBuf) = 2;
   *((int *)replyBuf) = 12;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE3;
   intdosx(&ipregs,&opregs,&spregs);

   if (opregs.h.al)
      return(opregs.h.al);

   memmove(networkNumber, replyBuf + 2, 4);
   memmove(physicalNodeAddress, replyBuf + 6, 6);
   memmove(socketNumber, replyBuf + 12, 2);

   return (0);
}

int GetObjectConnectionNumbers(objectName, objectType, numberOfConnections,
                               connectionList)
char *objectName;
int   objectType;
int  *numberOfConnections;
char *connectionList;
{
   char requestBuf[54], replyBuf[103];
   int offset;

   requestBuf[2] = 0x15;
   *((int *)(requestBuf + 3)) = IntSwap(objectType);
   requestBuf[5] = (char)strlen(objectName);
   strcpy(requestBuf + 6, objectName);
   *((int *)requestBuf) = 4 + requestBuf[5];
   *((int *)replyBuf) = 101;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE3;
   intdosx(&ipregs,&opregs,&spregs);

   if (opregs.h.al)
      return(opregs.h.al);

   *numberOfConnections = replyBuf[2];

   if (*numberOfConnections)
      for (offset = -1; ++offset < *numberOfConnections;
          connectionList[offset] = replyBuf     [3+offset]);

   return (0);
}
int LoginToFileServer(objectName, objectType, password)
char *objectName;
int  objectType;
char *password;
{

   char requestBuf[181], replyBuf[2];
   int ccode, objLength, paswdLength, offset;

   requestBuf[2] = 0x14;
   *((int *)(requestBuf + 3)) = IntSwap(objectType);
   objLength =  strlen(objectName);
   paswdLength = strlen(password);
   requestBuf[5] = objLength;
   strcpy(requestBuf + 6, objectName);
   offset = objLength + 6;
   requestBuf[offset++] = paswdLength;
   strcpy(requestBuf + offset, password);
   *((int *)requestBuf) = 5 + objLength + paswdLength;
   *((int *)replyBuf) = 0;

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.x.si = FP_OFF(requestBuf);
   ipregs.x.di = FP_OFF(replyBuf);
   ipregs.h.ah = 0xE3;
   intdosx(&ipregs,&opregs,&spregs);

   return(opregs.h.al);
}
void logout()
{

   char  requestBuf[2], replyBuf[2];

   spregs.ds   = FP_SEG(requestBuf);
   spregs.es   = FP_SEG(replyBuf);
   ipregs.h.ah = 0xD7;
   intdosx(&ipregs,&opregs,&spregs);

   return;
}

void LogoutFromFileServer(connectionID)
int   connectionID;
{

   ipregs.h.ah = 0xF1;
   ipregs.h.al = 0x02;
   ipregs.h.dl = (char)connectionID;
   intdosx(&ipregs,&opregs,&spregs);

   return;
}

⌨️ 快捷键说明

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