socket c语言程序 c/s tcp模式 (程序部分2)

本文介绍了一个分布式银行系统的实现细节,包括账户初始化、存款、取款、查询等功能的处理流程,并展示了如何通过互斥锁保证多线程操作的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*************************************************************************
* COPYRIGHT MIMO9527                                                  *
**************************************************************************
* 文件名   :           
* 功能     :
* 版本     : v1.0                                                                
* 编写日期 : 10/16/2010              
* 说明     : 描述使用文件功能时的制约条件                                      
* 修改历史 :
*
*   修改人 日期 内容
* ------------------------------------------------------------------------
*   MIMO9527 2010-10-16 创建
*************************************************************************/

#include "dbs_distributed_banking_system.h"

DBS_BANKING_SERVER_CONTEXT_STRUCT   gstBankingServerCtx;

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_BankingServerInitAccount(void)
{
int iLoopAccount = 0;

gstBankingServerCtx.uiAccountNumber = DBS_MAX_ACCOUNT_NUMBER;
for (iLoopAccount = 0; iLoopAccount < DBS_MAX_ACCOUNT_NUMBER; iLoopAccount++)
{
gstBankingServerCtx.astAccountRecord[iLoopAccount].uiAccountId = iLoopAccount+1;
gstBankingServerCtx.astAccountRecord[iLoopAccount].uiBalance = 1000;
gstBankingServerCtx.astAccountRecord[iLoopAccount].uiSockConnId = INVALID_SOCKET;
gstBankingServerCtx.astAccountRecord[iLoopAccount].hMutex
   = CreateMutex(NULL,              // default security attributes
       FALSE,             // initially not owned
       NULL);
if (DBS_NULL == gstBankingServerCtx.astAccountRecord[iLoopAccount].hMutex)
{
   DBS_DEBUG_LOGGING("CreateMutex error: %d/n", GetLastError());
   return;
}
}

return;
}

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_BankingServerDepositReqHandle(DBS_CLIENT_SERVER_ACCOUNT_DEPOSIT_REQ_MSG_STRUCT *vpstMsg)
{
UINT32 uiAccountIndex = 0;
UINT32 uiWaitResult = 0;
time_t Time;
DBS_SERVER_CLIENT_ACCOUNT_DEPOSIT_RSP_MSG_STRUCT stRspMsg = {0};

vpstMsg->uiAccountId = ntohl(vpstMsg->uiAccountId);
vpstMsg->uiAmount = ntohl(vpstMsg->uiAmount);

stRspMsg.iResult = htonl(DBS_SUCCESS);
if (DBS_MAX_ACCOUNT_NUMBER < vpstMsg->uiAccountId ||0 == vpstMsg->uiAccountId)
{
DBS_ERROR_LOGGING("Receive wrong account id=%d!/n",vpstMsg->uiAccountId);
stRspMsg.iResult = htonl(DBS_FAIL);
}

uiAccountIndex = vpstMsg->uiAccountId-1;

uiWaitResult = WaitForSingleObject(
gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex,    // handle to mutex
            INFINITE); // no time-out interval
switch (uiWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
{
   gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiBalance += vpstMsg->uiAmount;
   ReleaseMutex(gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex);   
}
break;  
// The thread got ownership of an abandoned mutex
case WAIT_ABANDONED:
DBS_DEBUG_LOGGING("/nWaitForSingleObject() WAIT_ABANDONED/n");
return;
}


//向客户Socket发送数据
stRspMsg.uiMessageType = htonl(SERVER_CLIENT_ACCOUNT_DEPOSIT_RSP);
stRspMsg.uiAccountId = htonl(vpstMsg->uiAccountId);


send(gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiSockConnId,(char *)&stRspMsg,sizeof(DBS_SERVER_CLIENT_ACCOUNT_DEPOSIT_RSP_MSG_STRUCT)+1,0);
Time = time(NULL);
DBS_DEBUG_LOGGING("/n%s Send message SERVER_CLIENT_ACCOUNT_DEPOSIT_RSP account=%d to ATM[%d]~~/n",ctime(&Time),vpstMsg->uiAccountId,ntohl(vpstMsg->uiAtmId));
return;
}

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_BankingServerWithdrawReqHandle(DBS_CLIENT_SERVER_ACCOUNT_WITHDRAW_REQ_MSG_STRUCT *vpstMsg)
{
UINT32 uiAccountIndex = 0;
UINT32 uiWaitResult = 0;
time_t Time;
DBS_SERVER_CLIENT_ACCOUNT_WITHDRAW_RSP_MSG_STRUCT stRspMsg = {0};

vpstMsg->uiAccountId = ntohl(vpstMsg->uiAccountId);
vpstMsg->uiAmount = ntohl(vpstMsg->uiAmount);

stRspMsg.iResult = htonl(DBS_SUCCESS);

if (DBS_MAX_ACCOUNT_NUMBER < vpstMsg->uiAccountId ||0 == vpstMsg->uiAccountId)
{
DBS_ERROR_LOGGING("Receive wrong account id=%d!/n",vpstMsg->uiAccountId);
stRspMsg.iResult = htonl(DBS_FAIL);
}

uiAccountIndex = vpstMsg->uiAccountId-1;
uiWaitResult = WaitForSingleObject(
gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex,    // handle to mutex
INFINITE); // no time-out interval
switch (uiWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
{
   if(gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiBalance < vpstMsg->uiAmount)
   {
    stRspMsg.iResult = htonl(DBS_FAIL);
   }
   else
   {
    gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiBalance -= vpstMsg->uiAmount;
   }
   /*Release ownership of the mutex object*/
   ReleaseMutex(gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex);
}
break;  
// The thread got ownership of an abandoned mutex
case WAIT_ABANDONED:
DBS_DEBUG_LOGGING("/nWaitForSingleObject() WAIT_ABANDONED/n");
return;
}

//向客户Socket发送数据
stRspMsg.uiMessageType = htonl(SERVER_CLIENT_ACCOUNT_WITHDRAW_RSP);
stRspMsg.uiAccountId = htonl(vpstMsg->uiAccountId);

send(gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiSockConnId,(char *)&stRspMsg,sizeof(DBS_SERVER_CLIENT_ACCOUNT_WITHDRAW_RSP_MSG_STRUCT)+1,0);

Time = time(NULL);
DBS_DEBUG_LOGGING("/n%s Send message SERVER_CLIENT_ACCOUNT_WITHDRAW_RSP account=%d to ATM[%d]~~/n",ctime(&Time),vpstMsg->uiAccountId,ntohl(vpstMsg->uiAtmId));

return;
}

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_BankingServerInquiryReqHandle(DBS_CLIENT_SERVER_ACCOUNT_INQUIRY_REQ_MSG_STRUCT *vpstMsg)
{
UINT32 uiAccountIndex = 0;
UINT32 uiWaitResult = 0;
time_t Time;
DBS_SERVER_CLIENT_ACCOUNT_INQUIRY_RSP_MSG_STRUCT stRspMsg = {0};

vpstMsg->uiMessageType = ntohl(vpstMsg->uiMessageType);
vpstMsg->uiAccountId = ntohl(vpstMsg->uiAccountId);

stRspMsg.iResult = htonl(DBS_SUCCESS);

if (DBS_MAX_ACCOUNT_NUMBER < vpstMsg->uiAccountId ||0 == vpstMsg->uiAccountId)
{
DBS_ERROR_LOGGING("Receive wrong account id=%d!/n",vpstMsg->uiAccountId);
stRspMsg.iResult = htonl(DBS_FAIL);
}


//向客户Socket发送数据
uiAccountIndex = vpstMsg->uiAccountId-1;

stRspMsg.uiMessageType = htonl(SERVER_CLIENT_ACCOUNT_INQUIRY_RSP);
stRspMsg.uiAccountId = htonl(vpstMsg->uiAccountId);

uiWaitResult = WaitForSingleObject(
gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex,    // handle to mutex
INFINITE); // no time-out interval
switch (uiWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
{
   stRspMsg.uiBalance = htonl(gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiBalance);
  
   /*Release ownership of the mutex object*/
   ReleaseMutex(gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex);  
}
break;
// The thread got ownership of an abandoned mutex
case WAIT_ABANDONED:
DBS_DEBUG_LOGGING("/nWaitForSingleObject() WAIT_ABANDONED/n");
return;
}

send(gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiSockConnId,(char *)&stRspMsg,sizeof(DBS_SERVER_CLIENT_ACCOUNT_INQUIRY_RSP_MSG_STRUCT)+1,0);

Time = time(NULL);
DBS_DEBUG_LOGGING("/n%s Send message SERVER_CLIENT_ACCOUNT_INQUIRY_RSP account=%d to ATM[%d]~~/n",ctime(&Time),vpstMsg->uiAccountId,ntohl(vpstMsg->uiAtmId));

return;
}

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_BankingServerTransactionEndIndHandle(DBS_CLIENT_SERVER_ACCOUNT_TRANSACTION_END_IND_MSG_STRUCT *vpstMsg)
{
//TBD
return;
}

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
DBS_RET_CODE dbs_BankingServerSetSockConnId(UINT32 vuiSockConnId,UINT32 vuiAccountId)
{
UINT32 uiAccountIndex = 0;
UINT32 uiWaitResult = 0;

// if (1 == giForTest)
// {
//   Sleep(100);
// }

if (DBS_MAX_ACCOUNT_NUMBER < vuiAccountId ||0 == vuiAccountId)
{
DBS_ERROR_LOGGING("Receive wrong account id=%d!/n",vuiAccountId);
        return DBS_FAIL;
}

uiAccountIndex = vuiAccountId-1;


uiWaitResult = WaitForSingleObject(
gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex,    // handle to mutex
INFINITE); // no time-out interval
switch (uiWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
{
   gstBankingServerCtx.astAccountRecord[uiAccountIndex].uiSockConnId = vuiSockConnId;
  
   /*Release ownership of the mutex object*/
   ReleaseMutex(gstBankingServerCtx.astAccountRecord[uiAccountIndex].hMutex);  
}
break;
// The thread got ownership of an abandoned mutex
case WAIT_ABANDONED:
DBS_ERROR_LOGGING("/nWaitForSingleObject() WAIT_ABANDONED/n");
return DBS_FAIL;
}

return DBS_SUCCESS;
}
/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_BankingServerProcess(void *vpvSockConnId)
{
UINT32 uiMessageType = 0;
UINT32 uiSockConnId = (UINT32 *)vpvSockConnId;
UINT8 aucrecvbuffer[DBS_MAX_BUFFER_LENGTH]={0};
time_t Time;
DBS_RET_CODE iRetrun = DBS_FAIL;


/************************************************************************/
/*    从客户Socket接收数据                                              */
/************************************************************************/

recv(uiSockConnId,(char FAR *)aucrecvbuffer,128,0);

uiMessageType = ntohl(*(UINT32 *)aucrecvbuffer);
Time = time(NULL);

switch (uiMessageType)
{
case CLIENT_SERVER_ACCOUNT_INQUIRY_REQ:/*查询*/
DBS_DEBUG_LOGGING("/n%s Receive message CLIENT_SERVER_ACCOUNT_INQUIRY_REQ account=%d from ATM[%d]~~/n",
   ctime(&Time),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_INQUIRY_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAccountId),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_INQUIRY_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAtmId));
