Socket
Socket就是为网络服务提供的一种机制
通信的两端都有Socket
网络通信其实就是Socket间的通信
数据在两个Socket间通过IO传输
UDP
将数据及源和目的的封装成数据包中,不需要建立连接
每个数据包的大小限制在64k内
因无连接,是不可靠的协议
不需要建立连接,速度快
Udp的使用:
例子:通过udp实现网络通讯(实现两台机的对话)
发送端步骤:
1.建立udpsocket服务。
2.提供数据,并将数据封装到数据包中
3.通过socket服务的发送功能,将数据包发出去
4.关闭资源
接收端步骤:
1.定义udpsocket服务
2.定义一个数据包,用来要存储接收到的字节数据(数据包对象中有更多的功能提取字节数据中的不同数据信息)
3.通过socket服务的reseive方法将收到的数据存储入已定义好的数据包中
4.通过数据包对象特有的功能,将这些不同的数据取出
5.关闭资源
A机:
发送类(UdpSend.java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpSend implements Runnable {
private static DatagramSocket ds;
public UdpSend(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(in);
String say = null;
while ((say = buffer.readLine()) != null) {
byte[] buf = say.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("192.168.144.1"), 10002);
ds.send(dp);
}
} catch (IOException e) {
System.out.println("发送失败");
}
}
}
接收类(UpdRece.java)
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UpdRece implements Runnable {
private static DatagramSocket ds;
public UpdRece(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
// 创建udp socket,建立端点
// ds = new DatagramSocket(10000);
while (true) {
// 定义数据包,用于存储数据
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// 通过服务的receive方法将收到数据存入数据包中
ds.receive(dp);
// 通过数据包的方法获取其中的数据
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, dp.getLength());
int port = dp.getPort();
System.out.println(ip + ":" + port + " say:\n " + data);
// 关闭资源
// ds.close();
}
} catch (Exception e) {
System.out.println("接收失败");
}
}
}
3.入口类
import java.net.DatagramSocket;
import java.net.SocketException;
public class IntentClient {
/**
* @param args
* @throws SocketException
*/
public static void main(String[] args) throws Exception {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(10002); // 接收10002端口的数据
new Thread(new UdpSend(sendSocket)).start();
new Thread(new UpdRece(receSocket)).start();
}
}
B机
发送类(UdpSend.java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpSend implements Runnable {
private static DatagramSocket ds;
public UdpSend(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(in);
String say = null;
while ((say = buffer.readLine()) != null) {
byte[] buf = say.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("192.168.144.1"), 10002);
ds.send(dp);
}
} catch (IOException e) {
System.out.println("发送失败");
}
}
}
接收类(UpdRece.java)
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UpdRece implements Runnable {
private static DatagramSocket ds;
public UpdRece(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
// 创建udp socket,建立端点
// ds = new DatagramSocket(10000);
while (true) {
// 定义数据包,用于存储数据
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// 通过服务的receive方法将收到数据存入数据包中
ds.receive(dp);
// 通过数据包的方法获取其中的数据
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, dp.getLength());
int port = dp.getPort();
System.out.println(ip + ":" + port + " say:\n " + data);
// 关闭资源
// ds.close();
}
} catch (Exception e) {
System.out.println("接收失败");
}
}
}
4.入口类
import java.net.DatagramSocket;
import java.net.SocketException;
public class IntentClient {
/**
* @param args
* @throws SocketException
*/
public static void main(String[] args) throws Exception {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(10002); // 接收10002端口的数据
new Thread(new UdpSend(sendSocket)).start();
new Thread(new UpdRece(receSocket)).start();
}
}