计网实验二 套接字编程Word文档格式.docx

上传人:b****3 文档编号:6456105 上传时间:2023-05-06 格式:DOCX 页数:17 大小:141.23KB
下载 相关 举报
计网实验二 套接字编程Word文档格式.docx_第1页
第1页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第2页
第2页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第3页
第3页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第4页
第4页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第5页
第5页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第6页
第6页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第7页
第7页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第8页
第8页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第9页
第9页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第10页
第10页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第11页
第11页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第12页
第12页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第13页
第13页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第14页
第14页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第15页
第15页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第16页
第16页 / 共17页
计网实验二 套接字编程Word文档格式.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

计网实验二 套接字编程Word文档格式.docx

《计网实验二 套接字编程Word文档格式.docx》由会员分享,可在线阅读,更多相关《计网实验二 套接字编程Word文档格式.docx(17页珍藏版)》请在冰点文库上搜索。

计网实验二 套接字编程Word文档格式.docx

//打开输入输出流

OutputStreamoutstr;

DataInputStreamdin;

//封装输入输出流

DataOutputStreamdout;

初始化变量

s=newServerSocket(6666);

client=s.accept();

instr=client.getInputStream();

outstr=client.getOutputStream();

din=newDataInputStream(instr);

dout=newDataOutputStream(outstr);

功能主体:

将服务器的接收和发送放到子线程Run()中

TCPClient:

初始化变量:

客户端的寻址是“localhost”相当于127.0.0.1

Socketsoc=newSocket("

localhost"

6666);

InputStreaminstr=soc.getInputStream();

OutputStreamoutstr=soc.getOutputStream();

DataInputStreamdin=newDataInputStream(instr);

DataOutputStreamdout=newDataOutputStream(outstr);

Client的发送和接收功能放到两个不同的线程

UDPClient和UDPServer与TCP的实现相似,不同的是UDP不需要建立连接,即不需要等待UDPServer的开启。

(3)Socket原理说明

24.在一台主机上安装编译用java写的TCPClient和UDPClient程序,在另一台主机上安装编译TCPServer和UDPServer程序。

a.如果你在运行TCPServer之前运行TCPClient,将会发生什么现象?

为什么?

答:

如图,TCPClient打不开,抛出Exception异常。

