完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx

上传人:b****6 文档编号:20645231 上传时间:2023-01-24 格式:DOCX 页数:21 大小:130.06KB
下载 相关 举报
完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx_第1页
第1页 / 共21页
完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx_第2页
第2页 / 共21页
完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx_第3页
第3页 / 共21页
完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx_第4页
第4页 / 共21页
完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx

《完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx(21页珍藏版)》请在冰豆网上搜索。

完整计算机网络TCP聊天室实验报告Word文档下载推荐.docx

IOException;

publicclassClientApp{

publicstaticvoidmain(String[]args){

/**

*javax.swing.UIManager$LookAndFeelInfo[Metaljavax。

swing。

plaf。

metal。

MetalLookAndFeel]

javax。

UIManager$LookAndFeelInfo[Nimbuscom.sun.java.swing。

plaf.nimbus。

NimbusLookAndFeel]

swing.UIManager$LookAndFeelInfo[CDE/Motifcom.sun.java.swing.plaf。

motif。

MotifLookAndFeel]

javax.swing.UIManager$LookAndFeelInfo[Windowscom.sun。

java。

plaf.windows。

WindowsLookAndFeel]

javax.swing。

UIManager$LookAndFeelInfo[WindowsClassiccom。

sun.java。

windows。

WindowsClassicLookAndFeel]

*@authorAdministrator

*/

try{

//使用Nimbus皮肤

UIManager.setLookAndFeel("

com。

sun。

swing.plaf。

nimbus。

NimbusLookAndFeel"

);

//localhost或127.0.0.1

ClientFramefrClientFrame=newClientFrame("

localhost”,Server。

CHAT_ROOM_PORT);

frClientFrame。

setConnected(true);

setVisible(true);

//如果登录成功

if(frClientFrame.login(”222”)){

frClientFrame。

receiveMessage();

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(InstantiationExceptione){

e.printStackTrace();

}catch(IllegalAccessExceptione){

e。

printStackTrace();

}catch(UnsupportedLookAndFeelExceptione){

printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

packageTCP_chat_room.action;

importjava.awt。

AWTException;

publicclassClientFrameActionextendsWindowAdapter{

privateClientFramefrClientFrame;

publicClientFrameAction(ClientFramefrClientFrame){

this.frClientFrame=frClientFrame;

publicvoidwindowIconified(WindowEvente){

if(SystemTray.isSupported()){

finalSystemTraysystem=SystemTray.getSystemTray();

Imageimage=ImageManager.getImage(”piScreen。

gif"

finalStringtoolTip=newString("

大家聊天室”);

finalPopupMenupopup=newPopupMenu();

finalMenuItemmiRevert=newMenuItem(”Restore"

finalMenuItemmiAddFriends=newMenuItem("

AddFriends"

);

finalMenuItemmiRemoveFriends=newMenuItem("

RemoveFriends"

finalMenuItemmiModifyFriendsInfo=newMenuItem("

ModifyFriendsInfomation”);

finalMenuItemmiSearch=newMenuItem("

SearchFriends"

finalMenuItemmiExit=newMenuItem(”Exit"

popup.add(miRevert);

popup.addSeparator();

popup。

add(miAddFriends);

popup.add(miRemoveFriends);

add(miModifyFriendsInfo);

popup.add(miSearch);

addSeparator();

add(miExit);

finalTrayIconicon=newTrayIcon(image,toolTip,popup);

try{

system.add(icon);

frClientFrame.setVisible(false);

}catch(AWTExceptione1){

System。

out.print(”软件在系统通知区域显示出错"

e1.printStackTrace();

miRevert。

addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

system。

remove(icon);

frClientFrame。

setVisible(true);

frClientFrame.setState(JFrame。

NORMAL);

});

miAddFriends。

addActionListener(newActionListener(){

frClientFrame.setVisible(true);

setState(JFrame。

NORMAL);

miExit。

windowClosing(null);

});

//不知何用

icon。

addActionListener(miRevert.getActionListeners()[0]);

publicvoidwindowClosing(WindowEvente){

intresult=MessageBox。

showConfirmMessage(”您确定要退出聊天系统吗?

”);

if(result==JOptionPane.YES_OPTION){

disconnect();

setVisible(false);

dispose();

//System.exit(0);

}else{

//donothing

packageTCP_chat_room。

gui;

importjava。

awt.Color;

publicclassClientFrameextendsJFrame{

privatebooleanconnected=false;

privateintport;

privateStringhost;

privateSockets;

privateDataInputStreamdis;

privateDataOutputStreamdos;

publicDataInputStreamgetDis(){

returndis;

publicDataOutputStreamgetDos(){

returndos;

privatefinalJPanelpanel=newJPanel();

privatefinalJPanelpnlChatArea=newJPanel();

privatefinalImageIconimageSetting=ImageManager。

getIcon("

setting。

privatefinalJLabellblSettingImage=newJLabel(imageSetting);

privatefinalJPanelpnlSetting=newJPanel();

privatefinalJPanelpnlInputArea=newJPanel();

privatefinalJButtonbnClose=newJButton("

关闭”);

privatefinalJButtonbnSend=newJButton(”发送"

privatefinalJTextAreatxtChatArea=newJTextArea(6,15);

privatefinalJTextAreatxtInputArea=newJTextArea(2,15);

privatefinalDimensionminDimension=newDimension(300,413);

publicbooleanisConnected(){

returnconnected;

publicvoidsetConnected(booleanconnected){

this。

connected=connected;

publicJTextAreagetTxtChatArea(){

returntxtChatArea;

publicJTextAreagetTxtInputArea(){

returntxtInputArea;

publicClientFrame(Stringhost,intport)throwsIOException{

host=host;

port=port;

s=newSocket(host,port);

dos=newDataOutputStream(s.getOutputStream());

dis=newDataInputStream(s.getInputStream());

initComponents();

privatevoidinitComponents(){

this.setTitle("

大家聊天室”);

this.setSize(minDimension);

setDefaultCloseOperation(JFrame。

DO_NOTHING_ON_CLOSE);

getContentPane().setLayout(null);

panel。

setBounds(0,0,292,379);

getContentPane()。

setBackground(Color。

LIGHT_GRAY);

add(panel);

setLayout(null);

pnlChatArea.setBounds(0,0,292,226);

JScrollPanejspChatArea=newJScrollPane(txtChatArea);

jspChatArea.setAutoscrolls(false);

pnlChatArea。

add(jspChatArea);

panel.add(pnlChatArea);

setLayout(null);

txtChatArea。

setLineWrap(true);

txtChatArea.setEditable(false);

//pnlChatArea。

setBounds(10,10,272,206);

jspChatArea.setBounds(10,10,272,206);

pnlSetting.setBounds(0,235,292,45);

pnlSetting。

add(lblSettingImage);

add(pnlSetting);

pnlInputArea。

setBounds(0,279,292,67);

JScrollPanejspInputArea=newJScrollPane(txtInputArea);

pnlInputArea.add(jspInputArea);

panel.add(pnlInputArea);

pnlInputArea.setLayout(null);

txtInputArea。

setLineWrap(true);

//pnlInputArea。

setBounds(10,10,272,47);

jspInputArea。

setBounds(10,10,272,47);

bnClose.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

//按下关闭按钮则直接退出聊天室

exitSystem();

});

bnClose。

setBounds(99,356,74,23);

add(bnClose);

bnSend。

addActionListener(newSendActionListener(this));

bnSend.setBounds(197,356,74,23);

bnSend.setMnemonic(0);

panel.add(bnSend);

setMinimumSize(minDimension);

this.addWindowListener(newClientFrameAction(this));

//this。

getRootPane().setDefaultButton(bnSend);

publicbooleanlogin(Stringnickname){

try{

dos。

writeUTF(”login_”+nickname);

intloginResult=dis.readInt();

if(loginResult==1){

out。

println("

登陆成功,欢迎进入聊天室!

"

returntrue;

}elseif(loginResult==-1){

登陆失败,没有以login_开头”);

returnfalse;

}elseif(loginResult==2){

System.out。

println(”登陆失败,用户昵称已经存在!

”);

}catch(IOExceptione){

e.printStackTrace();

returnfalse;

privateclassSendActionListenerimplementsActionListener{

privateClientFramefrClientFrame;

publicSendActionListener(ClientFramefrClientFrame){

this。

frClientFrame=frClientFrame;

publicvoidactionPerformed(ActionEvente){

Stringsaying=frClientFrame.getTxtInputArea()。

getText();

//System。

out.println(saying);

frClientFrame.getTxtInputArea().setText(null);

frClientFrame.getDos().writeUTF(saying);

frClientFrame.getDos()。

flush();

}catch(IOExceptione1){

}

publicvoidexitSystem(){

newClientFrameAction(this).windowClosing(null);

publicvoiddisconnect(){

this.setConnected(false);

close();

dis.close();

s.close();

}catch(IOExceptione3){

e3。

publicvoidreceiveMessage(){

newThread(newThreadreceiveMessage(this)).start();

privateclassThreadreceiveMessageimplementsRunnable{

publicThreadreceiveMessage(ClientFramefrClientFrame){

frClientFrame=frClientFrame;

Stringmessage=”"

publicvoidrun(){

try{

while(isConnected()){

message=dis.readUTF();

System.out.println(message);

frClientFrame。

txtChatArea.append(message+"

\n"

Thread.sleep(100);

}catch(IOExceptione){

e.printStackTrace();

}catch(InterruptedExceptione){

Thread。

interrupted();

importjavax.swing。

JOptionPa

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 纺织轻工业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1