查看用户界面
实现代码:
UINT RecvThread(LPVOID sock)
{
BOOL IsRun = TRUE;
SOCKET mySocket;
mySocket = (SOCKET)sock;
POSITION pos;
POSITION pos2;
CP2PData temp;
CUserSocket usersocket;
CUserSocket tempsock;
CRoomInfo roominfo;
int listcount;
while(IsRun)
{
int n_recv;
char RecvBuffer[1024];
//接收用户消息
n_recv = ::recv(mySocket, RecvBuffer, sizeof(RecvBuffer), 0);
if (n_recv > 0)
{
//分割消息,取出标志位
RecvBuffer[n_recv] = '\0';
int i = 0;
char flag[20];
char *pbuffer = RecvBuffer;
while(*pbuffer != '\0')
{
if(*pbuffer == '~')
break;
flag[i] = *pbuffer;
i++, pbuffer++;
}
flag[i] = '\0';
pbuffer++;
//用户退出消息处理
if (strcmp(flag, "USER_QUIT") == 0)
{
//查找用户结点
pos = SocketList.GetHeadPosition();
listcount = SocketList.GetCount();
for(int j=0; j<listcount; j++)
{
usersocket = SocketList.GetNext(pos);
if (usersocket.clientsocket == mySocket)
{
//找到用户结点
send(mySocket, RecvBuffer, strlen(RecvBuffer) + 1, 0);
break;
}
}
if (pos == NULL)
{
pos = SocketList.GetHeadPosition();
}
751com.cn
{
SocketList.GetPrev(pos);
}
//删除该用户
SocketList.RemoveAt(pos);
//设置标志位,让线程结束
IsRun = FALSE;
}
//普通消息处理
else if (strcmp(flag, "USER_MESSAGE") == 0)
{
int len;
int roomno;
char *m_pstr;
char usname[20];
CString SourceName;
char sendbuffer[200];
//查找当前消息的用户
pos = SocketList.GetHeadPosition();
listcount =SocketList.GetCount();
for(int j=0; j<listcount; j++)
{
usersocket = SocketList.GetNext(pos);
if (usersocket.clientsocket == mySocket)
{
//找到并记录用户名和房间编号
SourceName = usersocket.username;
roomno = usersocket.RoomNo;
break;
}
}
//查找字符串pbuffer中是否有:
m_pstr = strchr(pbuffer,':');
if (m_pstr == NULL)
{
//如果为空,则是一般聊天消息
sprintf(sendbuffer, "%s~%s 说:%s", flag, usersocket.username, pbuffer);
pos =SocketList.GetHeadPosition();
for(j=0; j<listcount; j++)
{
usersocket = SocketList.GetNext(pos);
if (usersocket.RoomNo == roomno)
{
//向所有在同一聊天室的用户转发消息
send(usersocket.clientsocket, sendbuffer, strlen(sendbuffer) + 1, 0);
}
}
}
else
{
//不为空,则是密语
len = strlen(pbuffer) - strlen(m_pstr);
//取出密语对象用户名
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ... 下一页 >>