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

📄 serversock.c

📁 这个是服务器端的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        printf("\n");
        printf("Mask Nagle in function MyAccept() failed!\n");
        printf("\n");
#endif
		    return (-3);
	  }                    

  	FD_SET(newsock, &(ssocket->readfds));
	  FD_SET(newsock, &(ssocket->writefds));

	  FD_SET(newsock, &(ssocket->exceptfds));
	
	  i = 0;
  	while (ssocket->clientonline[i].ClientSock != 0)
  	{
		    i++;
  	}
	
  	if (i >= 64)
  	{
	    	myclosesocket(newsock);
		    return (-4); 
    }
	
	  ssocket->clientonline[i].ClientSock = newsock;
//	  ssocket->clientonline[i].ClientPort = 1024;
	  strcpy(ssocket->clientonline[i].ClientIP, inet_ntoa(client_addr.sin_addr));
	  ssocket->clientcount++;
#if defined (SOCKCOMP_DEBUG)
    tmp = ssocket->clientcount;
    printf("\n");
    printf("The ssocket->clientcount is increase from %d to %d in MyAccept(),\n", tmp-1, tmp);
    printf("The IP of client is %s.\n", ssocket->clientonline[i].ClientIP);
    printf("This is the IP list:\n");
    for (j=0;j<ssocket->clientcount;j++)
    {
        printf("The IP of client %d is %s.\n", j, ssocket->clientonline[j].ClientIP);
    }
    printf("\n");
#endif
	  return (i);
}

//关闭由Sockno指示的连接 
MYBOOL CloseConnection(int Sockno, ServerSockets *ssocket)
{
  	int ret;
    int i, j;//add by lhm
    
#if defined (SOCKCOMP_DEBUG)
    int tmp;
#endif

	  if ((Sockno >= 64) || (Sockno < 0) || (ssocket->clientonline[Sockno].ClientSock == 0))
    {
#if defined (SOCKCOMP_DEBUG)
        if (ssocket->clientonline[Sockno].ClientSock == 0)
        {
            printf("The ssocket->clientonline[%d].ClientSock == 0 in funtion CloseConnection()\n", Sockno);
        }
        else
            printf("The Sockno %d is larger than 64 or litter than 0 in funtion CloseConnection()!\n", Sockno);
#endif
		    return WRONG;
    }

	  if (!(ret = myclosesocket(ssocket->clientonline[Sockno].ClientSock)))
    {
        return WRONG;
    }

	  FD_CLR(ssocket->clientonline[Sockno].ClientSock, &(ssocket->readfds));
	  FD_CLR(ssocket->clientonline[Sockno].ClientSock, &(ssocket->writefds));
	  FD_CLR(ssocket->clientonline[Sockno].ClientSock, &(ssocket->exceptfds));
    
    //add by lhm begin
    i = 0;
    while (ssocket->ReadQueue[i] != Sockno)
    {
        i++;
    }
    if (i < ssocket->ReadNum)
    {
        for (j=i; j<ssocket->ReadNum; j++)
        {
            ssocket->ReadQueue[j] = ssocket->ReadQueue[j+1];
        }
        ssocket->ReadNum--;
#if defined (SOCKCOMP_DEBUG)
        tmp = ssocket->ReadNum;
        printf("The ssocket->ReadNum was descrease from %d to %d in CloseConnection().\n", tmp+1, tmp);
#endif
    }

    i = 0;
    while (ssocket->WriteQueue[i] != Sockno)
    {
        i++;
    }
    if (i < ssocket->WriteNum)
    {
        for (j=i; j<ssocket->WriteNum; j++)
        {
            ssocket->WriteQueue[j] = ssocket->WriteQueue[j+1];
        }
        ssocket->WriteNum--;
#if defined (SOCKCOMP_DEBUG)
        tmp = ssocket->WriteNum;
        printf("The ssocket->WriteNum was descrease from %d to %d in CloseConnection().\n", tmp+1, tmp);
#endif      
    }

    i = 0;
    while (ssocket->ExceptQueue[i] != Sockno)
    {
        i++;
    }
    if (i < ssocket->ExceptNum)
    {
        for (j=i; j<ssocket->ExceptNum; j++)
        {
            ssocket->ExceptQueue[j] = ssocket->ExceptQueue[j+1];

        }
        ssocket->ExceptNum--;
#if defined (SOCKCOMP_DEBUG)
        tmp = ssocket->ExceptNum;
        printf("The ssocket->ExceptNum was descrease from %d to %d in CloseConnection().\n", tmp+1, tmp);
#endif         
    }   
    //add by lhm end
    
	  ssocket->clientonline[Sockno].ClientSock = 0;
	  ssocket->clientcount--;
#if defined (SOCKCOMP_DEBUG)
        tmp = ssocket->clientcount;
        printf("The ssocket->clientcount was descrease from %d to %d in CloseConnection().\n", tmp+1, tmp);
#endif  
	  return(ret);
}