iRetrun = dbs_BankingServerSetSockConnId(uiSockConnId,ntohl(((DBS_CLIENT_SERVER_ACCOUNT_INQUIRY_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAccountId));
if (DBS_SUCCESS == iRetrun)
{
   dbs_BankingServerInquiryReqHandle((DBS_CLIENT_SERVER_ACCOUNT_INQUIRY_REQ_MSG_STRUCT *)aucrecvbuffer);
}
break;
case CLIENT_SERVER_ACCOUNT_DEPOSIT_REQ:/*存款*/
DBS_DEBUG_LOGGING("/n%s Receive message CLIENT_SERVER_ACCOUNT_DEPOSIT_REQ account=%d from ATM[%d]~~/n",
   ctime(&Time),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_DEPOSIT_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAccountId),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_DEPOSIT_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAtmId));
iRetrun = dbs_BankingServerSetSockConnId(uiSockConnId,ntohl(((DBS_CLIENT_SERVER_ACCOUNT_DEPOSIT_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAccountId));
if (DBS_SUCCESS == iRetrun)
{
   dbs_BankingServerDepositReqHandle((DBS_CLIENT_SERVER_ACCOUNT_DEPOSIT_REQ_MSG_STRUCT *)aucrecvbuffer);
}
break;
case CLIENT_SERVER_ACCOUNT_WITHDRAW_REQ:/*取款*/
DBS_DEBUG_LOGGING("/n%s Receive message CLIENT_SERVER_ACCOUNT_WITHDRAW_REQ account=%d from ATM[%d]~~/n",
   ctime(&Time),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_WITHDRAW_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAccountId),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_WITHDRAW_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAtmId));
