计算机网络实验IP电话.docx

上传人:b****7 文档编号:26656987 上传时间:2023-06-21 格式:DOCX 页数:9 大小:69.01KB
下载 相关 举报
计算机网络实验IP电话.docx_第1页
第1页 / 共9页
计算机网络实验IP电话.docx_第2页
第2页 / 共9页
计算机网络实验IP电话.docx_第3页
第3页 / 共9页
计算机网络实验IP电话.docx_第4页
第4页 / 共9页
计算机网络实验IP电话.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

计算机网络实验IP电话.docx

《计算机网络实验IP电话.docx》由会员分享,可在线阅读,更多相关《计算机网络实验IP电话.docx(9页珍藏版)》请在冰豆网上搜索。

计算机网络实验IP电话.docx

计算机网络实验IP电话

计算机网络实验—IP电话

 

专业/班级软件42

学号2140306010

学生姓名牛庆德

目录

实验任务3

实验设计3

实验思路3

实现代码3

PhoneServer.java3

PhoneClient.java5

运行结果7

致谢8

参考文献8

实验任务

使用VoIP,实现IP电话,并传递语音。

实验设计

分为客户端与服务端实现IP电话,客户端输入IP拨打服务端的电话,并且实现通过UDP传送语音信息,实现语言为Java。

实验思路

发送端调用J包里的DatagramSocket实现套接字,并提示用户输入目的端的IP地址(可以通过命令Ipconfig得到IPV4地址),在用户输入目的端的IP地址之后,程序通过套接字将数据由应用层移交到传输层,再通过socket.send方法,将数据包交由网络层发送给服务端。

服务端使用Javax.swing中的类实现可视化界面,注册ActionListener监听器实现对按钮的监听,再通过socket.receive方法读取的数据包,并得到发送方的IP地址并显示在界面中,并且可以选择接受或停止接受语音消息。

实现代码

PhoneServer.java

packageip_phone;

importjava.awt.*;

import.*;

importjavax.swing.*;

importjava.awt.event.*;

importjavax.sound.sampled.*;

publicclassPhoneServerextendsJFrameimplementsRunnable,ActionListener{

privatestaticfinallongserialVersionUID=1L;

intport;

SourceDataLineline;

DatagramSocketsocket=null;

JButtonstart_button=newJButton("开启服务器");

JButtonstop_button=newJButton("停止服务器");

Threadthread;

Stringmsg=null;

booleanStopFlag=false;

publicPhoneServer(){

setTitle("IPPhone");

AudioFormatformat=newAudioFormat(8000,16,2,true,true);

DataLine.Infoinfo=newDataLine.Info(SourceDataLine.class,format);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

thread=newThread(this);

start_button.addActionListener(this);//注册Listener

stop_button.addActionListener(this);

JPanelnorth=newJPanel();

north.add(start_button);

north.add(stop_button);

add(north,BorderLayout.NORTH);

port=7091;//端口号

try{

socket=newDatagramSocket(port);

line=(SourceDataLine)AudioSystem.getLine(info);

line.open(format,1024);

line.start();

setBounds(500,500,300,200);

setVisible(true);

}catch(Exceptione){

e.printStackTrace();

}

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==start_button){//单击按钮start_button触发事件

start_button.setBackground(Color.red);

stop_button.setBackground(Color.green);

if(!

(thread.isAlive())){

thread=newThread(this);

}

thread.start();

StopFlag=false;

}

if(e.getSource()==stop_button){

start_button.setBackground(Color.green);

stop_button.setBackground(Color.red);

StopFlag=true;}

}

publicvoidrun(){

while(true){

bytedata[]=newbyte[1024];

DatagramPacketpacket=null;

packet=newDatagramPacket(data,data.length);

try{

socket.receive(packet);//接收数据

msg="HearingvociefromIP:

\n"+packet.getAddress();

JTextAreashowing=newJTextArea();

showing.setText(msg);

validate();

add(showing,BorderLayout.CENTER);

line.write(data,0,data.length);

}catch(Exceptione){

e.printStackTrace();

}

if(StopFlag){//用于停止服务器

break;

}

}

}

publicstaticvoidmain(String[]args){

newPhoneServer();

}

}

PhoneClient.java

packageip_phone;

import.*;

importjavax.swing.*;

importjavax.sound.sampled.*;

publicclassPhoneClientextendsThread{

privateTargetDataLineline;

intport=7091;

InetAddressiaddress=null;

DatagramSocketsocket=null;

PhoneClient(){

AudioFormatformat=newAudioFormat(8000,16,2,true,true);

DataLine.Infoinfo=newDataLine.Info(TargetDataLine.class,format);

try{

Stringip_sender=JOptionPane.showInputDialog("输入IP");

iaddress=InetAddress.getByName(ip_sender);

JOptionPane.showMessageDialog(null,"已建立连接,正在传递消息...","IPPhone",JOptionPane.PLAIN_MESSAGE);

socket=newDatagramSocket(5151);

line=(TargetDataLine)AudioSystem.getLine(info);

line.open(format,line.getBufferSize());

line.start();

}catch(Exceptione){

e.printStackTrace();

}

}

publicvoidrun(){

while(true){

DatagramPacketpacket=null;

bytedata[]=newbyte[1024];

line.read(data,0,data.length);

//将数据打包

packet=newDatagramPacket(data,data.length,iaddress,port);

try{

socket.send(packet);

}catch(Exceptione){

e.printStackTrace();

}

}

}

publicstaticvoidmain(String[]args){

newPhoneClient().run();

}

}

运行结果

致谢

感谢老师的悉心教学,让我顺利完成了实验,并且通过这次试验,我对Java这门语言的理解更深了一步,体会到了使用API开发的便利与简洁。

参考文献

【1】JamesF.KuroseandKeithW.RossComputerNetworkingATop-DownApproachChinaMachinePress2016

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

当前位置:首页 > 总结汇报 > 实习总结

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

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