实验四基于Socket的CS程序Word格式文档下载.doc
- 文档编号:13167332
- 上传时间:2022-10-07
- 格式:DOC
- 页数:12
- 大小:142.50KB
实验四基于Socket的CS程序Word格式文档下载.doc
《实验四基于Socket的CS程序Word格式文档下载.doc》由会员分享,可在线阅读,更多相关《实验四基于Socket的CS程序Word格式文档下载.doc(12页珍藏版)》请在冰豆网上搜索。
二、实验内容
利用Java语言提供的Socket技术,建立一个C/S模式的应用,允许客户端用户输入2个整数,服务器端接收这2个整数,并计算出它们的和、差、积、商,最后送回客户端。
服务器端ServerSocket类(.ServerSocket)
构造方法:
publicServerSocket(int
port)throwsIOException——S_1
接受连接请求:
publicSocketaccept()throwsIOException——S_2
客户端Socket类(.Socket)
构造方法:
publicSocket(String
host,int
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);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 基于 Socket CS 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)