//query all sockets, include daemon socket and all client sockets
int QuerySockets(ServerSockets *ssocket)
{
#if defined (SOCKCOMP_DEBUG)
    int tmp, j;
#endif
	  fd_set rfds, wfds, efds;
  	int ret, i;
	  struct timeval TimeOut;

	  rfds = ssocket->readfds;
	  wfds = ssocket->writefds;
	  efds = ssocket->exceptfds;

	  TimeOut.tv_sec = 0; 
	  TimeOut.tv_usec = 0;

    //clear all previous information
    {
        ssocket->RequestNum = 0;
        ssocket->ReadNum = 0;
        ssocket->WriteNum = 0;
        ssocket->ExceptNum = 0;
        for (i=0; i<64; i++)
        {
            ssocket->ReadQueue[i] = 0;
            ssocket->WriteQueue[i] = 0;
            ssocket->ExceptQueue[i] = 0;
        }
    }
    
	  if ((ret = select(64, &rfds, &wfds, &efds, &TimeOut)) == 0)
    {
		   return(0);
     }
	
	  if (FD_ISSET(ssocket->DaemonSock, &rfds))
    {
		    ssocket->RequestNum = 1;    //some client call server.
#if defined (SOCKCOMP_DEBUG)
            printf("The daemon socket %d is readable.\n", ssocket->DaemonSock);
            printf("The ssocket->ReadNum was increase from %d to %d in QuerySockets().\n", tmp-1, tmp);
#endif 
    }

	  for (i=0; i<64; i++)           //Data in message
  	{
	    	if ((ssocket->clientonline[i].ClientSock > 0) && (FD_ISSET(ssocket->clientonline[i].ClientSock, &rfds)))
        {
            ssocket->ReadNum += 1;
            ssocket->ReadQueue[ssocket->ReadNum] = i;
#if defined (SOCKCOMP_DEBUG)
            tmp = ssocket->ReadNum;
            printf("The ssocket->ReadNum was increase from %d to %d in QuerySockets().\n", tmp-1, tmp);
#endif 
        }
	  }

	  for (i=0; i<64; i++) //Data out ready message
  	{
		    if ((ssocket->clientonline[i].ClientSock > 0) && (FD_ISSET(ssocket->clientonline[i].ClientSock, &wfds)))
        {
            ssocket->WriteNum += 1;
			      ssocket->WriteQueue[ssocket->WriteNum] = i;
#if defined (SOCKCOMP_DEBUG)
            tmp = ssocket->WriteNum;
            printf("The ssocket->WriteNum was increase from %d to %d in QuerySockets().\n", tmp-1, tmp);
#endif
        }
    }
	
	  if (FD_ISSET(ssocket->DaemonSock, &efds))
    {
        ssocket->RequestNum = -1; //server socket error.
    }
  	for (i=0; i<64; i++)        //Error message
	  {
		    if ((ssocket->clientonline[i].ClientSock > 0) && (FD_ISSET(ssocket->clientonline[i].ClientSock, &efds)))
        {
            ssocket->ExceptNum += 1;
			      ssocket->ExceptQueue[ssocket->ExceptNum] = i;
#if defined (SOCKCOMP_DEBUG)
            tmp = ssocket->ExceptNum;
            printf("The ssocket->ExceptNum was increase from %d to %d in QuerySockets().\n", tmp-1, tmp);
#endif
        }
	  }
    
#if defined (SOCKCOMP_DEBUG)
    printf("The current ReadNum is %d\n", ssocket->ReadNum);
    printf("The sockets list which are readable is:\n");
    for (j=0;j<ssocket->ReadNum;j++)
    {
        printf("socket %d\n", ssocket->clientonline[ssocket->ReadQueue[j]].ClientSock);
    }
    printf("The current WriteNum is %d\n", ssocket->WriteNum);
    printf("The sockets list which are writeable is:\n");
    for (j=0;j<ssocket->WriteNum;j++)
    {
        printf("socket %d\n", ssocket->clientonline[ssocket->WriteQueue[j]].ClientSock);
    }
    printf("The current ExceptNum is %d\n", ssocket->ExceptNum);
    printf("The sockets list which are Exception is:\n");
    for (j=0;j<ssocket->ExceptNum;j++)
    {
        printf("socket %d\n", ssocket->clientonline[ssocket->ExceptQueue[j]].ClientSock);
    }
    printf("The current clientcount is %d\n", ssocket->clientcount);
    printf("\n\n");
#endif
	
	  return(ret);
}

////send a data packet to a socket identify by Sockno
int SendPacket(int Sockno, void *buf, int len, ServerSockets *ssocket)

{
	  int ret; 

	  if ((Sockno >= 64) || (Sockno < 0) || (ssocket->clientonline[Sockno].ClientSock == 0))
	  {
#if defined (SOCKCOMP_DEBUG)
        printf("The Sockno %d is larger than 64 or litter than 0 in funtion SendPacket()!\n", Sockno);
#endif
		    return (-1);
	  }

	  if ((ret = send(ssocket->clientonline[Sockno].ClientSock, buf, len, 0)) < 0)
	  {
#if defined (SOCKCOMP_DEBUG)
        printf("send data to Sockno %d is litter than 0 in funtion SendPacket()!\n", Sockno);
        printf("It means error occur when user call function send().\n\n");
#endif
		    return (0);//

	  }

	  return (ret);
}


//receive a data packet from a socket identify by Sockno
int RecvPacket(int Sockno, void *buf, int size, ServerSockets *ssocket)
{
	  int ret;

	  if ((Sockno >= 64) || (Sockno < 0) || (ssocket->clientonline[Sockno].ClientSock == 0))

    {
#if defined (SOCKCOMP_DEBUG)
        printf("The Sockno %d is larger than 64 or litter than 0 in funtion RecvPacket()!\n", Sockno);
#endif
        return (-1);
  	}

  	if ((ret = recv(ssocket->clientonline[Sockno].ClientSock, buf, size, 0)) < 0) 
  	{
#if defined (SOCKCOMP_DEBUG)
        printf("Received data form Sockno %d is litter than 0 in funtion RecvPacket(),\n", Sockno);
        printf("It means error occur when user call function recv().\n\n");
#endif
		    return (0);//return 0 will cause this socket will be closed by user
	  }

  	return (ret); 
}

⌨️ 快捷键说明

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