Java中的InetAddress代表地址,主要是代表ip地址
获取地址的对象
(1)public static InetAddress getLocalhost() throws UnkownHostException:获取本地的主机
(2)public static InetAddress getByName(String host)throws UnkownHostException:依据主机名(Ip地址)获取主机
通过地址对象获取ip字符串或者是主机的名称
(1)public static String getHostName():获取主机的名字
(2)public static String getHostAddress():获取主机的字符串形式ip
UDP是一个传输协议
特点:传输速度快,但不稳定,无连接,存在丢包。
通常用于不是非常要求专业的传输,比如微信语音和视频,网络游戏
使用UDP的步骤:
1、创建连接:DatagramSocket(int port):指定端口号,方便发送端寻找
2、建立数据包,用于接收数据:DatagramSocket(byte[] buf,int length)
3、调用socket的方法
4、解析数据
5、关闭socket
package cn.ant.demoNet;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
/*
* 发送数据
* */
public class UDPClient {
public static void main(String[] args) throws IOException {
//建立连接
DatagramSocket ds=new DatagramSocket();
Scanner sc=new Scanner(System.in);
//循环发送
while(true){
//创建数据
System.out.println("请输入数据:");
String s=sc.next();
//打包数据
byte[] data=s.getBytes();
int length=data.length;
//连接服务器的ip地址和端口
InetAddress address=InetAddress.getByName("localhost");
int port=5650;
//打包
DatagramPacket dp=new DatagramPacket(data, length,address,port);
//调用方法
ds.send(dp);
}
//关闭
// ds.close();
}
}
package cn.ant.demoNet;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPServer {
public static void main(String[] args) throws Exception {
//只接收数据
//1、建立连接
DatagramSocket ds=new DatagramSocket(45377);
//反复接收数据
while(true){
//建立数据包 用于接收数据
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
//调用socket方法接收数据
ds.receive(dp);
//解析数据 针对数据包进行解析 --dp\
InetAddress sendAddress=dp.getAddress();
String sendIP=sendAddress.getHostAddress();
int sendPort=dp.getPort();
//获取数据包中的数据
byte[] data=dp.getData();
int length=dp.getLength();
//将字节数据中的数据转换成字符串
String content=new String(data,0,length);
//输出
System.out.println("接收到来自"+sendIP+"的,端口号为"+sendPort+"的数据:"+content);
}
//关闭
// ds.close();
}
}
Java中提供了Socket进行网络传输
TCP是一个传输协议
package cn.ant.demoNet;
import java.net.*;
import java.io.*;
public class MyServer extends Thread{
public static void main(String[] args) throws IOException {
//建立服务器端
ServerSocket serverSocket=new ServerSocket(5650);
//当前是服务器端 所以获取io流的同时需要等待客户端发送数据
Socket socket=serverSocket.accept();
//
System.out.println(socket.getInetAddress().getHostAddress());
//建立连接之后,通过socket中的io流,进行数据传输
InputStream is=socket.getInputStream();
//读取反复读取客户端发送过来的数据
int read=-1;
while((read=is.read())!=-1){
System.out.print((char)read);
}
//接收到客户端的数据之后 返回一个数据给客户端
OutputStream os=socket.getOutputStream();
String content="o";
os.write(content.getBytes());
//关闭
socket.close();
serverSocket.close();
}
}
package cn.ant.demoNet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyClients {
public static void main(String[] args) throws IOException, IOException {
// TODO Auto-generated method stub
//锟斤拷锟斤拷锟酵伙拷锟斤拷
Socket socket=new Socket("127.0.0.1",5650);
//锟斤拷锟斤拷锟斤拷锟接猴拷始锟斤拷锟斤拷
OutputStream os=socket.getOutputStream();
// //请求
// string req = "";
// req += "GET " + res + " HTTP/1.1\r\n";
// req += "Host: " + host + "\r\n";
// req += "User-Agent:*\r\n";
// req += "Connection:Close\r\n";
// req += "\r\n";
//锟斤拷锟斤拷锟斤拷锟斤拷
String content="I love Java 锟斤拷锟斤拷锟芥拻";
os.write(content.getBytes());
//锟斤拷锟竭凤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
socket.shutdownOutput();
//锟斤拷取锟斤拷锟斤拷锟斤拷锟剿碉拷锟斤拷应--o
InputStream is=socket.getInputStream();
int read=-1;
while((read=is.read())!=-1){
System.out.print((char)read);
}
socket.close();
}
}