JAVA开发进阶实验报告模板.docx

上传人:b****5 文档编号:7773342 上传时间:2023-01-26 格式:DOCX 页数:15 大小:266.40KB
下载 相关 举报
JAVA开发进阶实验报告模板.docx_第1页
第1页 / 共15页
JAVA开发进阶实验报告模板.docx_第2页
第2页 / 共15页
JAVA开发进阶实验报告模板.docx_第3页
第3页 / 共15页
JAVA开发进阶实验报告模板.docx_第4页
第4页 / 共15页
JAVA开发进阶实验报告模板.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

JAVA开发进阶实验报告模板.docx

《JAVA开发进阶实验报告模板.docx》由会员分享,可在线阅读,更多相关《JAVA开发进阶实验报告模板.docx(15页珍藏版)》请在冰豆网上搜索。

JAVA开发进阶实验报告模板.docx

JAVA开发进阶实验报告模板

《java开发进阶》实验报告

实验序号:

4实验项目名称:

套接字通信

学  号

姓  名

何征

专业、班

12软件工程

实验地点

实1-518

指导教师

唐朝辉

实验时间

2013.3.26

一、实验目的及要求

了解套接字通信并且熟悉输入流、输出流得操作同时掌握套接字编程

二、实验设备(环境)及要求

windows操作系统;

EclipsePC计算机

三、实验内容与步骤

1.请看例子4-2。

2.请看例子4-4。

四、实验结果与数据处理

 

五、分析与讨论

编写的过程当中,客户端和服务端经常出现系统崩溃,后来才发现是编写的代码出现了问题,导致使用不了,后来在努力查找下终于实现了运行。

第二个实验也是,比较多的问题都是代码出错,表示写得有点纠结,不过之后经过修改就成功了。

发现细心很重要。

六、教师评语

签名:

日期:

成绩

列出程序代码:

(1)、

importjava.io.*;

import.*;

import.Socket;

