1.网络编程的三要素
A:IP地址
B:端口
是应用程序的标示。范围:0-65535,其中0-1024被系统占用或保留,不建议使用
C:协议
UDP:数据打包传输,包大小有限制,不连接,效率高,传输中可能会丢包,不可靠
TCP:建立数据传输通道,数据大小无限制,效率低,可靠
2.UDP
A:最基本的UDP协议发送和接收数据
发送端:
public class SendDemo{
public static void main(String[] args) throws IOException{
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//创建IP地址对象
InetAddress address = InetAddress.getByName("192.168.1.124");
//创建数据并打包
byte[] bys = "hello,我来了!".getBytes();
int length = bys.length;
int port = 10088;
DatagramPacket dp = new DatagramPacket(bys,length,address,port);
//调用Socket对象的发送端发送数据
ds.send(dp);
//释放资源
ds.close();
}
}
发送端简化版:
public class SendDemo{
public static void main(String[] args) throws IOException {
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//创建数据并打包
byte[] bys = "hello,大家好!".getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),12345);
//发送数据
ds.send(dp);
//释放资源
ds.close();
}
}
接收端:
public class ReceiveDemo{
public static void main(String[] args) throws IOException{
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(10088);
//创建一个接收数据包(接收容器)
byte[] bys = new byte[1024];
int length = bys.length;
DatagramPacket dp = new DatagramPacket(bys,length);
//调用Socket对象的接收方法接收数据
ds.receive(dp);
//解析数据包并显示在控制台上
InetAddress address = dp.getAddress();
String IP = address.getHostAddress();
byte[] bys2 = dp.getData();
int len = dp.getLength();
String str = new String(bys2,o,len);
System.out.println(IP +"传递的数据是:"+ str);
//释放资源
ds.close();
}
}
接收端简化版:
public class ReceiveDemo{
public static void main(String[] args) throws IOException {
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(12345);
//创建一个接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据并打印
String IP = dp.getAddress().getHostAddress();
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(IP + "传递的数据是:" + str);
//释放资源
ds.close();
}
}
B:键盘录入数据发送和接收
发送端:
public calss SendDemo{
public static void main(String[] args) throws IOException {
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
//创建数据并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),10011);
//发送数据
ds.send(dp);
}
//关闭资源
}
}
接收端:
public calss ReceiveDemo{
public static void main(String[] args) throws IOException{
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(10011);
while(true){
//创建接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据
String IP = dp.getAdress().getHostAdress();
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(IP + "传输的数据是:" + str)
}
}
}
C:使用多线程实现同一个窗口下数据的接收和发送(模拟聊天窗口)
数据发送线程:
public class SendThread implements Runnable{
private DatagramSocket ds;
public SendThread(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
//封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
//创建数据并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),10068);
//发送数据
ds.send(dp);
}
//释放资源
ds.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
数据接收线程:
public class ReceiveThread implements Runnable {
private DatagramSocket ds;
pubic ReceiveThread(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
while(true){
//创建一个接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据
String IP = dp.getAddress().getHostAddress();
String str = new String(dp.getDate(),0,dp.getLength());
System.out.println(IP + " : " + str);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
聊天室:
public class ChatRoom{
public static void main(String[] args) throws IOException{
//创建发送端和接收端的Socket对象
DatagramSocket dsSend = new DatagramSocket();
DategramSocket dsReceive = new DatagramSocket(12345);
//创建发送端和接收端的资源对象
SendThread st = new SendThread(dsSend);
ReceiveThread rt = new ReceiveThread(dsReceive);
//创建线程
Thread t1 = new Thread(st);
Thread t2 = new Thread(rt);
//开启线程
t1.start();
t2.start();
}
}
3.TCP
A:最基本的TCP协议发送和接收数据
客户端:
public static ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",8888);
//创建输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,TCP,我来了!".getBytes());
//释放资源
s.close();
}
}
服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端Socket对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//创建输入流,读取数据并显示在控制台
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String str = new String(bys,0,len);
String IP = s.getInetAddress().getHostAddress();
System.out.println(IP + " : " + str);
//释放资源
s.close();
}
}
B:服务器端给出反馈信息
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10089);
//获取输出流对象,写出数据
OutputStream os = s.getOutputStream();
os.write("天气不错,适合睡觉!".getBytes());
//获取输入流对象,读入数据并显示
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String client = new String(bys,0,len);
System.out.println("client: "+client);
//关闭资源
s.close();
}
}
服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(10089);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//获取输入流对象,读入数据并显示
InputStream is = s.InputStream();
byte[] bys = new byte[1024];
len = is.read(bys);
String server = new String(bys,0,len);
System.out.println("server: " + server);
//获取输出流对象,写出反馈信息
OutputStream os = s.getOutputStream();
os.write("数据已收到!".getBytes());
//关闭资源
s.close();
}
} |
|