iRetrun = dbs_BankingServerSetSockConnId(uiSockConnId,ntohl(((DBS_CLIENT_SERVER_ACCOUNT_WITHDRAW_REQ_MSG_STRUCT *)aucrecvbuffer)->uiAccountId));
if (DBS_SUCCESS == iRetrun)
{
   dbs_BankingServerWithdrawReqHandle((DBS_CLIENT_SERVER_ACCOUNT_WITHDRAW_REQ_MSG_STRUCT *)aucrecvbuffer);
}
break;
case CLIENT_SERVER_ACCOUNT_TRANSACTION_END_IND:
DBS_DEBUG_LOGGING("/n%s Receive message CLIENT_SERVER_ACCOUNT_TRANSACTION_END_IND account=%d from ATM[%d]~~/n",
   ctime(&Time),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_TRANSACTION_END_IND_MSG_STRUCT *)aucrecvbuffer)->uiAccountId),/
   ntohl(((DBS_CLIENT_SERVER_ACCOUNT_TRANSACTION_END_IND_MSG_STRUCT *)aucrecvbuffer)->uiAtmId ));
iRetrun = dbs_BankingServerSetSockConnId(uiSockConnId,ntohl(((DBS_CLIENT_SERVER_ACCOUNT_TRANSACTION_END_IND_MSG_STRUCT *)aucrecvbuffer)->uiAccountId));
if (DBS_SUCCESS == iRetrun)
{
   dbs_BankingServerTransactionEndIndHandle((DBS_CLIENT_SERVER_ACCOUNT_TRANSACTION_END_IND_MSG_STRUCT *)aucrecvbuffer);
}
break;
default:
DBS_ERROR_LOGGING("/n%s Receive unknown message,messagetype = %d/n",ctime(&Time),uiMessageType);
break;
}

