——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——
ds=new DatagramSocket()即可做发送,也可做接收
字节数组byte [] by=new byte[1024];
数据包 DatagramPacket dp=new DatagramPacket(by,by.length,InetAddress.getByName(“localhost”),8080);
DatagramPacket dp2=new DatagramPacket(by,by.length);
ds. send(dp);发送,ds.receive(dp2)接收
/**
* @author myluo
* 网络编程
*1:ip(通过ip确定主机)
*2:端口(通过端口确定应用程序1024以上)
*3:协议(TCP/UDP)(前者安全效率低如打电话:后者效率高但不安全如发短信)
*4:Socket编程(应用的插座)
package com.theima.www;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class IPDemo1 {
public static void main(String[] args) {
//网络编程基本方法
// method1();
//udp协议下的数据包的发送
// udpSendMethod();
//udp协议下的数据发送
udpSendmethod2();
}
//udp协议下的数据发送
private static void udpSendmethod2() {
// TODO Auto-generated method stub
try {
DatagramSocket ds=new DatagramSocket(5555);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String len=null;
try {
while((len=br.readLine())!=null){
if("over".equals(len)){
break;
}
byte [] by=len.getBytes();
DatagramPacket dp=new DatagramPacket(by,by.length,InetAddress.getByName("localhost"),7777);
ds.send(dp);
}
ds.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//udp数据包的发送
private static void udpSendMethod() {
// TODO Auto-generated method stub
DatagramSocket ds=null;
try {
//创建数据包服务
ds=new DatagramSocket(8888);
//创建要发送的字节数组
byte[] by="测试数据包".getBytes();
try {
//创建数据包对象
DatagramPacket dp=new DatagramPacket(by,by.length,InetAddress.getByName("localhost"),9999);
try {
//发送数据包
ds.send(dp);
//关闭资源
ds.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//网络编程获取ip
private static void method1() {
// TODO Auto-generated method stub
try {
//根据主机的名字获取ip
InetAddress obj=InetAddress.getByName("www.baidu.com");
System.out.println("百度的主机名和ip"+obj);
System.out.println("百度的主机名"+obj.getHostName());
System.out.println("百度的ip"+obj.getHostAddress());
//获取本机的名字和ip
InetAddress ip=InetAddress.getLocalHost();
System.out.println("主机名和ip"+ip);
String name=ip.getHostName();
System.out.println("主机名:"+name);
String ip2=ip.getHostAddress();
System.out.println("ip=="+ip2);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.theima.www;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @author myluo
*
*/
public class IPDemo2 {
//udp协议下的信息接受
public static void main(String[] args) {
//创建服务对象,DatagramSocket 既能发信息也能收信息。
DatagramSocket ds=null;
try {
//指定已接受的端口
ds=new DatagramSocket(9999);
//创建要接受的字节数组
byte [] by =new byte[1024];
//创建接受的数据包对象
DatagramPacket dp=new DatagramPacket(by,by.length);
try {
//服务接受数据包
ds.receive(dp);
//创建InetAddress对象
InetAddress ip=dp.getAddress();
//获取数据
System.out.println("ip==="+ip.getLocalHost());
System.out.println("收到的信息:="+new String(dp.getData(),0,dp.getLength()));
System.out.println("那个端口号发过来的:"+dp.getPort());
//关闭资源
ds.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.theima.www;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @author myluo
*
*
*
*/
public class IODemo3 {
//udp之数据的接收
public static void main(String[] args) {
try {
DatagramSocket ds=new DatagramSocket(8888);
while(true){
byte [] by=new byte[1024];
DatagramPacket dp=new DatagramPacket(by,by.length);
try {
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
System.out.println(ip+" : "+data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.theima.www;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @author myluo
*
* 聊天程序
*
*/
public class IODemo4 {
public static void main(String[] args) {
try {
DatagramSocket ds=new DatagramSocket(4777);
DatagramSocket dgs=new DatagramSocket(4666);
new Thread(new Send(ds)).start();
new Thread(new Receive(dgs)).start();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//发送信息的线程
class Send implements Runnable{
//Socket 服务
private DatagramSocket ds;
public Send(DatagramSocket ds){
this.ds=ds;
}
public Send(){}
public void run(){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String len=null;
try {
while((len=br.readLine())!=null){
if("over".equals(len)){
break;
}
byte [] by=len.getBytes();
DatagramPacket dp=new DatagramPacket(by,by.length,InetAddress.getByName("localhost"),7777);
ds.send(dp);
}
ds.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//接收信息的线程
class Receive implements Runnable{
private DatagramSocket ds;
public Receive(DatagramSocket ds){
this.ds=ds;
}
public Receive(){}
public void run(){
while (true) {
byte[] by = new byte[1024];
DatagramPacket dp = new DatagramPacket(by, by.length);
try {
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data= new String(dp.getData(),0,dp.getLength());
System.out.println(ip+" : "+data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.theima.www;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @author myluo
*
* 聊天程序
*
*/
public class IODemo5 {
public static void main(String[] args) {
try {
DatagramSocket ds=new DatagramSocket(6666);
DatagramSocket dgs=new DatagramSocket(7777);
new Thread(new Send1(ds)).start();
//new Thread(new Receive1(dgs)).start();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//发送信息的线程
class Send1 implements Runnable{
//Socket 服务
private DatagramSocket ds;
public Send1(DatagramSocket ds){
this.ds=ds;
}
public Send1(){}
public void run(){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String len=null;
try {
while((len=br.readLine())!=null){
if("over".equals(len)){
break;
}
byte [] by=len.getBytes();
DatagramPacket dp=new DatagramPacket(by,by.length,InetAddress.getByName("localhost"),7777);
ds.send(dp);
}
ds.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//接收信息的线程
class Receive1 implements Runnable{
private DatagramSocket ds;
public Receive1(DatagramSocket ds){
this.ds=ds;
}
public Receive1(){}
public void run(){
while (true) {
byte[] by = new byte[1024];
DatagramPacket dp = new DatagramPacket(by, by.length);
try {
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data= new String(dp.getData(),0,dp.getLength());
System.out.println(ip+" : "+data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
原文地址:http://blog.csdn.net/myadmini/article/details/45965441