一、网络通信基础
IP地址:唯一标识网络中设备,目前用的是32位,形式为“A,B,C,D”,例如:“192.168.0.1”
端口号:在网络中标识不同的应用http-80H,ftp-2H
查看端口占用情况,在命令执行中,netstat-naolfindstar“xxxx”
进程编号可以通过任务管理器查看
TCP:建立连接后再传输数据,是可靠的。适合传输文件,不能有任何遗漏和错误。
UDP:传输数据无需建立连接,是不可靠的,但速度快。适合音视频传输,因为少量丢包影响不大。
InetAddress类
public class Demo {
public static void main(String[] args)throws UnknownHostException {
//InetAddress输入地址 getLocalHost获取本地主机
InetAddress ip=InetAddress.getLocalHost();
//InetAddress ip=InetAddress.getByName("www.baidu.com");
System.out.println("IP地址:"+ip.getHostAddress());//HostAddress主机地址
System.out.println("主机的名称:"+ip.getHostName());
}
}
运行结果:
二、 UDP通信
UDP 是用户数据报协议的缩写,一个无连接的协议。提供了应用程序之间要发送的数据的数据包
如图所示:
发送端代码如下:
System.out.println("发送端");
//DatagramSocket数据插座
DatagramSocket sendSocket=new DatagramSocket();
//要发送的信息
byte[] bytes="将要下课了".getBytes();
//目标--收货地址
InetAddress inetAddress=InetAddress.getByName("192.168.105.18");
//数据报文
DatagramPacket datagramPacket=new DatagramPacket(bytes,bytes.length,inetAddress,5200);
//发送了
sendSocket.send(datagramPacket);
//关闭
sendSocket.close();
}
接收端:
public static void main(String[] args)throws IOException{
System.out.println("接收端");
DatagramSocket receiveSocket=new DatagramSocket(200);
while (true){
byte[] buff=new byte[1024];
//创建一个报文,用来接收传过来的报文
DatagramPacket packet=new DatagramPacket(buff,buff.length);
//接收
receiveSocket.receive(packet);
InetAddress ip=packet.getAddress();
System.out.println(ip.getHostAddress()+"---"+new
String(packet.getData(),0,packet.getLength()));
receiveSocket.close();
}
}
三、TCP通信
TCP 是传输控制协议的缩写,它保障了两个应用程序之间的可靠通信。通常用于互联网协议,被称 TCP / IP。
socket(套接字使用TCP提供了两台计算机之间的通信机制)
public static void main(String[] args) {
String serverName=args[0];
int port=Integer.parseInt(args[1]);
try{//尝试
System.out.println("连接到主机:"+serverName+",端口号:"+port);
Socket client=new Socket(serverName,port);
System.out.println("远程主机地址:"+client.getRemoteSocketAddress());//getRemoteSocketAddress获取远程地址
OutputStream outputStream=client.getOutputStream();// OutputStream 输出流
DataOutputStream out=new DataOutputStream(outputStream);//DataOutputStream数据输出流
out.writeUTF("Hello from"+client.getLocalSocketAddress());//getLocalSocketAddress获取本地地址
//InputStream输入流
InputStream inFromServer=client.getInputStream();//getInputStream获取输入流
DataInputStream in=new DataInputStream(inFromServer);//DataInputStream数据输入流
System.out.println("服务武器响应:"+in.readUTF());
client.close();
}catch (IOException e){//抓住、接住
e.printStackTrace();//printStackTrace打印跟踪
}
}
客户端的编写
代码如下:
public static void main(String[] args)throws IOException {
System.out.println("客户端socket");
Socket socket = new Socket("192.168.31.103", 2018);
while (true) {
OutputStream out = socket.getOutputStream();
Scanner sc=new Scanner(System.in);
String s=sc.next();
out.write(s.getBytes());
//通过输入流,拿到服务器端返回的数据
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
inputStream.read(bytes);
System.out.println("服务端返回:" + new String(bytes));
// socket.close();
}
}
运行结果:
服务端的编写
代码如下:
public static void main(String[] args)throws IOException{
System.out.println("服务端socket");
ServerSocket serverSocket=new ServerSocket(2018);
Socket socket = serverSocket.accept();//谁链接我,我就能拿到连接我的客户端socket
InetAddress ip = socket.getInetAddress();
InputStream input = socket.getInputStream();//从客户端输出流
byte[] buff = new byte[1024];
;while (true) {
input.read(buff);
String str = String.format("%s:%s", ip.getHostAddress(), new String(buff));//客户端发送过来的
System.out.println(str);
Scanner sc=new Scanner(System.in);
String s=sc.next();
OutputStream output = socket.getOutputStream();
output.write(s.getBytes());
}
}
}
运行结果: