实验5基于TCP的Sockets编程解答Word文件下载.docx
《实验5基于TCP的Sockets编程解答Word文件下载.docx》由会员分享,可在线阅读,更多相关《实验5基于TCP的Sockets编程解答Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。
publicstaticvoidmain(String[]args){
try{
ServerSocketss=newServerSocket(2007);
while(true){
Sockets=ss.accept();
DataOutputStreamout=newDataOutputStream(
s.getOutputStream());
writeTime(out);
out.close();
s.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidwriteTime(DataOutputStreamout)throwsIOException{
Calendarcurrent=Calendar.getInstance();
out.writeInt(current.get(Calendar.YEAR));
out.writeByte(current.get(Calendar.MONTH));
out.writeByte(current.get(Calendar.DAY_OF_MONTH));
out.writeByte(current.get(Calendar.HOUR_OF_DAY));
out.writeByte(current.get(Calendar.MINUTE));
out.writeByte(current.get(Calendar.SECOND));
}
3.客户端代码为:
publicclassDaytimeClient{
Sockets=newSocket("
localhost"
2007);
DataInputStreamin=newDataInputStream(s.getInputStream());
printTime(in);
}
}
staticvoidprintTime(DataInputStreamin)throwsIOException{
System.out.println("
服务器当前时间是:
"
);
System.out.print(in.readInt()+"
年"
System.out.print(in.readByte()+"
月"
日"
时"
分"
秒"
4.为了使服务器能够响应多个客户端,必须增加一个线程类,该类的run方法代码如下,请将该类补充完整:
publicclassDaytimeThreadextendsThread{
Sockets;
publicDaytimeThread(Sockets){
this.s=s;
publicvoidrun(){
try{
DataOutputStreamout=newDataOutputStream(
s.getOutputStream());
Calendarcurrent=Calendar.getInstance();
out.writeInt(current.get(Calendar.YEAR));
out.writeByte(current.get(Calendar.MONTH));
out.writeByte(current.get(Calendar.DAY_OF_MONTH));
out.writeByte(current.get(Calendar.HOUR_OF_DAY));
out.writeByte(current.get(Calendar.MINUTE));
out.writeByte(current.get(Calendar.SECOND));
out.close();
}catch(IOExceptione){
5.修改服务端程序,使其利用上面的线程类支持多线程;
publicclassDaytimeServer{
newDaytimeThread(s).start();
publicstaticvoidwriteTime(Sockets)throwsIOException{
DataOutputStreamout=newDataOutputStream(
s.getOutputStream());
out.close();
6.基于图形界面的时钟客户端的部分代码如下,请将其补充完整:
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassDayTimeGUIClientextendsApplet{
privateGraphicsgBuf;
privateImageimgBuf;
staticintyear,month,day,hour,minute,second;
Frameapp=newFrame("
时钟客户端"
app.setSize(800,600);
app.setLocationByPlatform(true);
DayTimeGUIClientapplet=newDayTimeGUIClient();
app.add(applet);
app.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEventevent){
event.getWindow().dispose();
System.exit(0);
});
app.show();
applet.start();
while(true){
try{
Sockets=newSocket("
DataInputStreamin=newDataInputStream(s.getInputStream());
readTime(in);
}catch(IOExceptione){
e.printStackTrace();
applet.repaint();
Thread.sleep(1000);
}catch(InterruptedExceptione){
}
staticvoidreadTime(DataInputStreamin)throwsIOException{
year=in.readInt();
month=in.readByte();
day=in.readByte();
hour=in.readByte();
minute=in.readByte();
second=in.readByte();
}
publicvoidinit(){
publicvoidpaint(Graphicsg){
finaldoublePI=3.1415926;
intx0,y0,x1,y1,x2,y2,t;
intgraphWidth=bounds().width;
intgraphHeight=bounds().height;
imgBuf=createImage(graphWidth,graphHeight);
gBuf=imgBuf.getGraphics();
gBuf.clearRect(0,0,graphWidth,graphHeight);
//用字符方式显示当前时间
gBuf.drawString(year+"
+month+"
+day+"
+hour+"
+minute+"
+second+"
graphWidth/20,graphHeight/20);
//获取直径,圆心坐标
intr=(graphWidth<
graphHeight)?
graphWidth:
graphHeight;
r=r*6/7;
x0=graphWidth/2;
y0=graphHeight/2;
//绘制时钟刻度
gBuf.drawOval((graphWidth-r)/2,(graphHeight-r)/2,r,r);
r=r/2;
for(t=0;
t<
60;
t++){
x1=x0+(int)(r*Math.cos(t*PI/30));
y1=y0+(int)(r*Math.sin(t*PI/30));
//System.out.println("
x1,y1:
+x1+"
"
+y1);
if(t%5==0){
x2=x0+(int)(r*Math.cos(t*PI/30)*17/20);
y2=y0+(int)(r*Math.sin(t*PI/30)*17/20);
}else{
x2=x0+(int)(r*Math.cos(t*PI/30)*19/20);
y2=y0+(int)(r*Math.sin(t*PI/30)*19/20);
x2,y2:
+x2+"
+y2);
gBuf.drawLine(x1,y1,x2,y2);
//绘制时针
t=60*((hour<
12)?
hour:
hour%12)+minute;
x1=x0+(int)(r*Math.cos(t*PI/360-PI/2)*4/10);
y1=y0+(int)(r*Math.sin(t*PI/360-PI/2)*4/10);
x2=x0+(int)(r*Math.cos(t*PI/360+PI/2)*1/10);
y2=y0+(int)(r*Math.sin(t*PI/360+PI/2)*1/10);
gBuf.drawLine(x1,y1,x2,y2);
//绘制分针
t=60*minute+second;
x1=x0+(int)(r*Math.cos(t*PI/1800-PI/2)*6/10);
y1=y0+(int)(r*Math.sin(t*PI/1800-PI/2)*6/10);
x2=x0+(int)(r*Math.cos(t*PI/1800+PI/2)*1/10);
y2=y0+(int)(r*Math.sin(t*PI/1800+PI/2)*1/10);
//绘制秒针
t=second;
x1=x0+(int)(r*Math.cos(t*PI/30-PI/2)*8/10);
y1=y0+(int)(r*Math.sin(t*PI/30-PI/2)*8/10);
x2=x0+(int)(r*Math.cos(t*PI/30+PI/2)*1/10);
y2=y0+(int)(r*Math.sin(t*PI/30+PI/2)*1/10);
g.drawImage(imgBuf,0,0,this);
publicvoidupdate(Graphicsg){
paint(g);
7.编写一个登录的应用来观察flush语句的作用。
将flush语句注释后会有什么结果?
服务器:
ServerSocketss;
Sockets;
BufferedReaderbr;
PrintWriterpw;
ss=newServerSocket(3456);
s=ss.accept();
br=newBufferedReader(newInputStreamReader
(s.getInputStream()));
pw=newPrintWriter(s.getOutputStream());
if(br.readLine().equals("
user"
)&
&
(br.readLine().equals("
password"
)))
{
pw.println("
T"
System.out.println("
userisvalid."
}else{
F"
userisinvalid."
}
pw.flush();
}
客户端:
publicstaticvoidmain(String[]args){
BufferedReaderbin,br;
try{
s=newSocket(InetAddress.getByName("
),3456);
br=newBufferedReader(newInputStreamReader
(s.getInputStream()));
pw=newPrintWriter(s.getOutputStream());
bin=newBufferedReader(newInputStreamReader
(System.in));
System.out.println("
pleaseinputyouraccount:
pw.println(bin.readLine());
pleaseinputyourpassword:
pw.flush();
if(br.readLine().equals("
))
System.out.println("
loginsucceeded."
else
loginfailed."
将flush语句去除后,客户端将不会将输出的数据发送到服务器端,从而造成客户端与服务器端同时阻塞的情况。
8.针对第4题,定义数据传递的格式为:
绘制N个点:
POINTNx0y0x1y1……(回车换行)
绘制折线:
POLYLINENx0y0x1y1……(回车换行)
9.按照上面给出的数据传递格式编写客户端与服务器端程序。
四、调试分析
实验过程中经常遇到的问题就是阻塞问题,由于在客户端与服务器互相传递数据时,经常会遗忘flush语句,即在输出数据后,忘记调用flush语句强制输出,从而使程序莫名其妙地停顿,得不到正确的结果,改正后一切正常。
五、实验结果
按照实验指导书的步骤完成第1、2、3题,得到了实验所要求的实验结果。
另外编写代码实现了第4题,其代码如下:
publicclassDrawClient{
Pleaseinputpoints:
Stringreadline=(newBufferedReader(newInputStreamReader(System.in))).readLine();
Sockets=newSocket(InetAddress.getByName("
),2007);
PrintWriterpw=newPrintWriter(s.getOutputStream());
pw.println(readline);
pw.close();
s.close();
服务器端:
publicclassDrawServerextendsApplet{
staticintx[]=newint[10];
staticinty[]=newint[10];
staticintn;
Server"
DrawServerapplet=newDrawServer();
ss=newServerSocket(2007);
}catch(IOExceptione1){
e1.printStackTrace();
return;
try{
BufferedReaderin=newBufferedReader(newInputStre