实验四 基于Socket的CS程序文件Word下载.docx
- 文档编号:17979528
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:11
- 大小:105.68KB
实验四 基于Socket的CS程序文件Word下载.docx
《实验四 基于Socket的CS程序文件Word下载.docx》由会员分享,可在线阅读,更多相关《实验四 基于Socket的CS程序文件Word下载.docx(11页珍藏版)》请在冰豆网上搜索。
port)throwsUnknownHostException,IOException——C_1
读取数据:
publicInputStreamgetInputStream()throwsIOException——S_5,C_3
发送数据:
publicOutputStreamgetOutputStream()throwsIOException——S_3,C_4
服务器端编程
创建ServerSocket对象
ServerSocketserver=newServerSocket(8000);
监听来自客户端的请求
SocketlinkSocket=server.accept();
与客户端进行通信
利用linkSocket.getInputStream()接收客户端数据;
对读取的数据进行加、减、乘、除计算;
利用linkSocket.getoutputStream()向客户端发送计算结果;
监听结束时关闭服务器
server.close();
客户端编程
建立连接
Socketclient=newSocket(“ServerComputerName”,8000);
与服务器进行通信
从键盘读入两个整数;
利用client.getoutputStream()向服务器端发送数据;
利用client.getInputStream()接收服务器端结果;
显示从服务器端得到的结果;
释放连接
Client.close();
要求:
在一台主机上安装并运行服务器端程序,在另一台计算机上安装并运行客户端程序,测试所编写的程序能否完成规定的功能。
三、实验步骤与源程序
源程序:
服务器:
import.*;
importjava.util.*;
importjava.io.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassServer
{
publicServer()
{ServerSocketserver=null;
Server_threadthread;
Socketyou=null;
while(true)
try
{server=newServerSocket(8755);
}
catch(IOExceptione1)
{System.out.println("
正在监听"
);
}//ServerSocket对象不能重复创建
等待客户呼叫"
you=server.accept();
System.out.println("
已连接到客户"
客户的地址:
"
+you.getInetAddress());
catch(IOExceptione)
正在等待客户"
if(you!
=null)
{newServer_thread(you).start();
}//为每个客户启动一个专门的线程
}
publicstaticvoidmain(Stringargs[])
{newServer();
classServer_threadextendsThread
Socketsocket;
DataOutputStreamout=null;
DataInputStreamin=null;
Strings=null;
booleanquesion=false;
Server_thread(Sockett)
{socket=t;
{out=newDataOutputStream(socket.getOutputStream());
in=newDataInputStream(socket.getInputStream());
catch(IOExceptione){}
publicvoidrun()
{doublea[]=newdouble[2];
inti=0;
s=in.readUTF();
//堵塞状态,除非读取到信息
quesion=false;
StringTokenizerfenxi=newStringTokenizer(s,"
"
while(fenxi.hasMoreTokens()&
&
quesion==false)
Stringtemp=fenxi.nextToken();
{a[i]=Double.valueOf(temp).doubleValue();
i++;
}catch(NumberFormatExceptione)
{out.writeUTF("
请输入数字字符"
quesion=true;
if(quesion==false)
doubles=a[0]+a[1];
doublep=a[0]-a[1];
doubleq=a[0]*a[1];
doubler=a[0]/a[1];
out.writeUTF("
"
+a[0]+"
+"
+a[1]+"
="
+s);
-"
+p);
*"
+q);
/"
+r);
客服端数据:
和"
+a[1]);
完成客户端运算"
客户离开"
return;
客户端:
publicclassClientextendsFrameimplementsRunnable,ActionListener
{DataInputStreamin=null;
DataOutputStreamout=null;
Panelp;
Labellab1,lab2,lab3;
TextFieldtf1,tf2,tf3;
Buttonconnection,send;
Socketsocket=null;
Threadthread;
TextAreatext;
publicClient()
{super("
setLayout(newFlowLayout());
setSize(300,250);
//设置窗口大小
setLocation(200,200);
//设置窗口显示位置
setResizable(false);
p=newPanel();
p.setLayout(null);
p.setBounds(0,0,300,250);
addWindowListener(newWindowAdapter()
publicvoidwindowClosing(WindowEvente)
{System.exit(0);
});
text=newTextArea();
lab1=newLabel("
输入IP地址:
lab1.setBounds(5,5,60,25);
lab2=newLabel("
第一个数字:
lab2.setBounds(5,35,60,25);
lab3=newLabel("
第二个数字:
lab3.setBounds(5,65,60,25);
tf1=newTextField(10);
tf1.setBounds(80,5,100,25);
tf2=newTextField(10);
tf2.setBounds(80,35,100,25);
tf3=newTextField(10);
tf3.setBounds(80,65,100,25);
connection=newButton("
连接服务器"
connection.setBounds(200,5,90,25);
connection.addActionListener(this);
send=newButton("
发送数据"
send.setBounds(200,65,90,25);
send.addActionListener(this);
text=newTextArea(200,50);
text.setBounds(5,100,290,110);
p.add(lab1);
p.add(lab2);
p.add(lab3);
p.add(tf1);
p.add(tf2);
p.add(tf3);
p.add(connection);
p.add(send);
p.add(text);
add(p);
setVisible(true);
socket=newSocket();
thread=newThread(this);
publicvoidactionPerformed(ActionEvente)
{if(e.getSource()==connection)
InetAddressaddress=null;
InetSocketAddresssocketAddress=null;
try//请求和服务器建立套接字连接:
if(socket.isConnected()){}
else
{address=InetAddress.getByName(tf1.getText());
socketAddress=newInetSocketAddress(address,8755);
socket.connect(socketAddress,2000);
//*等待2秒中
in=newDataInputStream(socket.getInputStream());
out=newDataOutputStream(socket.getOutputStream());
send.setEnabled(true);
JOptionPane.showMessageDialog(this,"
连接成功!
成功!
JOptionPane.INFORMATION_MESSAGE);
thread.start();
}}
catch(IOExceptionex)
timeout"
+ex);
System.out.println(socketAddress.getHostName()+"
:
+socketAddress.getPort());
JOptionPane.showMessageDialog(this,"
连接超时!
错误!
JOptionPane.ERROR_MESSAGE);
newClient();
dispose();
if(e.getSource()==send)
{Strings=tf2.getText()+"
+tf3.getText();
if(s!
{try
{out.writeUTF(s);
catch(IOExceptione1){}
{Strings=null;
{s=in.readUTF();
text.append(s+"
\n"
catch(IOExceptione)
{text.append("
与服务器已断开"
break;
{newClient();
四、测试数据与实验结果
五、结果分析与实验体会
通过这次实验,进一步了解了套接字一些用法,并且巩固了传输层与套接字的基本知识;
以前对套接字不是很熟悉,经过这次实验,明白了套接字在网络编程中有着重要的角色,比如我们使用的聊天工具就是利用套接字来实现的。
六、思考题
六、Java语言中,服务器端和客户端套接字对象类相同吗?
如不同,有何差别?
答:
不相同。
客户端的程序使用Socket类建立到服务器的套接字连接,而服务器使用ServerSocket对象等待接收客户的信息。
七、程序编写完毕并编译后,先运行客户端程序,还是先运行服务器端程序?
为什么?
先运行服务器端程序。
因为服务器端需要一直监听服务器端是否有请求。
八、Java语言中如何使用UDP协议进行数据传输的?
java里UDP协议传送的方式细节已经封装了到类DatagramSocket里了,只要用DatagramSocket的对象进行传输数据就可以了。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验四 基于Socket的CS程序文件 实验 基于 Socket CS 程序 文件