网络原理实验报告命令Word文档下载推荐.docx
《网络原理实验报告命令Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《网络原理实验报告命令Word文档下载推荐.docx(22页珍藏版)》请在冰点文库上搜索。
returnjTextArea_sysInfo;
//通过TCP来发送数据的方法
publicvoidTCPSend(Stringstr){
if(s!
=null)
try{
dos.writeUTF(str);
dos.flush();
}catch(SocketExceptione){
//e.printStackTrace();
jTextArea_sysInfo.append("
服务器已经离线!
\n"
);
}catch(IOExceptione){
e.printStackTrace();
}
//通过TCP来接受数据的方法
publicStringTCPReceive(){
Stringstr=null;
try{
str=dis.readUTF();
}catch(NullPointerExceptione1){
//e1.printStackTrace();
newChatMessageDialog(this,"
提示"
"
服务器未开机"
请联系管理员!
"
}catch(IOExceptione){
if(s!
s.close();
}catch(NullPointerExceptione1){
e1.printStackTrace();
//newChatMessageDialog(this,"
"
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
returnstr;
//接收线程用来处理和服务器端的信息传递
classReceiveThreadimplementsRunnable{
publicvoidrun(){
while(running){
//接收数据
Stringstr=TCPReceive();
//对不同的数据格式的处理
//对收到在线人员端口号的处理
if(str!
=null&
&
str.startsWith("
当前在线#"
)){
str_Online=str;
addFriendTree();
}//对收到服务器消息的处理
elseif(str!
-系统-"
jTextArea_sysInfo.append(str+"
}//对收到服务器对密码验证的结果的处理
密码验证#"
Stringstr_splits[]=str.split("
#"
//System.out.println("
str_splits[0]:
+str_splits[0]);
str_splits[1]:
+str_splits[1]);
if(str_splits[1].trim().equals("
密码正确"
ld.dispose();
verifyed=true;
setTitle("
NewChatClient--"
+s.getLocalPort()+"
--"
+ld.account);
jLabel_account.setText(ld.account);
setVisible(true);
}else{
ld.setInfoLabel(str_splits[1]);
continue;
}
}//对收到服务器端的注册反馈信息的处理
注册反馈#"
System.out.println("
注册成功"
ld.rd.dispose();
applyed=true;
MessageDialog("
恭喜你"
MessageDialog(str_splits[1],str_splits[2]);
}else{
distribute(str);
}
//包装消息窗口的方法
publicvoidMessageDialog(Stringstr_up,Stringstr_down){
newChatMessageDialog(this,"
str_up,str_down);
//对服务器发来其他客户端给本客户端信息的处理特别处理了窗口匹配的问题
publicvoiddistribute(Stringstr){
Stringstr_port=str.split("
:
)[0];
if(ChatDialogs.containsKey(str_port.trim())){
ChatDialogcd=ChatDialogs.get(str_port.trim());
cd.jta.append(str+"
}else{
ChatDialogcd=newChatDialog(this);
cd.launchFrame(str_port.trim());
if(isContained(cd)==false){
addChatDialog(cd);
}catch(NullPointerExceptione){
由于不明网络故障断线"
请联系网络管理员!
System.exit(-1);
//添加主界面的朋友树组建的方法
publicvoidaddFriendTree(){
//jTextArea_sysInfo.append(str_Online+"
FriendTreefriendTree=newFriendTree(str_Online,jPanel_friendTree,
this);
//添加聊天对话框的方法是对话框的管理方法
publicvoidaddChatDialog(ChatDialogcd){
this.ChatDialogs.put(cd.str_Port,cd);
//查询对话框是否存在的方法时对话框的管理方法
publicbooleanisContained(ChatDialogcd){
returnChatDialogs.containsKey(cd.str_Port);
//生成密码验证协议格式的方法
publicbooleancheckPassword(Stringaccount,Stringpassword){
TCPSend("
Password#"
+account+"
+password+"
returnverifyed;
//生成注册协议格式的方法
publicbooleanapplyAccount(Stringaccount,Stringpassword){
Regist#"
returnthis.applyed;
}
服务器端的主界面截图:
图1服务器端初始界面
服务器端在启动服务器时创建套接字,并进行监听处理客户端的连接以及消息管理。
voidCE04Doc:
OnFileStart()
{
//TODO:
Addyourcommandhandlercodehere
CChannelDlgdlg;
if(dlg.DoModal()!
=IDOK)return;
if(m_listenSocket!
=NULL)
{
m_listenSocket->
Close();
deletem_listenSocket;
m_listenSocket=newCListenSocket(this);
if(!
m_listenSocket->
Create(atoi(dlg.m_channel)+BASE_CHANNEL))
AfxMessageBox("
创建Socket失败!
m_listenSocket=NULL;
return;
Listen())
Socket侦听异常!
POSITIONpos=m_list.GetHeadPosition();
while(pos!
CClientSocket*client=(CClientSocket*)m_list.GetNext(pos);
deleteclient;
AcceptClient()
m_clientSocket=newCClientSocket(this);
Accept(*m_clientSocket))
Socket接收出现异常!
deletem_clientSocket;
m_clientSocket=NULL;
#ifdef_DEBUG
AfxMessageBox("
有用户加入!
#endif
m_list.AddTail(m_clientSocket);
客户端主要界面:
图2客户端初始界面
客户端需要先连接服务器,选择聊天频道。
连接服务器的代码如下所示:
ReadMessage(CClientSocket*socket)
charbuffer[MAX_BUFFER_SIZE];
intlen=socket->
Receive(buffer,MAX_BUFFER_SIZE-1);
if(len<
1)
接收信息出现异常!
buffer[len]=0;
ShowMessage(socket,buffer);
SendMessage(socket,buffer);
ShowMessage(CClientSocket*socket,char*buffer)
POSITIONpos=GetFirstViewPosition();
CE04View*eView=(CE04View*)GetNextView(pos);
CEdit&
edit=eView->
GetEditCtrl();
CStringaddr;
UINTport;
socket->
GetPeerName(addr,port);
charmsg[MAX_BUFFER_SIZE];
wsprintf(msg,"
%d[%s]:
%s\r\n"
port,addr,buffer);
intlen=eView->
GetWindowTextLength();
edit.SetSel(len,len);
edit.ReplaceSel(msg);
SendMessage(CClientSocket*socket,char*buffer)
if(client==socket)continue;
client->
Send(buffer,strlen(buffer));
本机的ip地址是10.90.213.163
服务器启动过程如图:
图3启动服务
创建聊天频道,如下图:
图4创建聊天频道
启动客户端,与服务器连接,如下图
图5与服务器连接
客户端选择聊天频道,连接服务器,如下图
图6客户端与服务器连接
连接成功界面:
图7某一客户端连接成功
连接失败时,如下图
图8某一客户端连接失败
publicclassNewChatClientextendsJFrame{
//构成用户界面的全局变量有容器类和各种组件类
privateJPaneljContentPane=null;
privateJPaneljPanel_SysInfo=null;
privateJPaneljPanel_photo=null;
privateJLabeljLabel=null;
privateJLabeljLabel_account=null;
privateJPaneljPanel_state=null;
privateJPaneljPanel_friendTree=null;
privateJScrollPanejScrollPane=null;
privateJTextAreajTextArea_sysInfo=null;
//两个数据流处理的类输入和输出
DataOutputStreamdos=null;
DataInputStreamdis=null;
//Sockets用来进行连接的用户Socket
Sockets=null;
//用户的ID
StringID=null;
//用来内部线程RUN方法的启动和运行的布尔量
booleanrunning=false;
//用来在于服务器端进行同信时存储在线人员端口的变量
//协议格式:
当前在线#?
?
#?
#
Stringstr_Online="
"
;
//用来处理日期信息的日期变量
privateDatenow=null;
privateDateFormatdf=DateFormat.getDateTimeInstance();
//用来解决窗口匹配的问题,如我和你通信的窗口如果创建了就不创建新的窗口
Map<
String,ChatDialog>
ChatDialogs=newHashMap<
();
//登陆界面对话框
LoginDialogld=newLoginDialog(this);
//用来存储服务器端传回来的验证账号密码正确的布尔量
booleanverifyed=false;
//用来存储服务器端传回来的申请账号成功与否结果的布尔量
booleanapplyed=false;
//主函数
publicstaticvoidmain(String[]args){
//用户界面的启动方法
newNewChatClient().launcher();
//用来美化界面的代码段通过UI管理器来实现
UIManager
.setLookAndFeel("
com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
//UIManager.setLookAndFeel("
com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(InstantiationExceptione){
}catch(IllegalAccessExceptione){
}catch(UnsupportedLookAndFeelExceptione){
//启动方法由于我的界面是通过VE插件实现的所以通过这个方法来间接启动
publicvoidlauncher(){
getJFrame();
*ThismethodinitializesjFrame
*@returnjavax.swing.JFrame
privatevoidgetJFrame(){
this.setSize(newDimension(350,450));
this.setLocation(300,200);
this.setContentPane(getJContentPane());
this.setTitle("
NewChatClient"
JMenuBarjmb=newJMenuBar();
JMenu