javaSE(22)(网络编程)

UDP发送端:
package zz.itheima.netprogram;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

//给予UDP的发送端
public class Sender {

    public static void main(String[] args) throws Exception{
// 1.创建DatagramSocket对象用来发送数据
        DatagramSocket ds = new DatagramSocket();
// 2.准备好数据
        String msg = "要钱";
// 3.创建DatagramPacket数据报对象用来存储数据
        DatagramPacket dp = new DatagramPacket(msg.getBytes(), msg.getBytes().length, InetAddress.getByName("127.0.0.1"),3000);
// 4.发送
        ds.send(dp);
// 5.关闭DatagramSocket对象
        ds.close();
    }

}
UDP的接收端:
package zz.itheima.netprogram;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

//基于UDP的接收端
public class Receiver {

    public static void main(String[] args) throws Exception {

// 1.创建DatagramSocket对象用来接收数据
        DatagramSocket ds = new DatagramSocket();
// 2.创建DatagramPacket数据报
        byte[] msg = new byte[30];
        DatagramPacket dp = new DatagramPacket(msg, msg.length);
        //对象用来存储数据
// 3.接收
        ds.receive(dp);
// 4.读取数据
        System.out.println(new String(msg).trim());
// 5.关闭DatagramSocket对象
        ds.close();

    }

}
TcpClient:
package zz.itheima.netprogram;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
//客户端
import java.util.Scanner;

public class TcpClient {

    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        while (true) {
            //1.客户手机
            Socket s = new Socket("127.0.0.1",9000);
            //2.主动向服务器端发送请求
            OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
            System.out.println("请输入信息:");
            String msg = sc.nextLine();
            osw.write(msg);
            osw.flush();
            //3.接受服务器端的响应信息
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            char[] temp = new char[20];
            isr.read(temp);
            System.out.println("服务器说:"+new String(temp).trim());
            //4关闭
            s.close();
        }

    }

}
TcpServer:
package zz.itheima.netprogram;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

//服务器端
public class TcpServer {

    public static void main(String[] args) throws Exception{
        //1.创建一个秘书
        ServerSocket ss = new ServerSocket(9000);
        while (true) {
            //2.得到一个手机,等电话
            Socket s = ss.accept();
            //3.接受客户端发过来的消息
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            char[] temp = new char[20];
            isr.read(temp);
            System.out.println("客户端说:"+new String(temp).trim());
            //4.响应(回复)客户端
            OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
            osw.write("没钱!!!");
            osw.flush();
            //5.关闭
            s.close();


        }

    }

}
TestInetAddress:
package zz.itheima.netprogram;

import java.net.InetAddress;

public class TestInetAddress {


    public static void main(String[] args) throws Exception {

        InetAddress localIP=InetAddress.getLocalHost();   //本机IP对象

        InetAddress RemoteIP=InetAddress.getByName("192.168.0.28");   //对方ip对象

        System.out.println(localIP.getHostAddress());   //得到ip
        System.out.println(localIP);   //得到ip
        System.out.println(RemoteIP.getHostAddress());  //得到ip

    }

}
MyUDPQQ:
package zz.itheima.netprogram;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;


public class MyUDPQQ {

    //发送消息
    public void sendMsg() throws Exception{
// 1.创建DatagramSocket对象用来发送数据
        DatagramSocket ds=new DatagramSocket();
// 2.准备好数据
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入消息:");
        String msg=sc.nextLine();
// 3.创建DatagramPacket数据报对象用来存储数据
        DatagramPacket dp=new DatagramPacket(msg.getBytes(), msg.getBytes().length, InetAddress.getByName("127.0.0.1"), 3000);
// 4.发送
        ds.send(dp);
// 5.关闭DatagramSocket对象
        ds.close();
    }
    //接收消息
    public void receiveMsg()throws Exception{
// 1.创建DatagramSocket对象用来接收数据
        DatagramSocket ds=new DatagramSocket(3000);
// 2.创建DatagramPacket数据报
        byte[] msg=new byte[30];
        DatagramPacket dp=new DatagramPacket(msg,msg.length);
// 对象用来存储数据
// 3.接收
        ds.receive(dp);
// 4.读取数据
        System.out.println(new String(msg).trim());
// 5.关闭DatagramSocket对象
        ds.close();
        Thread.sleep(1000);
    }
}
TestUDPQQ:
package zz.itheima.netprogram;

public class TestUDPQQ {
    //同学一电脑:
    public static void main(String[] args) throws Exception{
        MyUDPQQ ud  = new MyUDPQQ();
        while (true) {
            ud.sendMsg();
        }
    }

}
TestUDPQQ2:
package zz.itheima.netprogram;
//这是同学2的电脑
public class TestUDPQQ2 {

    public static void main(String[] args) throws Exception{
        MyUDPQQ ud=new MyUDPQQ();
        while (true) {
            ud.receiveMsg();
        }

    }

}
UploadServer:
package zz.itheima.netprogram;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
//上传服务器
public class UploadServer {

    public static void main(String[] args) {
        //1.秘书
                try {
                    ServerSocket ss=new ServerSocket(9999);
                    while (true) {
                        //2.等电话
                        Socket s = ss.accept();
                        //3.接收客户端发过来的消息
                        InputStream is = s.getInputStream();
                        FileOutputStream fos=new FileOutputStream("e:\\ppts\\1.jpg");
                        byte[] temp=new byte[1024];
                        while(true)
                        {
                            int length=is.read(temp);
                            if(length==-1)
                                break;
                            else {
                                fos.write(temp, 0, length);
                            }
                        }
                        fos.close();

                        OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream());
                        osw.write("上传成功");
                        osw.flush();

                        s.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

    }

}

你可能感兴趣的:(java,网络编程,UDP,se)