Java语言实验报告1Java语言实验报告实验环境:Eclipse6.0环境实验目的:1、了解文件和流的概念,字节的输入输出,掌握文件的操作;2、了解网络通信,掌握TCPSocket通信和TCPSocket的工作方式。实验原理:1、采用TCPSocket通信是实现的点对点聊天,ChatServer类提供服务端的TCP的链接服务,SingleTalkClient类与SingleTalkServer类实现客户端与服务端的TCPSocket通信以及两者的图形界面;2、Socket对象用于在服务端和客户端的TCP链接之间进行发送和接受的双向数据通信,从Socket对象中能够获得字节输入流和字节输出流。实验内容:实现一对一的聊天程序1、分析需求,确定程序主要框架、所需要的编程语言、以及开发环境;2、研究一对一聊天程序的原理及主要功能;3、根据实验要求编写聊天界面及聊天程序;4、完成编码,进行测试;5、完成测试,完善代码。实验步骤:一、创建客户端界面packagejiemian;importjava.io.*;importjava.net.*;importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;/***用户界面SingleTalkClient*/publicclassSingleTalkClientextendsJFrame{privatestaticfinallongserialVersionUID=3774711323397279594L;JTextAreamainArea;JTextAreasendArea;ChatClientclient;JTextFieldipArea;Java语言实验报告2JTextFieldnameArea;JButtonbuttonLink;publicvoidsetClient(ChatClientclient){this.client=client;}publicSingleTalkClient(){super(用户);Containercontain=getContentPane();contain.setLayout(newBorderLayout());mainArea=newJTextArea();JScrollPanemainAreaP=newJScrollPane(mainArea);//为文本区添加滚动条JPanelpanel=newJPanel();panel.setLayout(newFlowLayout());sendArea=newJTextArea(3,30);JButtonsendBtn=newJButton(发送);JButtonleave=newJButton(离线);JButtonsentMessage=newJButton(发送);panel.add(leave);sendBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventae){client.sendMsg(sendArea.getText());mainArea.append(【用户】+sendArea.getText()+\n);sendArea.setText();}});JPanelipPanel=newJPanel();ipPanel.setLayout(newFlowLayout(FlowLayout.LEFT,10,10));ipPanel.add(newJLabel(服务器:));JPanelnamePanel=newJPanel();namePanel.setLayout(newFlowLayout(FlowLayout.LEFT,10,10));namePanel.add(newJLabel(昵称:));nameArea=newJTextField(4);nameArea.setText();namePanel.add(nameArea);ipArea=newJTextField(12);ipArea.setText(127.0.0.1);Java语言实验报告3ipPanel.add(ipArea);buttonLink=newJButton(连接);ipPanel.add(buttonLink);buttonLink.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventae){client=newChatClient(ipArea.getText(),8888,SingleTalkClient.this);//创建socket对象SingleTalkClient.this.setClient(client);}});panel.add(sendBtn,BorderLayout.EAST);panel.add(sendArea,BorderLayout.CENTER);contain.add(ipPanel,BorderLayout.NORTH);contain.add(namePanel,BorderLayout.WEST);contain.add(mainAreaP,BorderLayout.CENTER);contain.add(panel,BorderLayout.SOUTH);setSize(500,300);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicstaticvoidmain(String[]args){SingleTalkClientui=newSingleTalkClient();}}/***通讯类ChatClient负责守候数据到来*/classChatClientextendsThread{Socketserver;//对象server,用来处理与服务BufferedReaderin;//声明输入流缓冲区,用于存储服务器发来的信息PrintWriterout;//声明打印输出流,用于信息的发送SingleTalkClientui;publicChatClient(Stringip,intport,SingleTalkClientui){//初始化ChatClient类Java语言实验报告4this.ui=ui;try{server=newSocket(ip,port);//创建server,用服务器ip和端口作参数System.out.println(已顺利联接到服务器。);out=newPrintWriter(server.getOutputStream(),true);in=newBufferedReader(newInputStreamReader(server.getInputStream()));}catch(Exceptione){System.out.println(e);}start();}publicvoidrun(){//用于监听服务器端发送来的信息Stringmessaga=;while(true){try{messaga=in.readLine();//从缓冲区读入一行字符存于message}catch(SocketExceptionex){System.out.println(ex);break;}catch(Exceptionex){System.out.println(ex);}if(messaga!=null&&messaga.trim()!=){//若message信息不为空System.out.println(+messaga);ui.mainArea.append(messaga+\n);//把message信息添加到客户端的文本区域内}}}publicvoidsendMsg(Stringmsg){//用于发送信息try{out.println(【用户】+msg);}catch(Exceptione){System.out.println(e);}}}二、创建服务端界面Java语言实验报告5packagejiemian;importjava.io.*;importjava.net.*;importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;/***服务端界面SingleTalkServer*/publicclassSingleTalkServerextendsJFrame{privatestaticfinallongserialVersionUID=1L;JTextAreamainArea;JTextAreasendArea;JTextFieldindexArea;ServerComserver;publicvoidsetServer(ServerComserver){this.server=server;}publicSingleTalkServer(){super(服务器端);Containercontain=getContentPane();contain.setLayout(newBorderLayout());mainArea=newJTextArea();JScrollPanemainAreaP=newJScrollPane(mainArea);JPanelpanel=newJPanel();panel.setLayout(newBorderLayout());sendArea=newJTextArea(3,8);JButtonsendBtn=newJButton(发送);sendBtn.addActionListener(newActionListener()//注册动作监听器{Java语言实验报告6publicvoidactionPerformed(ActionEventae){server.sendMsg(sendArea.getText());//把信息传递到客户端mainArea.append(【服务器】+sendArea.getText()+\n);//把信息显示在服务器的聊天记录区域indexArea.setText(0);}});JPaneltmpPanel=newJPanel();indexArea=newJTextField(2);indexArea.setText(0);tmpPanel.add(sendBtn);tmpPanel.add(indexArea);panel.add(tmpPanel,BorderLayout.EAST);panel.add(sendArea,BorderLayout.CENTER);contain.add(mainAreaP,BorderLayout.CENTER);contain.add(panel,BorderLayout.SOUTH);setSize(500,300);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicstaticvoidmain(String[]args){SingleTalkServerui=newSingleTalkServer();ServerComserver=newServerCom(ui);//创建并启动网络通讯线程,准备接受客户端数据包}}/***通讯类ServerCom负责守候数据到来*/classServerComextendsThread//网络通讯类{Socketclient;ServerSocketsoc;BufferedReaderin;PrintWriterout;SingleTalkServerui;Java语言实验报告7//ChatServerserver;publicServ