TCP的通信需要建立连接,当TCPServer端没有打开时,运行TCPClient,那么连接建立失败,那么Socketsoc=newSocket("

初始化失败,程序无法运行。

b.如果你在运行UDPServer之前运行UDPClient,将会发生什么现象?

程序正常运行。

UDP通信不需要建立连接,没有Client和Server的严格区分,发送方的不会在意接收方是否在线,只负责将数据送出去。

c.如果你对客户机端和服务器端使用了不同的端口,将会发生什么现象?

答;

客户机和服务器的端口之间没有直接的联系,客户端的端口号信息会发送到服务器端,所以不管客户端的端口号怎么变化都不会影响正常的通信。

25.假定在UDPClient.java中,我们使用

DatagramSocketclientSocket=newDatagramSocket(5432);

代替

DatagramSocketclientSocket=newDatagramSocket();

在UDPServer.java中是否有必要进行修改?

UDPClient和UDPServer中的套接字端口号是多少?

在变化之前他们是多少?

没有必要进行修改,通过newDatagramSocket();

UDPClient会获得一个随机的可行端口号,newDatagramSocket(5432);

指定了UDPClient的端口号为5432,当端口5432空闲时,程序正常运行,当5432已经被占用时,程序将无法正常运行。

变化之前UDPServer的端口号:

8454

变化之前UDPClient的端口号:

63884

三、实验心得

通过这次实验,加深了我对UDP和TCP的Socket工作原理,UDP的通信不需要建立连接,只是尽力将数据发送出去,TCP通信需要建立连接,TCPClient开启之前,TCPServer必须要开启,一个服务器可以同时与多个客户端进行通信,一个客户端也可以同时与多个服务器进行连接。

因为每一个进程都由“IP+端口号”确定了唯一性。

 

Date:

2017/5/12

附件:

TCPClient:

publicclassTCPClientextendsFrameimplementsActionListener,Runnable{

privatestaticfinallongserialVersionUID=1L;

Socketsoc=newSocket("

privateTextAreaTa=newTextArea();

privateTextFieldTf1=newTextField(23);

publicTCPClient()throwsIOException{

setTitle("

TCP_Client"

);

setSize(400,300);

setVisible(true);

add(Ta,"

Center"

PanelP=newPanel(newGridLayout(1,1,5,5));

PanelP1=newPanel();

P1.add(newLabel("

发送的信息"

));

P1.add(Tf1);

Tf1.addActionListener(this);

//添加监听器

P.add(P1);

add(P,"

South"

//s=newServerSocket(Integer.parseInt(Tf1.getText().toString()));

addWindowListener(newWindowAdapter(){//关闭窗口

publicvoidwindowClosing(WindowEvente)

{

System.exit(0);

}

});

(newThread(this)).start();

//发送数据

publicvoidactionPerformed(ActionEventarg0){

try{

Stringmsg=Tf1.getText().toString();

dout.writeUTF(msg);

Tf1.setText("

"

Ta.append("

Clientsenddatas:

+msg+"

\n"

}

catch(IOExceptione)

{

e.printStackTrace();

}

//接收数据

publicvoidrun(){

try{

while(true){

Thread.sleep(0);

Ta.append(soc.getInetAddress()+"

+din.readUTF()+"

}

}catch(IOExceptione){

e.printStackTrace();

}catch(InterruptedExceptione){

}

publicstaticvoidmain(String[]args)throwsIOException{

newTCPClient();

}

TCPServer:

publicclassTCPServerextendsFrameimplementsActionListener,Runnable{

//ServerOK标志

privatebooleanokflag=true;

JTextPaneJp=newJTextPane();

publicTCPServer()throwsIOException{

TCP_Server"

//

s=newServerSocket(6666);

dout=newDataOutputStream(outstr);

while(true){

Stringmsg0=din.readUTF();

if(msg0.equals("

connect"

)){

Ta.append(client.getInetAddress()+"

+msg0+"

okflag=true;

while(true){

//发送ServerOK

if(okflag){

Thread.sleep(100);

dout.writeUTF("

ServerOK"

okflag=false;

}

//接收信息

Stringmsg=din.readUTF();

Ta.append(client.getInetAddress()+"

//断开连接

if(msg.equals("

bye"

ConnectionClose"

break;

}

}catch(IOExceptione){

}catch(InterruptedExceptione){

publicstaticvoidmain(String[]args)throwsIOException{

newTCPServer();

//TODOAuto-generatedmethodstub

UDPServer:

publicclassUDPServerextendsFrameimplementsRunnable{

/**

*

*/

//所有发送和接收的数据都将通过该套接字

privateDatagramSocketsoc=newDatagramSocket(8454);

privateDatagramPacketpac;

publicUDPServer()throwsSocketException{

UDP_Server"

addWindowListener(newWindowAdapter(){

(newThread(this)).start();

//接收和发送数据

while(true){

byte[]buf=newbyte[1024];

pac=newDatagramPacket(buf,buf.length);

soc.receive(pac);

Stringmsg=newString(pac.getData(),0,pac.getLength());

if(msg.equals("

Ta.append(pac.getAddress()+"

if(okflag){

msg="

;

buf=msg.getBytes();

System.out.println(pac.getPort());

DatagramPacketD=newDatagramPacket(buf,buf.length,pac.getAddress(),pac.getPort());

soc.send(D);

byte[]buf0=newbyte[1024];

pac=newDatagramPacket(buf0,buf0.length);

soc.receive(pac);

msg=newString(pac.getData(),0,pac.getLength());

Ta.append(pac.getAddress()+"

//连接断开

}

newUDPServer();

UDPClient:

publicclassUDPClientextendsFrameimplementsActionListener,Runnable{

//分配一个可用的端口号

DatagramSocketsoc=newDatagramSocket();

publicUDPClient()throwsSocketException{

UDP_Client"

PanelP=newPanel();

addWindowListener(newWindowAdapter(){//监听关闭窗口

//发送信息

Stringmsg=Tf1.getText().toString();

//InetAddressadr=InetAddress.getByName("

10.63.208.52"

DatagramPacketpac=newDatagramPacket(msg.getBytes(),msg.getBytes().length,InetAddress.getLocalHost(),8454);

soc.send(pac);

Tf1.setText("

Ta.append("

///接收信息

DatagramPacketD=newDatagramPacket(buf,buf.length);

soc.receive(D);

Stringmsg=newString(D.getData(),0,D.getLength());

Ta.append(D.getAddress()+"

newUDPClient();

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

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2