用MFC做socket通信时,客户端怎么把输入的用户名和密码发送到服务器端进行验证,有没有会的啊,谢谢啦!
2个回答
展开全部
//客户端
memset(sendMess.name,0,20);
memset(sendMess.mima,0,20);//赋值语句
int ServerSession::send_user(struct user_infor sendMess)
{
int nByteSent;
nByteSent=send(m_hSocket,(char *)&sendMess,sizeof(sendMess),0);
if(nByteSent==sizeof(sendMess))
{
char sRecv[256];
int nBytes;
//取得服务器自动回复的消息
nBytes=recv(m_hSocket,sRecv,255,0);
if(nBytes==SOCKET_ERROR)
{
AfxMessageBox("接受数据失败");
return 1;
}
sRecv[nBytes]='\0';
//显示回复信息
if(!strcmp(sRecv,"登陆成功!"))
flag=1;
else
flag=0;
AfxMessageBox(sRecv);
if(closesocket(m_hSocket)==SOCKET_ERROR)
{
AfxMessageBox("关闭连接失败");
m_hSocket=NULL;
return 1;
}
}
}
//服务器端
UINT Socket_Session::Log_Set_Run()//获取客户端发送的消息
{
SOCKADDR_IN sa_addr;
ASSERT(g_hSocket!=NULL);
int nLen=sizeof(SOCKADDR);
//等待接受客户端的连接请求
g_hAcceptSocket=accept(g_hSocket,(sockaddr *)&sa_addr,&nLen);
g_hAcceptObject[Num++]=g_hAcceptSocket;
m_strIP = inet_ntoa(sa_addr.sin_addr);
if(g_hAcceptSocket==INVALID_SOCKET)
{
if(WSAGetLastError()!=WSAEINTR)
AfxMessageBox("接受连接失败");
return 1;
}
//接受到一个客户端的请求后,立即启动一个线程重新开始监听
AfxBeginThread(Log_Set,this);
//处理与客户端的通信
AfxBeginThread(Proposal_Message,this);
return 0;
}
memset(sendMess.name,0,20);
memset(sendMess.mima,0,20);//赋值语句
int ServerSession::send_user(struct user_infor sendMess)
{
int nByteSent;
nByteSent=send(m_hSocket,(char *)&sendMess,sizeof(sendMess),0);
if(nByteSent==sizeof(sendMess))
{
char sRecv[256];
int nBytes;
//取得服务器自动回复的消息
nBytes=recv(m_hSocket,sRecv,255,0);
if(nBytes==SOCKET_ERROR)
{
AfxMessageBox("接受数据失败");
return 1;
}
sRecv[nBytes]='\0';
//显示回复信息
if(!strcmp(sRecv,"登陆成功!"))
flag=1;
else
flag=0;
AfxMessageBox(sRecv);
if(closesocket(m_hSocket)==SOCKET_ERROR)
{
AfxMessageBox("关闭连接失败");
m_hSocket=NULL;
return 1;
}
}
}
//服务器端
UINT Socket_Session::Log_Set_Run()//获取客户端发送的消息
{
SOCKADDR_IN sa_addr;
ASSERT(g_hSocket!=NULL);
int nLen=sizeof(SOCKADDR);
//等待接受客户端的连接请求
g_hAcceptSocket=accept(g_hSocket,(sockaddr *)&sa_addr,&nLen);
g_hAcceptObject[Num++]=g_hAcceptSocket;
m_strIP = inet_ntoa(sa_addr.sin_addr);
if(g_hAcceptSocket==INVALID_SOCKET)
{
if(WSAGetLastError()!=WSAEINTR)
AfxMessageBox("接受连接失败");
return 1;
}
//接受到一个客户端的请求后,立即启动一个线程重新开始监听
AfxBeginThread(Log_Set,this);
//处理与客户端的通信
AfxBeginThread(Proposal_Message,this);
return 0;
}
2012-06-04
展开全部
char buff[1024];
char buff2[1024*10];
int receiveLen;
strcpy(buff,"username=mike;pass=1234567890;");
typedef int (WINAPI ICEPUB_TCPSENDANDRECEIVETEXT)(char *sendBuff, int sendBuffLen,char *recvBuff,int recvMaxLen,char* serverIP,int serverPort);
ICEPUB_TCPSENDANDRECEIVETEXT *icePub_tcpSendAndReceiveText = 0;
HINSTANCE hDLLDrv = LoadLibrary("icePubDll.dll");
if(hDLLDrv)
{
icePub_tcpSendAndReceiveText=(ICEPUB_TCPSENDANDRECEIVETEXT *)GetProcAddress(hDLLDrv,"icePub_tcpSendAndReceiveText");
}
if(icePub_tcpSendAndReceiveText)
receiveLen=icePub_tcpSendAndReceiveText(buff,strlen(buff),buff2,1024*10,"192.168.1.111",8000);
if(hDLLDrv)
FreeLibrary(hDLLDrv);
AfxMessageBox(buff2);
char buff2[1024*10];
int receiveLen;
strcpy(buff,"username=mike;pass=1234567890;");
typedef int (WINAPI ICEPUB_TCPSENDANDRECEIVETEXT)(char *sendBuff, int sendBuffLen,char *recvBuff,int recvMaxLen,char* serverIP,int serverPort);
ICEPUB_TCPSENDANDRECEIVETEXT *icePub_tcpSendAndReceiveText = 0;
HINSTANCE hDLLDrv = LoadLibrary("icePubDll.dll");
if(hDLLDrv)
{
icePub_tcpSendAndReceiveText=(ICEPUB_TCPSENDANDRECEIVETEXT *)GetProcAddress(hDLLDrv,"icePub_tcpSendAndReceiveText");
}
if(icePub_tcpSendAndReceiveText)
receiveLen=icePub_tcpSendAndReceiveText(buff,strlen(buff),buff2,1024*10,"192.168.1.111",8000);
if(hDLLDrv)
FreeLibrary(hDLLDrv);
AfxMessageBox(buff2);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询