7,540
社区成员
发帖
与我相关
我的任务
分享
sockaddr_in * MySocket::ReciveInfo(message * info)
{
sockaddr_in from;
int addr_len = sizeof(struct sockaddr_in);
message reciveBuffer;
memset(&reciveBuffer, 0, sizeof(reciveBuffer));
addr_len = recvfrom(sock,(char * )&reciveBuffer,sizeof(reciveBuffer),0,(sockaddr *)&from,&addr_len);
if (addr_len == -1)
return NULL;
strcpy_s(info->info, (const char*)reciveBuffer.info);
strcpy_s(info->touser, (const char*)reciveBuffer.touser);
strcpy_s(info->user, (const char*)reciveBuffer.user);
info->typt = reciveBuffer.typt;
return &from;
}
short MySocket::Run(int msgTypt, char * userName, char* infoMsg, char* toUserName, sockaddr_in * clientIP)
{
short sRtn = false;
int addr_len = sizeof(struct sockaddr);
cout << "error1" << endl;
message sendBuffer;
sendBuffer.typt = msgTypt;
strcpy_s(sendBuffer.user, userName);
strcpy_s(sendBuffer.info, infoMsg);
strcpy_s(sendBuffer.touser, toUserName);
cout << "error2" << endl;
try
{
sRtn = sendto(sock, (const char *)&sendBuffer, sizeof(sendBuffer), 0, (const sockaddr *)clientIP, addr_len);
}
catch (...)
{
cout << "error" << endl;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL);
}
return sRtn;