//关闭Socket
closesocket(uiSockConnId);

return;
}

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_BankingServer(void)
{
UINT16 usPortId = 10742;
INT32 iSockId = 0;
INT32 iLen = sizeof(const struct sockaddr FAR);
UINT32 uiSockConnId = 0;
struct sockaddr_in stHostAddr = {0};
DBS_RET_CODE iReturn = DBS_FAIL;


DBS_DEBUG_LOGGING("=============================================================================/n");
DBS_DEBUG_LOGGING("===============================BANK SERVER===================================/n");
DBS_DEBUG_LOGGING("=============================================================================/n");


/************************************************************************/
/*     init account                                                     */
/************************************************************************/
dbs_BankingServerInitAccount();


/************************************************************************/
/*     InitSocket                                                       */
/************************************************************************/

iReturn = Dbs_InitSocket((unsigned short)usPortId,&iSockId,&stHostAddr);
if(DBS_SUCCESS != iReturn)
{
DBS_ERROR_LOGGING("dbs_ATM_client: Dbs_InitSocket() fail!/n");
return ;
}

//DBS_DEBUG_LOGGING("Socket init ok~~/n");

DBS_DEBUG_LOGGING("ip address of banking server is %d.%d.%d.%d/n",
stHostAddr.sin_addr.S_un.S_un_b.s_b1,stHostAddr.sin_addr.S_un.S_un_b.s_b2,/
stHostAddr.sin_addr.S_un.S_un_b.s_b3,stHostAddr.sin_addr.S_un.S_un_b.s_b4);
DBS_DEBUG_LOGGING("Start the banking server on port %d./n",usPortId);

listen(iSockId,5);

while(1)
{
//阻塞方法,获得一个客户Socket连接
uiSockConnId = accept(iSockId,(struct sockaddr FAR *)&stHostAddr,&iLen);
if (INVALID_SOCKET == uiSockConnId)
{
   DBS_ERROR_LOGGING("WSAGetLastError = %d/n",WSAGetLastError());
   return ;
}

//   DBS_DEBUG_LOGGING("===============================================================/n");
//   DBS_DEBUG_LOGGING("===============================================================/n");

//printf("uiSockConnId=%d/n",uiSockConnId);
//dbs_BankingServerProcess(&uiSockConnId);
_beginthread(dbs_BankingServerProcess,0,(void *)uiSockConnId);
//printf("thread ok~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/n");
}

//释放Winsock资源
WSACleanup();

return;

}