publicclassServerThreadextendsThread{

privateSocketconnectToClient;

privateDataInputStreaminFromClient;

privateDataOutputStreamoutToClient;

publicServerThread(Socketsocket)throwsIOException{

super();

connectToClient=socket;

inFromClient=newDataInputStream(connectToClient.getInputStream());

outToClient=newDataOutputStream(connectToClient.getOutputStream());

start();

}

publicvoidrun(){

try{

Stringstr;

doubleradius,area;

booleangoon=true;

while(goon){

str=inFromClient.readUTF();

if(!

str.equals("bye")){

radius=Double.parseDouble(str);

System.out.println("接收到得半径值为:

"+radius);

area=radius*radius*Math.PI;

str=Double.toString(area);

outToClient.writeUTF(str);

outToClient.flush();

System.out.println("圆面积"+str+"已经发送");

}else{

goon=false;

outToClient.writeUTF("bye");

outToClient.flush();

}

}

inFromClient.close();

outToClient.close();

connectToClient.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

(2)

importjava.io.*;

import.*;

publicclassMultiServer{

publicstaticvoidmain(String[]args){

try{

System.out.println("等待连接");

ServerSocketserverSocket=newServerSocket(3500);

SocketconnectToClient=null;

while(true){

connectToClient=serverSocket.accept();

newServerThread(connectToClient);

}

}catch(IOExceptione){

e.printStackTrace();

}

}

}

(3)

importjava.io.*;

import.*;

import.Socket;

publicclassClient{

publicstaticvoidmain(String[]args){

try{

SocketconnectToServer=newSocket("localhost",200);

DataInputStreaminFromServer=newDataInputStream(connectToServer.getInputStream());

DataOutputStreamoutToServer=newDataOutputStream(connectToServer.getOutputStream());

StringoutStr,inStr;

booleangoon=true;

BufferedReaderbuf=newBufferedReader(newInputStreamReader(System.in));

while(goon){

outStr=buf.readLine();

outToServer.writeUTF(outStr);

outToServer.flush();

inStr=inFromServer.readUTF();

if(!

inStr.equals("bye"))

System.out.println("从服务器返回的结果是"+inStr);

else

goon=false;

}

inFromServer.close();

outToServer.close();

connectToServer.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

(3)

importjava.io.*;

import.*;

import.Socket;

publicclassClient{

publicstaticvoidmain(String[]args){

try{

SocketconnectToServer=newSocket("localhost",);

DataInputStreaminFromServer=newDataInputStream(connectToServer.getInputStream());

DataOutputStreamoutToServer=newDataOutputStream(connectToServer.getOutputStream());

System.out.println("输入半径数值发送到服务器,输入bye结束。

");

StringoutStr,inStr;

booleangoon=true;

BufferedReaderbuf=newBufferedReader(newInputStreamReader(System.in));

while(goon){

outStr=buf.readLine();

outToServer.writeUTF(outStr);

outToServer.flush();

inStr=inFromServer.readUTF();

if(!

inStr.equals("bye"))

System.out.println("从服务器返回的结果是"+inStr);

else

goon=false;

}

inFromServer.close();

outToServer.close();

connectToServer.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

(1)、importjava.io.*;

import.*;

importjava.awt.*;

importjava.awt.Container;

importjava.awt.FlowLayout;

importjava.awt.event.*;

importjavax.swing.*;

publicclasschatServerimplementsActionListener,Runnable{

JTextAreashowArea;

JTextFieldmsgText;

JFramemainJframe;

JButtonsentBtn;

JScrollPaneJSPane;

JPanelpane;

Containercon;

Threadthread=null;

ServerSocketserverSocket;

SocketconnectToClient;

DataInputStreaminFromClient;

DataOutputStreamoutToClient;

publicchatServer(){

mainJframe=newJFrame("聊天——服务器端");

con=mainJframe.getContentPane();

showArea=newJTextArea();

showArea.setEditable(false);

showArea.setLineWrap(true);

JSPane=newJScrollPane(showArea);

msgText.setColumns(30);

msgText.addActionListener(this);

sentBtn=newJButton("发送");

sentBtn.addActionListener(this);

pane=newJPanel();

pane.setLayout(newFlowLayout());

pane.add(msgText);

pane.add(sentBtn);

con.add(JSPane,BorderLayout.CENTER);

con.add(pane,BorderLayout.SOUTH);

mainJframe.setSize(500,400);

mainJframe.setVisible(true);

mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try{

serverSocket=newServerSocket(5500);

showArea.append("正在等待对话请求\n");

connectToClient=serverSocket.accept();

inFromClient=newDataInputStream(connectToClient.getInputStream());

outToClient=newDataOutputStream(connectToClient.getOutputStream());

thread=newThread(this);

thread.setPriority(Thread.MIN_PRIORITY);

thread.start();

}catch(IOExceptione){

showArea.append("对不起,不能创建服务器\n");

msgText.setEditable(false);

sentBtn.setEnabled(false);

}

}

publicstaticvoidmain(String[]args){

newchatServer();

}

publicvoidactionPerformed(ActionEvente){

Strings=msgText.getText();

if(s.length()>0){

try{

outToClient.writeUTF(s);

outToClient.flush();

showArea.append("我说:

"+msgText.getText()+"\n");

}catch(IOExceptione1){

showArea.append("你的消息:

”"+msgText.getText()+"“未能发送出去\n");

}

}

}

publicvoidrun(){

try{

while(true){

showArea.append("对方说:

"+inFromClient.readUTF()+"\n");

Thread.sleep(1000);

}

}catch(IOExceptionel){

}catch(InterruptedExceptione){}

}

}

实验二

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

import.*;

importjavax.swing.*;

publicclasschatClientimplementsActionListener,Runnable{

JTextAreashowArea;

JTextFieldmsgText;

JFramemainJframe;

JButtonsentBtn;

JScrollPaneJSPane;

JPanelpane;

Containercon;

Threadthread=null;

SocketconnectToServer;

DataInputStreaminFromServer;

DataOutputStreamoutToServer;

publicchatClient(){

mainJframe=newJFrame("聊天----客户端");

con=mainJframe.getContentPane();

showArea=newJTextArea();

showArea.setEditable(false);

showArea.setLineWrap(true);

JSPane=newJScrollPane(showArea);

msgText=newJTextField();

msgText.setColumns(30);

msgText.addActionListener(this);

sentBtn=newJButton("发送");

sentBtn.addActionListener(this);

pane=newJPanel();

pane.setLayout(newFlowLayout());

pane.add(msgText);

pane.add(sentBtn);

con.add(JSPane,BorderLayout.CENTER);

con.add(pane,BorderLayout.SOUTH);

mainJframe.setSize(500,400);

mainJframe.setVisible(true);

mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try{

connectToServer=newSocket("localhost",5500);

inFromServer=newDataInputStream(

connectToServer.getInputStream());

outToServer=newDataOutputStream(

connectToServer.getOutputStream());

showArea.append("连接成功,请说话\n");

thread=newThread(this);

thread.setPriority(Thread.MIN_PRIORITY);

thread.start();

}catch(IOExceptione){

showArea.append("对不起,没能连接到服务器\n");

msgText.setEditable(false);

sentBtn.setEnabled(false);

}

}

publicstaticvoidmain(String[]args)

{newchatClient();}

publicvoidactionPerformed(ActionEvente){

Strings=msgText.getText();

if(s.length()>0){

try{

outToServer.writeUTF(s);

outToServer.flush();

showArea.append("我说:

"+msgText.getText()+"\n");

msgText.setText(null);

}catch(IOExceptionel){

showArea.append("你的消息:

"+msgText.getText()+"未能发送出去\n");

}

}

}

publicvoidrun(){

try{

while(true){

showArea.append("对方说:

"+inFromServer.readUTF()+"\n");

Thread.sleep(1000);

}

}catch(IOExceptionel){

}catch(InterruptedExceptione){}

{}

}

}

服务端:

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

import.*;

importjavax.swing.*;

publicclasschatServerimplementsActionListener,Runnable

{

JTextAreashowArea;

JTextFieldmsgText;

JFramemainJframe;

JButtonsentBtn;

JScrollPaneJSPane;

JPanelpane;

Containercon;

Threadthread=null;

ServerSocketserverSocket;

SocketconnectToClient;

DataInputStreaminFromClient;

DataOutputStreamoutToClient;

publicchatServer()

{

mainJframe=newJFrame("聊天----服务器端");

con=mainJframe.getContentPane();

showArea=newJTextArea();

showArea.setEditable(false);

showArea.setLineWrap(true);

JSPane=newJScrollPane(showArea);

msgText=newJTextField();

msgText.setColumns(30);

msgText.addActionListener(this);

sentBtn=newJButton("发送");

sentBtn.addActionListener(this);

pane=newJPanel();

pane.setLayout(newFlowLayout());

pane.add(msgText);

pane.add(sentBtn);

con.add(JSPane,BorderLayout.CENTER);

con.add(pane,BorderLayout.SOUTH);

mainJframe.setSize(500,400);

mainJframe.setVisible(true);

mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try{

serverSocket=newServerSocket(5500);

showArea.append("正在等待对话请求\n");

connectToClient=serverSocket.accept();

inFromClient=newDataInputStream(

connectToClient.getInputStream());

outToClient=newDataOutputStream(

connectToClient.getOutputStream());

thread=newThread(this);

thread.setPriority(Thread.MIN_PRIORITY);

thread.start();

}catch(IOExceptione){

showArea.append("对不起,不能创建服务器");

msgText.setEditable(false);

sentBtn.setEnabled(false);

}

}

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

当前位置:首页 > 工程科技 > 冶金矿山地质

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

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