网络编程
TCP编程
面向连接的协议,可靠传输,双向通信
// 方式1获取本地的ip地址
InetAddress address = InetAddress.getLocalHost();
// 方式2:获取指定网站或者ip的地址
// InetAddress address = InetAddress.getByName("192.168.1.1");
// 返回ip地址
System.out.println(address + "此ip地址为" + address.getHostAddress());
// 返回计算机名称
System.out.println("此ip的计算机名称" + address.getHostName());Process process = Runtime.getRuntime().exec("ping www.github.com");try (// 获取字节输入流,将其使用转换流转换成字符流,然后放入缓冲字符输入流---一行一行的读取InputStream is = process.getInputStream();InputStreamReader isr = new InputStreamReader(is, "GBK");BufferedReader br = new BufferedReader(isr)) {String str;while ((str = br.readLine()) != null) {System.out.println(str);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
单发单收
package day28_网络编程_TCP;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
//TCP:面向连接的协议,可靠传输,双向通信
public class Client01 {public static void main(String[] args) throws IOException {System.out.println("这个是客户端");// 1.创建Socket对象// 在创建对象的同时,连接服务器Socket socket = new Socket("127.0.0.1", 1025);// 2.获取输出流,写数据OutputStream os = socket.getOutputStream();os.write("测试".getBytes());// 3.释放资源os.close();socket.close();}
}
package day28_网络编程_TCP;import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;public class Server01 {public static void main(String[] args) throws IOException {System.out.println("这个是服务器端");// 1.创建服务器端Socket对象serverSocketServerSocket serverSocket = new ServerSocket(1025);// 2.监听客户端发送的消息Socket socket = serverSocket.accept();// 3.获取输入流,读取数据并将数据显示在控制台上InputStream is = socket.getInputStream();int len;byte[] bytes = new byte[15];while ((is.read(bytes)) != -1) {System.out.println(new String(bytes));}// 4.释放资源socket.close();serverSocket.close();}
}
多发多收
package day28_网络编程_TCP;import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;//多收
public class Server02 {public static void main(String[] args) throws IOException {System.out.println("这个是服务器端");// 1.创建服务器端Socket对象serverSocketServerSocket serverSocket = new ServerSocket(1025);// 2.监听客户端发送的消息Socket socket = serverSocket.accept();// 3.获取输入流,读取数据并将数据显示在控制台上InputStreamReader isr = new InputStreamReader(socket.getInputStream());int ch;while ((ch = isr.read()) != -1) {System.out.print((char) ch);}// 4.释放资源socket.close();serverSocket.close();}
}
package day28_网络编程_TCP;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;public class Client02 {public static void main(String[] args) throws IOException {System.out.println("这个是客户端");// 1.创建Socket对象// 在创建对象的同时,连接服务器Socket socket = new Socket("127.0.0.1", 1025);// 2.获取输出流,写数据OutputStream os = socket.getOutputStream();Scanner sc=new Scanner(System.in);while(true) {System.out.println("请你输入想要发送到服务器端的内容");String str=sc.nextLine();if("886".equals(str)) {break;}os.write(str.getBytes());}// 3.释放资源socket.close();sc.close();}
}
练习题1
客户端:发送一条消息给服务器端,接口服务器的返回并进行消息打印
package day28_网络编程_TCP;import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;//客户端:发送一条消息给服务器端,端口服务器的返回并进行消息 打印
public class Server03 {public static void main(String[] args) throws IOException {// 1.创建ServerSocket对象ServerSocket serverSocket = new ServerSocket(9999);// 2.监听并等待客户端连接Socket socket = serverSocket.accept();// 3.读取数据InputStreamReader isr = new InputStreamReader(socket.getInputStream());// read方法会从连接通道读取数据// 但是,需要有结束标志,此处循环才会停止// 否则,程序会一直停在read方法这里,等待读取下面的数据int ch;while ((ch = isr.read()) != -1) {System.out.print((char) ch);}// 4.反馈--回写消息OutputStream os = socket.getOutputStream();os.write("收到消息啦".getBytes());// 5.释放资源socket.close();}
}
package day28_网络编程_TCP;import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;//客户端:发送一条消息给服务器端,接口服务器的返回并进行消息打印
public class Client03 {public static void main(String[] args) throws IOException {// 1.创建Socket对象Socket socket = new Socket("127.0.0.1", 9999);// 2.写出数据OutputStream os = socket.getOutputStream();os.write("见到你很开心".getBytes());// 写出一个结束标识socket.shutdownOutput();// 3.接收服务器回显的消息
// InputStreamReader isr = new InputStreamReader(socket.getInputStream());
// int ch;
// while ((ch = isr.read()) != -1) {
// System.out.print((char) ch);
// }//4.释放资源socket.close();}
}
练习题2
传输文件————大文件传输
package day28_网络编程_TCP;import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;public class Server04_test {public static void main(String[] args) throws IOException {// 1.创建服务器端对象,绑定端口号ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务器端启动成功");// 2.等待客户端进行连接Socket socket = serverSocket.accept();// 3.读取客户端发来的数据OutputStream os = new BufferedOutputStream(new FileOutputStream("a.jpg"));InputStream is = socket.getInputStream();int len;byte[] bytes = new byte[1024];while ((len = is.read(bytes)) != -1) {os.write(bytes, 0, len);}System.out.println("服务器保存完毕");// 4.提示信息OutputStream message = socket.getOutputStream();message.write(("服务器:文件上传成功" + System.lineSeparator()).getBytes());// 5.关闭资源os.close();is.close();message.close();socket.close();serverSocket.close();System.out.println("服务器端关闭");}
}
package day28_网络编程_TCP;import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;public class Client04_test {public static void main(String[] args) throws UnknownHostException, IOException {// 创建客户端对象Socket socket = new Socket("127.0.0.1", 9999);System.out.println("客户端启动");// 2.先读取照片再输出照片InputStream is = new BufferedInputStream(new FileInputStream("E:\\apesourcefile\\maomao.jpg"));OutputStream os = socket.getOutputStream();int len;byte[] bytes = new byte[1024];while ((len = is.read(bytes)) != -1) {os.write(bytes, 0, len);}System.out.println("客户端上传完毕");// 写出一个结束标识socket.shutdownOutput();// 3.读取反馈信息InputStream mis = socket.getInputStream();InputStreamReader isr = new InputStreamReader(mis);int message;while ((message = isr.read()) != -1) {System.out.print((char) message);}// 4.关闭流is.close();os.close();isr.close();socket.close();System.out.println("客户端关闭");}
}
UDP
面向无连接的协议,不保证可靠传输
package day28_网络编程_UDP;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;public class ReceiveMessage {// UDP通信接收public static void main(String[] args) throws IOException {// 1.创建DatagramSocket对象,注意在接收端,并且要绑定端口号// 端口号和发送的端口号保持一致DatagramSocket ds = new DatagramSocket(9999);// 2.接收数据包byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf, buf.length);// 程序在该方法是阻塞的,程序执行到这一步会死等// 等待发送端发送消息// 3.解析数据包ds.receive(dp);byte[] data = dp.getData();InetAddress address = dp.getAddress();int port = dp.getPort();System.out.println("IP地址为:" + address.getHostAddress() + "端口号为:" + port + "客户端发送的消息为:" + new String(data));// 4.资源释放ds.close();System.out.println("服务器端关闭");}
}
package day28_网络编程_UDP;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;//UDP:面向无连接的协议,不保证可靠传输
public class SendMessage {public static void main(String[] args) throws IOException {// 1.DatagramSocket// 细节:绑定端口号:以后消息的发送就是通过此端口号进行发送// 空参:随机找空闲// 有参:指定端口号来进行消息的发送DatagramSocket ds = new DatagramSocket();// 2.打包 DatagramPacketbyte[] buf = "爪爪吃蒸饺".getBytes();InetAddress address = InetAddress.getLocalHost();// 获取本机地址int port = 9999;DatagramPacket dp = new DatagramPacket(buf, buf.length, address, port);// 3.发送ds.send(dp);// 4.资源关闭ds.close();System.out.println("客户端发送消息完毕");}
}
广播
package day29_UDP_广播;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class ReceiveMessage {public static void main(String[] args) throws IOException {System.out.println("接收端");// 1.创建DatagramSocket对象DatagramSocket ds = new DatagramSocket(9999);/// 2.打包消息DatagramPacketbyte[] bytes = new byte[1024];DatagramPacket dp = new DatagramPacket(bytes, bytes.length);// 3.接收包并进行数据的分析while (true) {ds.receive(dp);System.out.println("发送的消息为" + new String(dp.getData(), 0, dp.getLength()));System.out.println("发送的端口号为" + dp.getPort());}}
}
package day29_UDP_广播;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;public class SendMessage {public static void main(String[] args) throws IOException {System.out.println("发送端");// 1.创建DatagramSocket对象DatagramSocket ds = new DatagramSocket();Scanner sc = new Scanner(System.in);while (true) {System.out.println("请输入想发送的消息");String str = sc.nextLine();if ("886".equals(str)) {break;}// 2.打包的数据包byte[] message = str.getBytes();InetAddress address = InetAddress.getLocalHost();int port = 9999;DatagramPacket dp = new DatagramPacket(message, message.length, address, port);// 3.发送ds.send(dp);}// 4.资源关闭}
}
组播
package day29_UDP_组播;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;//c类地址
//组播地址:224.0.0.0~239.255.255.255
//预留组播地址224.0.0.0~224.0.0.255//广播地址:255.255.255.255
public class SendMessage {public static void main(String[] args) throws IOException {System.out.println("组播发送端");// 1.创建组播对象MulticastSocket ms = new MulticastSocket();// 2.准备发送的数据包byte[] message = "今天中午吃什么?".getBytes();InetAddress address = InetAddress.getByName("224.0.1.0");int port = 9999;DatagramPacket dp = new DatagramPacket(message, message.length, address, port);// 3.发送组播消息ms.send(dp);// 4.释放资源ms.close();System.out.println("组播消息发送完成");}
}
package day29_UDP_组播;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;public class ReceiveMessage01 {public static void main(String[] args) throws IOException {System.out.println("组播接收端01");// 1.创建组播对象MulticastSocket ms = new MulticastSocket(9999);// 2.将当前的本机地址添加到224.0.1.0(同一组)中InetAddress address = InetAddress.getByName("224.0.1.0");ms.joinGroup(address);// 3.创建对象接收消息byte[] bytes = new byte[1024];DatagramPacket dp = new DatagramPacket(bytes, bytes.length);// 4.接收组播消息并解析ms.receive(dp);System.out.println("发送的消息为" + new String(dp.getData(), 0, dp.getLength()));System.out.println("发送端口号" + dp.getPort());// 5.释放资源ms.close();}
}
package day29_UDP_组播;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;public class ReceiveMessage02 {public static void main(String[] args) throws IOException {System.out.println("组播接收端02");// 1.创建组播对象MulticastSocket ms = new MulticastSocket(9999);// 2.将当前的本机地址添加到224.0.1.0(同一组)中InetAddress address = InetAddress.getByName("224.0.1.0");ms.joinGroup(address);// 3.创建对象接收消息byte[] bytes = new byte[1024];DatagramPacket dp = new DatagramPacket(bytes, bytes.length);// 4.接收组播消息并解析ms.receive(dp);System.out.println("发送的消息为" + new String(dp.getData(), 0, dp.getLength()));System.out.println("发送端口号" + dp.getPort());// 5.释放资源ms.close();}
}
package day29_UDP_组播;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;public class ReceiveMessage03 {public static void main(String[] args) throws IOException {System.out.println("组播接收端03");// 1.创建组播对象MulticastSocket ms = new MulticastSocket(9999);// 2.将当前的本机地址添加到224.0.1.0(同一组)中InetAddress address = InetAddress.getByName("224.0.2.1");ms.joinGroup(address);// 3.创建对象接收消息byte[] bytes = new byte[1024];DatagramPacket dp = new DatagramPacket(bytes, bytes.length);// 4.接收组播消息并解析ms.receive(dp);System.out.println("发送的消息为" + new String(dp.getData(), 0, dp.getLength()));System.out.println("发送端口号" + dp.getPort());// 5.释放资源ms.close();}
}