java 网络编程

来源:互联网 发布:桥林中学的网络 编辑:程序博客网 时间:2024/04/29 23:41

1.IP地址

public static void main(String[] args) throws UnknownHostException {        InetAddress ip = InetAddress.getByName("172.18.18.6");        System.out.println(ip.getHostName() + "---" + ip.getHostAddress());    }


2.TCP通信

主要使用到2个类: Socket(客服端)和ServerSocket


2.1 简单通信反馈

客服端给服务器发送一条消息,服务武收到后反馈客服端“copy that”

Client

public class ClientDemo {    public static void main(String[] args) throws IOException {        //1.创建客服端的Socket对象        Socket socket=new Socket("172.31.12.156",10086);        //2.获得输出流,写数据信息        OutputStream os=socket.getOutputStream();        os.write("jop".getBytes());        //获得通信输入流        InputStream is=socket.getInputStream();        byte[] bys=new byte[1024];        int len=is.read(bys);        String message=new String(bys,0,len);        System.out.println(message);        //3.释放资源        socket.close();;    }}

Server

public class ServerDemo {    public static void main(String[] args) throws IOException {        //1.创建服务端的Socket对象        ServerSocket ss = new ServerSocket(10086);        //2.监听客服端的请求,并返回一个通信socket        Socket socket = ss.accept(); //无客服端连接则阻塞        //3.获得通信输入流        InputStream is= socket.getInputStream();        byte[] bys=new byte[1024];        int len=is.read(bys); //无数据则阻塞        String message=new String(bys,0,len);        String ip=socket.getInetAddress().getHostAddress(); //获得客服端的ip地址        System.out.println(ip+": "+message);        //获得通信输出流        OutputStream os= socket.getOutputStream();        os.write("copy that".getBytes());        //4.释放通信socket        socket.close();    }}
2.2 文件上传

文件的read()可以使用null来判断是否读完,socket管道流没有null的概念,但是客服端可以使用socket.shutdownOutput(); //关闭soket管道写入流

Client

public class UploadClientDemo {    public static void main(String[] args) throws IOException {        FileInputStream  fis=new FileInputStream("D://mm.jpg");        Socket socket=new Socket("172.31.12.156", 10086);        OutputStream fos= socket.getOutputStream();        byte[] bys=new byte[1024];        int lenRead=0;        while ((lenRead = fis.read(bys))!=-1){            fos.write(bys,0,lenRead);        }        socket.shutdownOutput(); //关闭soket管道写入流        //接收服务器的反馈信息        BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));        String s= reader.readLine();        System.out.println(s);        reader.close();        socket.close();    }}

Server

public class UploadServerDemo {    public static void main(String[] args) throws IOException {        ServerSocket ss=new ServerSocket(10086);        Socket socket=ss.accept();        InputStream inputStream=socket.getInputStream();        FileOutputStream outputStream=new FileOutputStream("mm.txt");        byte[] bys=new byte[1024];        int lenRead=0;        while ((lenRead= inputStream.read(bys)) != -1) {            outputStream.write(bys,0,lenRead);        }        outputStream.close();        //反馈2Client        BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));        writer.write("upload successful");        writer.newLine();        writer.close();        socket.close();    }}


3.UDP通信

Send端

public class UDPSendDemo {    public static void main(String[] args) throws IOException {        //1.创建UDP数据报对象        DatagramSocket ds=new DatagramSocket();        //2.创建数据资源并打包        //DatagramPacket(byte[] buf, int length, InetAddress address, int port)        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));        String line=null;        while ((line=reader.readLine())!=null){            if("886".equals(line)) { //886退出                break;            }            byte[] bys=line.getBytes();            InetAddress address=InetAddress.getByName("172.31.12.156");            DatagramPacket dp=new DatagramPacket(bys,bys.length,address,10086);            //3.发送数据            ds.send(dp);        }        //4.释放资源        ds.close();    }}

Receive端

public class UPDReceiveDemo {    public static void main(String[] args) throws IOException {        //1.创建接收对象,它带一个端口        DatagramSocket ds = new DatagramSocket(10086);        //2.创建接收的数据包容器        byte[] bys = new byte[1024];        DatagramPacket dp = new DatagramPacket(bys, bys.length);        while (true) {            //3.接收数据信息            ds.receive(dp);            String message = new String(dp.getData(), 0, dp.getLength());            System.out.println(dp.getAddress() + ": " + message);        }    }}



0 0
原创粉丝点击