java网络之udp

import java.net.*;

/**
* InetAddress用法
* 获取本地主机名,ip
* 通过主机名,ip获取InetAddress对象
*/
public class Demo{
public static void main(String[] args) throws Exception {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost.toString());
System.out.println(localHost.getHostAddress());
System.out.println(localHost.getHostName()); InetAddress thinkpad = InetAddress.getByName("reborn-PC");
System.out.println(thinkpad.toString());
}
}
import java.net.*;

/**
* 简单udp发送接收
*/
class UdpSend {
public static void main(String[] args) throws Exception {
// 1,创建udp服务。通过DatagramSocket对象。DatagramSocket(int port)可以绑定本地端口
DatagramSocket ds = new DatagramSocket(); // 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("127.0.0.1"), ); // 3,通过socket服务,将已有的数据包发送出去。通过send方法。
ds.send(dp); // 4,关闭资源。 ds.close(); }
}
class UdpRece {
public static void main(String[] args) throws Exception {
// 1,创建udp socket,建立端点。
DatagramSocket ds = new DatagramSocket();
while (true) {
// 2,定义数据包。用于存储数据。
byte[] buf = new byte[];
DatagramPacket dp = new DatagramPacket(buf, buf.length); // 3,通过服务的receive方法将收到数据存入数据包中。
ds.receive(dp);// 阻塞式方法。 // 4,通过数据包的方法获取其中的数据。 获取发送端端口
String ip = dp.getAddress().getHostAddress(); String data = new String(dp.getData(), , dp.getLength()); int port = dp.getPort(); System.out.println(ip + "::" + data + "::" + port); }
// 5,关闭资源
// ds.close(); }
}
/*
udp多线程实现 编写一个聊天程序。
有收数据的部分,和发数据的部分。
这两部分需要同时执行。
那就需要用到多线程技术。
一个线程控制收,一个线程控制发。 因为收和发动作是不一致的,所以要定义两个run方法。
而且这两个方法要封装到不同的类中。 */
import java.io.*;
import java.net.*;
class Send implements 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)
{ byte[] buf = line.getBytes(); DatagramPacket dp =
new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),); ds.send(dp); if("".equals(line))
break;
}
}
catch (Exception e)
{
throw new RuntimeException("发送端失败");
}
}
} class Rece implements Runnable
{ private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
while(true)
{
byte[] buf = new byte[];
DatagramPacket dp = new DatagramPacket(buf,buf.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); String data = new String(dp.getData(),,dp.getLength()); if("".equals(data))
{
System.out.println(ip+"....离开聊天室");
break;
} System.out.println(ip+":"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("接收端失败");
}
}
} class ChatDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(); new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start(); }
}
上一篇:添加service到SystemService硬件服务


下一篇:windows平台下python 打包成exe可执行文件