您的位置:首页 > 职场人生

黑马程序员_java基础UDP数据传输

2015-04-19 22:55 218 查看
------ <a href="http://www.itheima.com" target="blank">Windows Phone 7手机开发</a>、<a href="http://www.itheima.com" target="blank">.Net培训</a>、期待与您交流! -------

/*逻辑端口 0-1024 0-65535 127.0.0.1
地址段相同可以通讯。通过子网掩码,将一群人分配一个公网。
传输层(udp tcp)和网际层(ip),应用层(http ftp)
*/
/*
拿到任意一台主机的IP,前提是该计算机在网上有注册
需求:通过udp传输方式,将一段文字数据发送出去。
思路:
1,建立udpsocket服务。
2,提供数据,并将数据封装到数据包中。
3,通过socket服务的发送功能,将数据包发出去。
4,关闭资源。
*/

/*import jaca.net.*;

class UdpSend
{
public static void main(String[] args) throws Exception
{
InetAddress ia = InetAddress.getLocalHost();

System.out.println(ia.toString());//InetAddress ia = getByName("www.baidu.com")

System.out.println("address:"+ia.getHostAddress());

System.out.println("name:"+ia,.getHostName());

}
}
*/

import jaca.net.*;
class UdpSend

{
public static void main(String[] args)
{
//1 创建udp服务。通过DatagramSocket对象
DatagramSocket ds = new DatagramSocket(8888);

//2, 确定数据,并封装成数据包。DatagramPacket(byte[] buf, int length, InetAddress address,int port)
byte[] buf = "udp ge men lai le".getBytes();
DatagramPacket dp=
new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.254"),10000);
//3.通过socket服务,将已有的数据包发送出去。通过send方法。
ds.send(dp);
//4,关闭资源。
ds.close();
}

}

/*
发送和接收端都是独立应用程序
接收udp协议传输的数据并处理
思路:
1,定义udpsocket服务.
2,定义一个数据包,存储接收到的字节数据。因为数据包对象中有更多功能可以提取字节数据中的不同数据信息。
3,通过socket服务的receive方法将受到的数据存入已定义好的数据包中。
4,通过数据包对象特有功能。将这些不同的数据取出。打印在控制台上。
5,关闭资源。
*/

import jaca.net.*;
class UdpRece

{
public static void main(String[] args) throws Exception
{
//1,创建udp socket,建立端点。
DatagramSocket ds = new DatagramSocket(10000);
//2,定义数据包,用于存储数据
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
//3,通过服务的receive方法将收到数据存入数据包中。
ds.receive(dp);
//4,通过数据包的方法获取其中的数据。
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int port =dp.getPort();
System.out.println(ip+"::"+data+"::"+port);
//5,关闭资源
ds.close();
}
}

import java.net.*;
import java.io.*;
class UdpSend2
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();

BufferedReader bufr=
new BufferedReader(new InputStreamReader(System.in));

String line= null;

while((line =bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket (buf,buf.length,InetAddress.getByName("192.168.1.125"),10001);
ds.send(dp);
}
ds.close();
}
}

class UdpRece2
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(10001);
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);

ds.receive(dp);

String ip=dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());

System.out.println(ip+"::"+data);
}
}
}

/*
编写一个聊天程序,有收数据和发数据部分。多线程技术。
因为收和发动作时不一致的,所以要定义两个run方法,且要封装到不同的类中。
*/

import java.net.*;
import java.io.*;
class Send inplements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds = ds;
}

public void run()
{
try
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in);
String line = null;
while((line =bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket (buf,buf.length,InetAddress.getByName("192.168.1.125"),10001);
ds.send(dp);
}
catch(Exception e)
{
throw new RuntimeException("发送端失败");
}
}
}
class Rece implements Runnable
{
private DatagramSocket ds;
pubklic Rece(DatagramSocket ds)
{
this.ds =ds;
}
public void run()
{
try
{
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);

ds.receive(dp);

String ip = dp.getAddress().getHostAddress();

String data = new String(dp.getData(),0,dp.getLength());

System.out.println(ip+":"+data);

}
}
catch(Exception e)
{
throw new RuntimeException("接收端失败");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