// Chat.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "Chat.h"
#include "MyIntro.h"
#define DEST_IP_ADDR "127.0.0.1"
#define PORT 56790
#define NO_FLAGS_SET 0
#define MAXBUFLEN 5025
#define WM_SYSTEMTRAY WM_USER+1
#define WM_RECVDATA WM_USER+2
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HWND recvWnd;
/////////////////////////////////////////////////////////////////////////////
// CChat dialog
UINT Recv_Thread(void* cs)
{
char buffer[MAXBUFLEN];
int numrcv;
SOCKET clientSocket=(SOCKET)cs;
while(1)
{
numrcv=recv(clientSocket, buffer, MAXBUFLEN, NO_FLAGS_SET);
if ((numrcv == 0) || (numrcv == SOCKET_ERROR))
{
AfxMessageBox("Connection terminated!",MB_OK);
break;
}
buffer[numrcv]='\0';
::PostMessage(recvWnd,WM_RECVDATA,DWORD((LPSTR)buffer),numrcv);
}
return 1;
}
CChat::CChat(CWnd* pParent /*=NULL*/)
: CDialog(CChat::IDD, pParent)
{
//{{AFX_DATA_INIT(CChat)
m_room = _T("");
m_recv = _T("");
//}}AFX_DATA_INIT
m_hIcon=AfxGetApp()->LoadIcon(131);
}
void CChat::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChat)
DDX_Control(pDX, IDC_FRIEND, m_friend);
DDX_Control(pDX, IDC_PETNAME, m_petname);
DDX_Control(pDX, IDC_MESSAGE, m_message);
DDX_Control(pDX, IDC_TUBIAO, m_tubiao);
DDX_Control(pDX, IDC_MORE, m_more);
DDX_Text(pDX, IDC_ROOM, m_room);
DDX_Text(pDX, IDC_RECEIVE, m_recv);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChat, CDialog)
//{{AFX_MSG_MAP(CChat)
ON_BN_CLICKED(IDC_MORE, OnMore)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_REPLY, OnReply)
ON_CBN_SELCHANGE(IDC_PETNAME, OnSelchangePetname)
ON_MESSAGE(WM_RECVDATA,RecvData)
ON_COMMAND(ID_ABOUT, OnAbout)
ON_COMMAND(ID_CLOSE, OnClose)
ON_COMMAND(ID_SHOW, OnShow)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SYSTEMTRAY,OnSystemTray)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChat message handlers
BOOL CChat::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
/////////////////////////////网络初始化//////////////////////////////////////
// clientsocket.SetParent(this);
Init_net();
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////对话框大小控制//////////////////////////////////
GetWindowRect(&m_rectFull);
m_rectHalf=m_rectFull;
CRect rect;
m_more.GetWindowRect(&rect);
m_rectHalf.bottom=rect.bottom+5;
m_bToggleSize=false;
ToggleSize();
/////////////////////////////////////////////////////////////////////////////
///////////////////////////读用户信息文件//////////////////////////////////////
CString str;
int j=0;
char *pos=new char;
char *pos1=new char;
char temp[40],temp1[40];
filename.Format(".\\data\\%s.db",m_username);
/* filename+=m_username;
filename+=".db";*/
m_file.Open(filename, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite | CFile::shareDenyWrite);
m_file.SeekToBegin();
while(m_file.ReadString(str))
{
str.TrimRight();
str.TrimLeft();
pos=strchr(str,',');
int len_str=strlen(str);
int len_pos=strlen(pos);
int len=len_str-len_pos;
pos+=1;
pos1=pos;
for(int i=0;i<len;i++)
temp[i]=str[i];
temp[len]='\0';
if(strcmp(temp,m_username)!=0)
m_petname.InsertString(m_petname.GetCount(),temp);
len_str=strlen(pos1);
pos=strchr(pos1,',');
len_pos=strlen(pos);
len=len_str-len_pos;
pos+=1;
for(i=0;i<len;i++)
temp1[i]=pos1[i];
temp1[len]='\0';
pet[j]=atoi(temp1);
if(strcmp(temp,m_username)==0)
res=pet[j];
// room[j]=pos;
map.SetAt(temp,pos);
j++;
}
m_petname.SetCurSel(0);
CString aa;
m_petname.GetLBText(0,aa);
m_room=map[aa];
m_file.Close();
///////////////////////////////////////////////////////////////////////////////
/////////////////////////系统托盘////////////////////////////////////////////
NOTIFYICONDATA nid;
nid.cbSize=sizeof(NOTIFYICONDATA);
nid.hWnd=m_hWnd;
nid.uID=ID_SYSTEMTRAY;
nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
nid.uCallbackMessage=WM_SYSTEMTRAY;
nid.hIcon=AfxGetApp()->LoadIcon(res);
strcpy(nid.szTip,"网际飞标");
::Shell_NotifyIcon(NIM_ADD,&nid);
if(res!=280)
{
HICON hIcon=AfxGetApp()->LoadIcon(res);
m_tubiao.SetIcon(hIcon);
}
m_netsel=res;
res_own.Format("%d",res);
flag=false;
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////界面///////////////////////////////////////////
GetDlgItem(IDC_REPLY)->EnableWindow(false);
recvWnd=m_hWnd;
CString title;
title.Format("%s%s",m_username,"的好友");
m_friend.SetWindowText(title);
bReply=false;
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////读文档////////////////////////////////////
filename.Format(".\\data\\%s.fly",m_username);
m_file.Open(filename, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite | CFile::shareDenyWrite);
m_file.SeekToBegin();
// CString str1;
while(m_file.ReadString(str))
{
str.TrimRight();
m_recv+=str;
m_recv+="\r\n";
}
m_file.Close();
///////////////////////////////////////////////////////////////////////////////
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CChat::Init_net()
{
/////////////////////////网络初始化///////////////////////////////////
status=WSAStartup(MAKEWORD(1, 1), &Data);
if (status != 0)
MessageBox("ERROR: WSAStartup unsuccessful!","网际飞标",MB_OK);
destAddr=inet_addr(DEST_IP_ADDR); //暂时定为本机使用
memcpy(&destSockAddr.sin_addr, &destAddr,sizeof(destAddr));
destSockAddr.sin_port=htons(PORT);
destSockAddr.sin_family=AF_INET;
destSocket=socket(AF_INET, SOCK_STREAM, 0);
if (destSocket == INVALID_SOCKET)
{
MessageBox("ERROR: socket unsuccessful!","网际飞标",MB_OK);
status=WSACleanup();
if (status == SOCKET_ERROR)
MessageBox("ERROR: WSACleanup unsuccessful!","网际飞标",MB_OK);
return false;
}
//////////////////////////////////////////////////////////////////////
///////////////////////////连接网络////////////////////////////////////////////
status=connect(destSocket, (LPSOCKADDR) &destSockAddr,sizeof(destSockAddr));
if (status == SOCKET_ERROR)
{
MessageBox("ERROR: connect unsuccessful!","网际飞标",MB_OK);
status=closesocket(destSocket);
if (status == SOCKET_ERROR)
MessageBox("ERROR: closesocket unsuccessful!","网际飞标",MB_OK);
status=WSACleanup();
if (status == SOCKET_ERROR)
MessageBox("ERROR: WSACleanup unsuccessful!","网际飞标",MB_OK);
return false;
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/* if(clientsocket.Create()==0)
{
MessageBox("ERROR: Create unsuccessful!","网际飞标",MB_OK);
return false;
}
if(clientsocket.Connect(DEST_IP_ADDR,56790)==0)
{
MessageBox("ERROR: Connect unsuccessful!","网际飞标",MB_OK);
return false;
}
int p=clientsocket.GetLastError();*/
send(destSocket,m_username,m_username.GetLength(),0);
AfxBeginThread(Recv_Thread,(LPVOID)destSocket);
return true;
}
/*void CChat::Receive()
{
m_message.SetReadOnly(true);
GetDlgItem(IDC_REPLY)->EnableWindow(true);
SetTimer(0,200,NULL);
}*/
void CChat::OnMore()
{
// TODO: Add your control notification handler code here
ToggleSize();
}
void CChat::ToggleSize()
{
CRect rect;
CString str;
if(m_bToggleSize)
{
str="<<&Less";
rect=m_rectFull;
}
else
{
str="&More>>";
没有合适的资源?快使用搜索试试~ 我知道了~
基于TCP/IP的局域网多用户通信
共91个文件
ico:30个
h:18个
cpp:15个
需积分: 0 7 下载量 116 浏览量
2008-08-01
17:04:24
上传
评论
收藏 745KB RAR 举报
温馨提示
实现了多用户同时在线通讯。
资源详情
资源评论
资源推荐
收起资源包目录
基于tcpip局域网多用户通信.rar (91个子文件)
43634636
注册_登陆服务器
ServerDlg.cpp 13KB
StdAfx.cpp 208B
resource.h 911B
Server.rc 5KB
Server.h 1KB
Server.clw 814B
StdAfx.h 1KB
Server.dsp 4KB
User.h 1KB
ServerDlg.h 2KB
res
Server.ico 1KB
Server.rc2 398B
1.ico 766B
User.cpp 2KB
Server.cpp 2KB
Server.dsw 537B
用户端
Register.cpp 6KB
Client.dsp 9KB
StdAfx.cpp 208B
resource.h 3KB
Chat.h 2KB
Chat.cpp 14KB
ClientDlg.h 2KB
IconComboBox.h 1KB
MyIntro.h 1KB
music
msg.wav 9KB
begin.wav 29KB
Register.h 2KB
MySocket.h 1KB
StdAfx.h 1KB
ClientDlg.cpp 11KB
IconComboBox.cpp 5KB
Client.dsw 537B
avi
begin.avi 934KB
MySocket.cpp 985B
Client.cpp 2KB
res
Client.rc2 398B
data
2.fly 412B
1.db 126B
1.fly 793B
2.db 126B
test.fly 263B
test.db 126B
face
Client.rc2 398B
18.ico 766B
11.ico 766B
17.ico 766B
14.ico 2KB
15.ico 2KB
19.ico 766B
05.ico 766B
04.ico 766B
13.ico 2KB
10.ico 766B
08.ico 766B
icon26.ico 766B
01.ico 766B
03.ico 766B
25.ico 766B
22.ico 766B
07.ico 766B
02.ico 766B
20.ico 766B
06.ico 766B
24.ico 766B
23.ico 766B
09.ico 766B
1.bmp 38KB
16.ico 766B
21.ico 766B
12.ico 766B
Client.ico 1KB
MyIntro.cpp 1KB
Client.clw 4KB
Client.rc 11KB
Client.h 1KB
netuser.mdb 116KB
下载说明.htm 5KB
通信服务器
StdAfx.cpp 212B
resource.h 733B
ChatServerDlg.h 2KB
ChatServer.dsw 545B
ChatServer.dsp 4KB
ChatServer.cpp 2KB
StdAfx.h 1KB
ChatServer.rc 5KB
ChatServer.clw 1KB
ChatServer.h 1KB
res
ChatServer.rc2 402B
ChatServer.ico 1KB
ChatServerDlg.cpp 10KB
共 91 条
- 1
点点拉圈圈
- 粉丝: 3
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0