java实验报告材料二.docx
《java实验报告材料二.docx》由会员分享,可在线阅读,更多相关《java实验报告材料二.docx(12页珍藏版)》请在冰点文库上搜索。
java实验报告材料二
《Java技术》实验报告
实验一:
2017年09月日
学院
计算机与信息学院
专业班级
姓名
成绩
课程
名称
Java技术
实验项目
名称
实验二基于GUI的网络通信程序设计
指导
教师
薛峰
教师评语
教师签名:
年月日
1、实验目的
1.掌握Java中GUI程序的编写,包括事件监听机制。
2.掌握Java的网络通信编程,ServerSocket,Socket类的使用。
3.掌握Java中多线程的编程,Thread类,Runnable接口的使用。
4.掌握用面向对象的方法分析和解决复杂问题。
二、实验原理
编写程序完成以下功能:
1.设计一个基于GUI的客户-服务器的通信应用程序,如图1,图2所示。
图1Socket通信服务器端界面
图2Socket通信客户端界面
2.图1为Socket通信服务器端界面,点击该界面中的【Start】按钮,启动服务器监听服务(在图1界面中间的多行文本区域显示“Serverstarting…”字样)。
图2为Socket通信客户端界面,点击该界面中的【Connect】按钮与服务器建立链接,并在图2所示界面中间的多行文本区域显示“Connecttoserver…”字样,当服务器端监听到客户端的连接后,在图1界面中间的多行文本区域追加一行“Clientconnected…”字样,并与客户端建立Socket连接。
3.当图1所示的服务器端和图2所示的客户机端建立Socket连接后,编程实现服务端、客户端之间的“单向通信”:
在客户端的输入界面发送消息,在服务端接收该消息,并将接收到对方的数据追加显示在多行文本框中。
三、使用硬件、软件环境
PC计算机一台,配置为CPU为1.5G,内存为512M,硬盘为40G,安装WindowsXP操作系统。
另外,使用eclipse,JDK1.6.0等软件
4、实验过程、步骤及原始记录(算法、原程序、测试结果,分析等)
1、先实现awt的界面编程,编写程序框,文字输入框,文本框等,并在按钮处插入actionlistener。
2、编写Listen1和Listen2来分别监听Start按钮和say按钮。
并在服务器与客户端各编写一个线程用来接收对方发来的信息。
服务器中的start创建serversocket然后等待客户端的start创建socket。
连接成功后两者分别开启接收信息的线程。
Say按钮按下时listen2发送信息去另一端口。
3、源代码
packagenet;
importjava.awt.*;
importjava.io.*;
importjava.awt.event.*;
import.*;
publicclassServerFrameextendsFrame{
publicServerFrame(Stringtitle){
super(title);
}
PanelnorthPanel,centerPanel,southPanel;
TextFieldtfport,tfsay,tfIP;
LabellabelPort,labelSay,labelIP;
ButtonbtStart,btSay;
TextAreatamsg;
ServerSockets=null;
Socketclient=null;
InputStreamis=null;
PrintWriteros=null;
Listen1ls1=newListen1();
Listen2ls2=newListen2();
BufferedReaderhear;
voidinitGUI(){
northPanel=newPanel();centerPanel=newPanel();southPanel=newPanel();
add(northPanel,"North");add(centerPanel,"Center");add(southPanel,"South");
btStart=newButton("start");btSay=newButton("say");
btStart.addActionListener(ls1);btSay.addActionListener(ls2);
labelSay=newLabel("Say");labelPort=newLabel("port");
tfport=newTextField("3000",60);tfsay=newTextField(60);
tamsg=newTextArea(16,75);
northPanel.add(labelPort);northPanel.add(tfport);northPanel.add(btStart);
centerPanel.add(tamsg);
southPanel.add(labelSay);southPanel.add(tfsay);southPanel.add(btSay);
pack();
setVisible(true);
addWindowListener(newWindowAdapter(){//Addsthespecifiedwindowlistenertoreceivewindoweventsfromthiswindow
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
}
publicstaticvoidmain(String[]args){
ServerFrameserver=newServerFrame("服务器");
server.initGUI();
}
classserverThreadextendsThread{
InputStreamis=null;
publicvoidrun(){
try{
is=client.getInputStream();
hear=newBufferedReader(newInputStreamReader(is));
tamsg.append(hear.readLine());
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
System.exit
(1);
}
}
}
classListen1implementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente){
tamsg.append("serverstarting");
try{
s=newServerSocket(3000);
tamsg.append("asdhasd");
}catch(IOExceptione1){
System.exit
(1);
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
try{
client=s.accept();
}catch(IOExceptione1){
System.exit
(1);
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
try{
os=newPrintWriter(client.getOutputStream(),true);
}catch(IOExceptione1){
System.exit
(1);
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
serverThreadis1=newserverThread();
is1.start();
}
}
classListen2implementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente){
if(tfsay.getText()!
=""){
tamsg.append(tfsay.getText()+'\n');
os.write(tfsay.getText()+'\n');
os.flush();
tfsay.setText("");
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------packagenet;
importjava.awt.*;
importjava.io.*;
importjava.awt.event.*;
import.*;
publicclassClientFrameextendsFrame{
publicClientFrame(Stringtitle){
super(title);
}
PanelnorthPanel,centerPanel,southPanel;
TextFieldtfport,tfsay,tfIP;
LabellabelPort,labelSay,labelIP;
ButtonbtStart,btSay;
TextAreatamsg;
Sockets;
PrintWriterout;
BufferedReaderin;
Listen1ls1=newListen1();
Listen2ls2=newListen2();
voidinitGUI(){
northPanel=newPanel();centerPanel=newPanel();southPanel=newPanel();
add(northPanel,"North");add(centerPanel,"Center");add(southPanel,"South");
btStart=newButton("Connect");btSay=newButton("say");
btStart.addActionListener(ls1);btSay.addActionListener(ls2);
labelSay=newLabel("Say");labelIP=newLabel("IP");labelPort=newLabel("port");
tfport=newTextField("3000",25);tfsay=newTextField(60);tfIP=newTextField("127.0.0.1",25);
tamsg=newTextArea(16,75);
northPanel.add(labelIP);northPanel.add(tfIP);
northPanel.add(labelPort);northPanel.add(tfport);northPanel.add(btStart);
centerPanel.add(tamsg);
southPanel.add(labelSay);southPanel.add(tfsay);southPanel.add(btSay);
pack();
setVisible(true);
addWindowListener(newWindowAdapter(){//Addsthespecifiedwindowlistenertoreceivewindoweventsfromthiswindow
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
}
publicstaticvoidmain(String[]args){
ClientFrameclient=newClientFrame("客户端");
client.initGUI();
}
classListen1implementsActionListener{
publicvoidactionPerformed(ActionEvente){
try{
Stringip=tfIP.getText();
intport=Integer.parseInt(tfport.getText());
s=newSocket(ip,port);
tamsg.append("Clientconnected");
ClientThreadcf=newClientThread();
cf.start();
}catch(Exceptionew){
System.exit
(1);
ew.getMessage();
}
}
}
classListen2implementsActionListener{
publicvoidactionPerformed(ActionEvente){
try{
out=newPrintWriter(s.getOutputStream());
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
System.exit
(1);
}
if(tfsay.getText()!
=""){
tamsg.append(tfsay.getText()+'\n');
out.write(tfsay.getText()+'\n');
out.flush();
tfsay.setText("");
}
}
}
classClientThreadextendsThread{
publicvoidrun(){
try{
in=newBufferedReader(newInputStreamReader(s.getInputStream()));
Strings=in.readLine();
while(s!
=null){
tamsg.append(s+"\n");
s=in.readLine();
}
}catch(Exceptionee){
}
}
}
}
五、实验结论、分析、思考题与心得体会
通过本次实验,我有以下几方面的收获、认识和体会:
1、学会了在三种环境下编写程序及运行程序的方法;
2、加深了Java中相关语法的认识:
在初次录入源程序时,我将public写成了Public,导致编译出错,结合语法我认识到Java是严格区分大小写的。
3、学会了socket的使用以及awt的界面实现。
4、在这个程序中要注意在输入后要清空对话框,不然就只能发送一条信息。
5、这两者的线程互不影响,所以只用考虑serversocket处可能产生的线程阻塞。