/************************************END OF FILE********************************/

 

/*************************************************************************
* COPYRIGHT MIMO9527                                                  *
**************************************************************************
* 文件名   : dbs_common.c           
* 功能     :
* 版本     : v1.0                                                                
* 编写日期 : 10/16/2010              
* 说明     : 描述使用文件功能时的制约条件                                      
* 修改历史 :
*
*   修改人 日期 内容
* ------------------------------------------------------------------------
*   MIMO9527 2010-10-16 创建
*************************************************************************/

#include "dbs_common.h"

/************************************************************************/
/* 全局变量定义                                                         */
/************************************************************************/
UINT8 *guc_DbsFileName = DBS_NULL;
UINT32 gui_DbsFileLine = 0;
UINT8   guc_SprintBuf[DBS_LOG_INFO_MAX_LENGTH];

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
* input
* return value : *
* -----------------------------------------------------------------------------
*
* comments      :
*******************************************************************************/
void dbs_Error_Logging(char *vpucFormat,...)
{
va_list   pucVarlist;

if (DBS_NULL == vpucFormat)
{
printf("%s,line:%d/n,input point is null/n",guc_DbsFileName,gui_DbsFileLine);
return;
}

if(DBS_LOG_INFO_MAX_LENGTH > strlen(vpucFormat) )
{
va_start(pucVarlist, vpucFormat);
vsprintf(guc_SprintBuf,vpucFormat,pucVarlist);
printf("ERROR: File:%s,Line:%d/n,%s",guc_DbsFileName,gui_DbsFileLine,guc_SprintBuf);
va_end(pucVarlist);
}
else
{
printf("%s,line:%d/n,logging info is too long(=%d) > DBS_LOG_INFO_MAX_LENGTH",guc_DbsFileName,gui_DbsFileLine,strlen(vpucFormat));
}

return;
}

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
* input
* return value :
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void dbs_Debug_Logging(char *vpucFormat, ...)
{
va_list   pucVarlist;

if (DBS_NULL == vpucFormat)
{
printf("%s,line:%d/n,input point is null/n",guc_DbsFileName,gui_DbsFileLine);
return;
}

if(DBS_LOG_INFO_MAX_LENGTH > strlen(vpucFormat) )
{
va_start(pucVarlist, vpucFormat);
vsprintf(guc_SprintBuf,vpucFormat,pucVarlist);
printf("%s",guc_SprintBuf);
va_end(pucVarlist);
}
else
{
printf("%s,line:%d/n,logging info is too long(=%d) > DBS_LOG_INFO_MAX_LENGTH",guc_DbsFileName,gui_DbsFileLine,strlen(vpucFormat));
}

return;
}

