网络编程
InetAddress应用
package com.ding.demo1;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Description InetAddress应用
* @Author 丁帅帅
* @Date 21/07/28 16:55
* @Version 1.0
*/
public class Inetaddress {
public static void main(String[] args) throws UnknownHostException {
InetAddress address=InetAddress.getByName("y9000x");
String hostName = address.getHostName();
System.out.println("主机名"+hostName);
String ip = address.getHostAddress();
System.out.println("IP为"+ip);
}
}
UDP发送数据
package com.ding.demo2;
import java.io.IOException;
import java.net.*;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 17:10
* @Version 1.0
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
String s="送礼物";
byte[] bytes=s.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port= 10000;
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
ds.send(dp);
ds.close();
}
}
UDP接收数据
package com.ding.demo2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 19:33
* @Version 1.0
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10000);
byte [] bytes=new byte[1024];
DatagramPacket dp =new DatagramPacket(bytes,bytes.length);
ds.receive(dp);
int length=dp.getLength();
System.out.println(new String(bytes,0,length));
ds.close();
}
}
UDP例子
UDP发送数据:数据来自于键盘录入,直到输入的数据是886,发送数据结束
package com.ding.demo3;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 19:40
* @Version 1.0
*/
public class ClientDemo {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
while (true) {
String s=sc.nextLine();
if("886".equals(s)){
break;
}
byte[] bytes=s.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port=10000;
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
ds.send(dp);
}
ds.close();
}
}
package com.ding.demo3;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 20:16
* @Version 1.0
*/
public class ServerDemo {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(10000);
while (true){
byte[] bytes=new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
ds.receive(dp);
byte[] data=dp.getData();
int len=dp.getLength();
System.out.println(new String(data,0,len));
}
}
}
UDP的通讯方式
package com.ding.demo4;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 20:29
* @Version 1.0
*/
public class Client {
public static void main(String[] args) throws Exception{
DatagramSocket ds = new DatagramSocket();
String s = "hello 组播";
byte[] bytes = s.getBytes();
InetAddress address = InetAddress.getByName("224.0.1.0");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
ds.send(dp);
ds.close();
}
}
package com.ding.demo4;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 20:32
* @Version 1.0
*/
public class Server {
public static void main(String[] args) throws Exception{
MulticastSocket ms = new MulticastSocket(10000);
DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
//把当前计算机绑定一个组播地址,表示添加到这一组中.
ms.joinGroup(InetAddress.getByName("224.0.1.0"));
ms.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
ms.close();
}
}
package com.ding.demo5;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 20:41
* @Version 1.0
*/
public class Client {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String s="广播 hello";
byte[] bytes=s.getBytes();
InetAddress address = InetAddress.getByName("255.255.255.255");
int port=10000;
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
ds.send(dp);
ds.close();
}
}
package com.ding.demo5;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 20:45
* @Version 1.0
*/
public class Server {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(10000);
DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
ds.receive(dp);
byte[] data=dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
ds.close();
}
}
TCP发送数据
package com.ding.demo6;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 20:49
* @Version 1.0
*/
public class Client {
public static void main(String[] args) throws IOException {
//1,创建一个Socket对象
Socket socket = new Socket("127.0.0.1",10001);
//2.获取一个IO流开始写数据
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes());
/*while(true){
}*/
//3.释放资源
os.close();
socket.close();
}
}
TCP接收数据
package com.ding.demo6;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 20:52
* @Version 1.0
*/
public class Server {
public static void main(String[] args) throws IOException {
//1. 创建Socket对象
ServerSocket ss = new ServerSocket(10001);
//2. 等待客户端连接
System.out.println(111);
Socket accept = ss.accept();
System.out.println(222);
//3.获得输入流对象
InputStream is = accept.getInputStream();
int b;
while((b = is.read()) != -1){
System.out.print((char) b);
}
System.out.println("看看我执行了吗?");
//4.释放资源
is.close();
ss.close();
}
}
TCP例子
客户端:发送数据,接受服务器反馈
服务器:收到消息后给出反馈
package com.ding.demo7;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 21:07
* @Version 1.0
*/
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 10001);
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes());
socket.shutdownOutput();//仅关闭输出流,并写一个结束标记,对socket没有任何影响
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
os.close();
socket.close();
}
}
package com.ding.demo7;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 22:03
* @Version 1.0
*/
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10001);
Socket accept=ss.accept();
InputStream is = accept.getInputStream();
int b;
while ((b=is.read())!=-1){
System.out.println((char)b);
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
bw.write("你谁啊?");
bw.newLine();
bw.flush();
bw.close();
is.close();
accept.close();
ss.close();
}
}
TCP文件上传
package com.ding.demo8;
import java.io.*;
import java.net.Socket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 22:24
* @Version 1.0
*/
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",10001);
//是本地的流,用来读取本地文件的.
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("client\\1.jpg"));
//写到服务器 --- 网络中的流
OutputStream os = socket.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
int b;
while((b = bis.read())!=-1){
bos.write(b);//通过网络写到服务器中
}
bos.flush();
//给服务器一个结束标记,告诉服务器文件已经传输完毕
socket.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while((line = br.readLine()) !=null){
System.out.println(line);
}
bis.close();
socket.close();
}
}
package com.ding.demo8;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/28 22:39
* @Version 1.0
*/
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10001);
Socket accept = ss.accept();
//网络中的流,从客户端读取数据的
BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
//本地的IO流,把数据写到本地中,实现永久化存储
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("server\\copy.jpg"));
int b;
while((b = bis.read()) !=-1){
bos.write(b);
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
bw.write("上传成功");
bw.newLine();
bw.flush();
bos.close();
accept.close();
ss.close();
}
}