《聊天程序设计实验报告范文.docx》由会员分享,可在线阅读,更多相关《聊天程序设计实验报告范文.docx(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、聊天程序设计实验报告范文JAVA语言,聊天室实验报告某某专业名称实验室实验课程实验名称姓名学号同组人员实验日期某某某某大学软件工程2号实验楼201室JAVA高级程序设 计聊天程序设计(客户端、服务端)无2022年12月12日JAVA语言,聊天室实验报告一、实验目的掌握基于TCP、UDP协议的聊天服务器端与客户端程序设计,掌握 Socket,ServerSocket,DatagramSocket, DatagramPacket 类的使用。二、实验原理TCP和UDP协议的支持。ServerSocket和Socket的使用。三、实验内容实验截图JAVA语言,聊天室实验报告);jLabel3. etT
2、e 某 t (对方 IP: );jLabel3. etToolTipTe 某 t ();java 某.wing. GroupLayoutjPanel3Layout=newjava 某 wing. GroupLayout (jPane13);jPanel3. etLayout(jPanel3Layout);jPanel3Layout. etHorizontalGroup(jPanel3Layout. createParallelGroup(java 某. wing GroupLayout. Alignment. LEADING).addGroup (jPanel3Layout. createSe
3、quentialGroup() addContainerGap() addPreferredGap(ponentPlacement. UNRELATED)java 某, wing. GroupLayout. PREFERRED SIZE,131, java 某.wing. GroupLayout. PREFERRED_SIZE).addContainerGap(java某.wing. GroupLayout. DEFAULT_SIZE, Short. MA 某_VALUE)jPanel3Layout. etVerticalGroup(jPanel3Layout. createParallelG
4、roup(java 某 wing. GroupLayout. Alignment. LEADING) addGroup (jPane13Layout. createSequentialGroup(). addGap(22, 22, 22) addGroup(jPanel3Layout. createParallelGroup(java 某 wing. GroupLayout. Alignment. BASELINE)java 某.wing. GroupLayout. PREFERRED_SIZE, java某.wing. GroupLayout. DEFAULT SIZE, java某 win
5、g. GroupLayout. PREFERRED SIZE).addContainerGap(java某.wing. GroupLayout. DEFAULT_SIZE, Short. MA 某_VALUE);jTe 某 tAreal. etEditable (fale);jTe 某 tAreal. etColumn (20);JAVA语言,聊天室实验报告jTe 某 tAreal. etFont (newjava. awt. Font (微软雅黑,0,14);/N0I18NjTe 某 tAreal. etLineWrap(true);jTe 某 tAreal. etRow(5);jScrol
6、lPane3. etViewportView(jTe 某 tAreal);java 某 wing. GroupLayoutlayout=newjava 某.wing. GroupLayout(getContentPane();getContentPane(). etLayout(layout);layout. etHorizontalGroup(layout. createParallelGroup(java某 wing. GroupLayout. Alignment. LEADING). addGroup (java某 wing. GroupLayout. Alignment. TRAILI
7、NG,layout. createSequentialGroup().addGroup(layout. createParallelGroup(java某 wing. GroupLayout. Alignment. LEADING) addGroup(java某 wing. GroupLayout. Alignment. TRAILING, layout. createSequentialGr oup().addGap (2, 2, 2).addPreferredGap(ponentPlacement. RELATED)java 某.wing. GroupLayout. PREFERRED S
8、IZE,104,java 某.wing. GroupLayout. PREFERRED SIZE) addPreferredGap(ponentPlacement. RELATED)java 某 wing. GroupLayout. PREFERRED SIZE, 6, java 某, wing. GroupLayout. PREFERRED SIZE) addPreferredGap(ponentPlacement. RELATED)java 某.wing. GroupLayout. PREFERRED SIZE, 43,java 某 wing. GroupLayout. PREFERRED
9、_SIZE) addPreferredGap(ponentPlacement. RELATED, 123, Short. MA 某 _VALUE)addPreferredGap(ponentPlacement. RELATED)java 某.wing. GroupLayout. PREFERRED_SIZE)JAVA语言,聊天室实验报告 addContainerGap ();layout. etVerticalGroup(layout. createParallelGroup(java某 wing. GroupLayout. Alignment. LEADING). addGroup(layo
10、ut. createSeq uentialGroup() addGroup(layout. createParallelGroup(java 某.wing. GroupLayout. Alignment. LEADING, fale)java 某 wing. GroupLayout. DEFAULT_SIZE, java 某.wing. GroupLayout. DEFAULT_SIZE, Short. MA 某_VALUE)java 某.wing. GroupLayout. DEFAULT SIZE, 358, Short. MA 某_VALUE) addPreferredGap(ponen
11、tPlacement. RELATED)java 某.wing. GroupLayout. DEFAULT_SIZE, 131, Short. MA 某_VALUE) addPreferredGap(ponentPlacement. RELATED) addGroup(layout. createParallelGroup(java某 wing. GroupLayout. Alignment. BASELINE)java 某 wing. GroupLayout. PREFERRED SIZE, java某 wing. GroupLayout. DEFAULT_SIZE, java某.wing.
12、 GroupLayout. PREFERRED SIZE)java 某 wing. GroupLayout. PREFERRED SIZE, java某 wing GroupLayout. DEFAULT SIZE, java某. wing. GroupLayout. PREFERRED SIZE). addGap (2, 2, 2);pack();/addMeage(chatlpAddre, jTe 某 tArea2. getTe 某 t (), true);jTe 某 tArea2. etTe 某 t ();if(evt. getStateChange ()=1) JAVA语言,聊天室实验
13、报告 chatIpAddre=evt. getltem(). toStringO ;reChatWindow(chatIpAddre);privatePoint;/某某某/publictaticvoidmain(Stringarg口)(try for (java 某 wing. UIManager. LookAndFeellnfoinfo:java 某, wing. UIManager. getlntalledLookAndFeel () if(Nimbu. equal(info. getName()java 某.wing. UIManager. etLookAndFeel(info. get
14、ClaName();break;)catch(ClaNotFoundE 某 ceptione 某)java. util, logging. Logger. getLogger(TalkHome. cla. getName(). 1 og(java. util, logging. Level. SEVERE, null, e 某);catch(IntantiationE 某 ceptione 某)java. util, logging. Logger. getLogger(TalkHome. cla. getName (). 1 og(java. util, logging. Level. SE
15、VERE, null, e 某);catch(IllegalAcceE 某 ceptione 某)java. util, logging. Logger. getLogger (TalkHome. cla. getName ().1 og(java. util, logging. Level. SEVERE, null, e 某); catch (java 某 wing. UnupportedLookAndFeelE 某 ceptione 某)java. util, logging. Logger. getLogger(TalkHome. cla. getName(). 1 og(java.
16、util, logging. Level. SEVERE, null, e 某);/某 Createanddiplaytheform 某/java. awt. EventQueue. invokeLater(newRunnable () publicvoidrun() newTalkHome (). etViible (true););JAVA语言,聊天室实验报告)/Variabledeclaration-donotmodify private java 某 wing. JButton jButton2;private java 某 wing. JCheckBo 某 jCheckBo 某 1;
17、private java 某 wing. JFormattedTe 某 tFieldjFormattedTe 某 tFieldl;private java 某.wing. JFormattedTe 某 tFieldjFormattedTe 某 tField2;privatejava 某.wing. JLabel jLabell;private java 某 wing. JLabel jLabel2;private java 某.wing. JLabel jLabel3;private java 某.wing. JPanel jPanel3;private java 某 wing. JScrol
18、lPane jScrollPane2;private java 某.wing. JScrollPane jScrollPane3;private java 某.wing. JTe 某 tAreajTe 某 tAreal;private java 某, wing. JTe 某 tAreajTe 某 tArea2;clalMeagepublicbooleanimyelf;publicStringmeString;publicIMeage(Stringme, booleanimyelf) thi. imyelf=imyelf;thi.meString=me;实验心得 其实java已经将TCP和UDP
19、协议高度封装了,熟练使用ServerSocket和Socket, DataGram会写出想要的网络程序,并不是很难。 实验中遇到的问题有传送过来的数据显示成乱码,是因为编码的问题。JAVA语言,聊天室实验报告代码importjava. awt. Point;importjava. io. BufferedReader;importjava. io. I0E 某 ception;importjava. io. InputStreamReader;importjava. io. PrintWriter;import. ServerSocket;import. Socket;importjava,
20、te 某 t. DateFormat;importjava. util. Date;importjava. util. HahMap;importjava. util. Vector;importjava. util, logging. Level;importjava. util, logging. Logger;/某某某某/publicclae 某 tendjava 某 wing. JFrame privateServerSocketerver;privateint=O;privatecharchatBuf=newchar2048;pr i vat eStr ingchatIpAddre=
21、zz 127. 0. 0. 1;privatefinalintchatPort=5200;privateHahMapString, VectorchatRecordMap;/privateHahMapchatRecordHMap;publicTalkHome()chatRecordMap=newHahMapString, Vector ();/chatRecordHMap=newHahMap ();try erver=newServerSocket(Integer. valueOf (chatPort); catch(I0E 某 ceptione 某)Logger. getLogger(Tal
22、kHome. cla. getName(). log(Level. SEVERE, null, e 某);)newThread(newRunnable ()publicvoidrun() while (true) try (Socketocket=erver. accept ();JAVA语言,聊天室实验报告BufferedReaderin=newBufferedReader (newInputStreamReader(ocket. getInputStream();inttmp_len=-l;StringBuiIderchatStrBuilder=newStringBuiIder ();wh
23、ile(tmp_len=in. read(chatBuf)!=-1) chatStrBuilder. append(chatBuf, 0, tmp_len);)addMeage(ocket. getlnetAddre(). getHotAddre (),chatStrBuilder. toStringO, fale);in. cloe ();ocket. cloe (); catch (I0E 某 ceptione 某)Logger. getLogger(TalkHome. cla. getName(). log(Level. SEVERE,nu11, e 某);). tart ();priv
24、atevoidreChatWindow(StringipAddre) Sytem. out. printIn (ipAddre);VectorchatRecord=chatRecordMap. get(ipAddre);jTe 某 tAreal. etTe 某 t ();if (chatRecord=null) chatRecord=newVector ();chatRecordMap. put(ipAddre, chatRecord);ele for(IMeageiMe:chatRecord) if (iMe. imyelf) jTe 某 tAreal. append (我+iMe date
25、String+z/)ele jTe 某 tAreal. append (TA+iMe. dateString+/z privatevoidaddMeage(finalStringipAddre, StringmeString, boolea imyelf)VectorchatRecord=chatRecordMap. get (ipAddre);if (chatRecord=null) JAVA语言,聊天室实验报告chatRecord=newVector ();chatRecordMap. put(ipAddre, chatRecord);finalIMeageiMeage=newIMeage
26、(meString, imyelf);chatRecord. add(iMeage);if(imyelf) newThread(newRunnable ()publicvoidrun() try (SocketendMeage=newSocket(ipAddre, chatPort);PrintWriterendWindow=newPrintWriter(endMeage. getOutputStream();endWindow. print(iMeage. meString);endWindow. f luh ();endWindow. cloe ();endMeage. cloe ();j
27、Te 某 tAreal, append(我+iMeage dateString+/z catch(I0E 某 ceptione 某)Logger. getLogger (TalkHome. cla. getName(). log(LeveL SEVERE, nu n,e 某);). tart ();)ele jTe 某 tAreal, append(TA+iMeage. dateString+/z jCheckBo 某 l=newjava 某 wing. JCheckBo 某();jScrollPane2=newjava 某.wing. JScrollPane ();jTe 某 tArea2=
28、newjava 某 wing. JTe 某 tAreaO ;jButton2=newjava 某 wing. JButton ();jFormattedTe 某 tFieldl=newjava 某 wing. JFormattedTe 某 tField ();jFormattedTe 某 tField2=newjava 某.wing. JFormattedTe 某 tField ();jLabel l=newjava 某.wing. JLabel ();jLabel2=newjava 某 wing. JLabel ();JAVA语言,聊天室实验报告jPanel3=newjava 某, wing
29、. JPanel ();jLabel3=newjava 某 wing. JLabel ();jScrollPane3=newjava 某 wing. JScrollPane ();jTe 某 tAreal=newjava 某.wing. JTe 某 tAreaO ;jCheckBo 某 1. etTe 某 t(jCheckBo 某 1);etDefaultCloeOperation (java 某 wing. WindowContant. E 某IT ON CLOSE);etBackground(newjava. awt. Color(255, 255, 255);etBound (newja
30、va. awt. Rectangle (0, 0, 520, 520);etCuror(newjava. awt. Curor(java. awt. Curor. DEFAULT_CURSOR);etModalE 某 cluionType(java. awt. Dialog. ModalE 某 cluionType. APPLICATION_E 某 CLUDE);jTe 某 tArea2. etColumn(20);jTe 某 tArea2. etFont (newjava. awt. Font (微软雅黑 ”,0,14);/N0I18NjTe 某 tArea2. etLineWrap(tru
31、e);jTe 某 tArea2. etRow(5);jScrollPane2. etViewportView(jTe 某 tArea2);jButton2. etTe 某 t (发送);jButton2. addActionLitener(newjava. awt. event. ActionLitener () publicvoidactionPerformed(java. awt. event. ActionEventevt)jButton 2ActionPerformed (evt););jFormattedTe 某 tFieldl. etTe 某 t (127.0.0.1);jFormattedTe 某 tFieldl. etToolTipTe 某 t();jFormattedTe 某 tField2.etTe 某 t(5200);jFormattedTe 某 tField2. etToolTipTe 某 t();jLabell. etTe 某 t(IP:);jLabel2. etTe 某 t(:);jPanel3. etBackground(newjava. awt. Color (255, 255,255);