/*******************************************************************************
* name          : Dbs_InitSocket_Udp()
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
* input
* return value : *
* -----------------------------------------------------------------------------
*   DBS_SUCCESS --- function success
*   DBS_FAIL --- function fail
* comments      :
*******************************************************************************/
DBS_RET_CODE Dbs_InitSocket(UINT16 vusPortId,INT32 *vpiSockId,struct sockaddr_in *vpstHostAddr)
{
WSADATA stWsaData = {0};
struct hostent *pstHost = DBS_NULL;
INT32 iMyAddr = 0;
UINT8 aucBbuf[80];


if (0 != WSAStartup(MAKEWORD (1, 1), &stWsaData))
{
DBS_ERROR_LOGGING("Init socket error/n");
return DBS_FAIL;
}


memset(aucBbuf,0,sizeof(80*sizeof(UINT8)));

gethostname(aucBbuf,50);

if (DBS_NULL == (pstHost = (struct hostent *) gethostbyname (aucBbuf)))
{
DBS_ERROR_LOGGING("Unknown host /n");
return DBS_FAIL;
}

memcpy((void *)&iMyAddr,(void *)pstHost->h_addr,sizeof(unsigned long));

memset(vpstHostAddr,0,sizeof(struct sockaddr_in));

vpstHostAddr->sin_family = AF_INET;
memcpy((void *)&vpstHostAddr->sin_addr.s_addr,(void *)&iMyAddr,sizeof(unsigned long));
vpstHostAddr->sin_port =htons(vusPortId);


*vpiSockId = socket(AF_INET,SOCK_STREAM,0);
if ( *vpiSockId < 0)
{
DBS_ERROR_LOGGING("/nError while opening datagram socket.");
DBS_ERROR_LOGGING("errno: %d./n/n", errno);
return DBS_FAIL;
}
if (0 != vusPortId)
{

if (bind (*vpiSockId, (struct sockaddr *)vpstHostAddr, sizeof(struct sockaddr)) < 0 )
{
   DBS_ERROR_LOGGING("/nError while binding datagram socket. errno = %d /n/n",errno);
   DBS_ERROR_LOGGING("errno: %d./n/n", errno);
   shutdown(*vpiSockId,2);
   return DBS_FAIL;
}
}
return DBS_SUCCESS;
}

/************************************END OF FILE********************************/

 

/*************************************************************************
* COPYRIGHT MIMO9527                                                  *
**************************************************************************
* 文件名   :           
* 功能     :
* 版本     : v1.0                                                                
* 编写日期 : 10/16/2010              
* 说明     : 描述使用文件功能时的制约条件                                      
* 修改历史 :
*
*   修改人 日期 内容
* ------------------------------------------------------------------------
*   MIMO9527 2010-10-16 创建
*************************************************************************/

#include "dbs_distributed_banking_system.h"

/*******************************************************************************
* name          :
* description   :
* storage style :
* parameters    :
* parameter style              input/output description
* --------- ----------------- ------------ --------------------------
*
* -----------------------------------------------------------------------------
* comments      :
*******************************************************************************/
void main(void)
{
    UINT8 ucInputPar = 0;
UINT8 aucInput[128] = {0};



LABLE: DBS_DEBUG_LOGGING("Please select run as a Server or a Client,input 's' for Server,'c' for client:/n");

scanf("%c",&ucInputPar);

if ('c' == ucInputPar ||'C' == ucInputPar)
{
_beginthread(dbs_ATM_client,0,0);
while(1)
{
   if (1 == giForTest)
   {   
    scanf("%s",aucInput);
    if (0 == strcmp("stop",aucInput)||0 == strcmp("STOP",aucInput))
    {
     //printf("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/n");
     giForTest = 0;
    }
   }

   Sleep(100);
  
}
}
else if ('s' == ucInputPar||'S' == ucInputPar)
{
_beginthread(dbs_BankingServer,0,0);
while(1)
{
   Sleep(100);
  
}
}
else if ('/n' == ucInputPar || '/r' == ucInputPar)
{
goto LABLE;
}
else
{
DBS_DEBUG_LOGGING("Input is error!!/n/N");
goto LABLE;
}

return;
}

/************************************END OF FILE********************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值