栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现tcp,udp消息传输

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现tcp,udp消息传输

java实现tcp,udp消息传输 tcp
public class Tcplearn1 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9999);
        OutputStream stream = socket.getOutputStream();

        FileInputStream fileInputStream = new FileInputStream(new File("1.jpg"));
        byte[] bytes = new byte[1024];
        int len;
        while ((len=fileInputStream.read(bytes))!=-1)
            stream.write(bytes,0,len);

        fileInputStream.close();
        stream.close();
        socket.close();
    }
}
public class Tcprecive {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        InputStream is = socket.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream(new File("2.jpg"));

        byte[] buffer = new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
            fileOutputStream.write(buffer,0,len);
        }


        fileOutputStream.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}
udp
public class udpclient {
    public static void main(String[] args) throws IOException {
        //建立一个socket
        DatagramSocket datagramSocket = new DatagramSocket();
        //建消息包
        String msg="hello server";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port=9090;
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
        //send
        datagramSocket.send(packet);

        datagramSocket.close();

    }
}
public class udpserver {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9090);

        byte[] bytes = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
        datagramSocket.receive(packet);//阻塞接收

        System.out.println(packet.getSocketAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));

        datagramSocket.close();
    }
}
输入消息传输
public class udpclient {
    public static void main(String[] args) throws IOException {
        //建立一个socket
        DatagramSocket datagramSocket = new DatagramSocket();
        //建消息包
        //控制台读取输入
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String readLine = reader.readLine();

        byte[] bytes = readLine.getBytes();
        InetAddress localhost = InetAddress.getByName("localhost");
        int port=6666;
        DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, localhost, port);
        //send
        datagramSocket.send(packet);

        datagramSocket.close();

    }
}

public class udpserver {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(6666);

        while (true){
            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);

            datagramSocket.receive(packet);//阻塞接收

            byte[] data = packet.getData();
            String s = new String(data, 0, packet.getLength());
            System.out.println(s);
            if (s.equals("bye"))break;
        }

        datagramSocket.close();
    }
}
udp对话
public class TalkReceive implements Runnable {
    DatagramSocket datagramSocket =null;
    private int port;
    private String msgFrom;

    public TalkReceive(int port,String msgFrom) {
        this.port = port;
        this.msgFrom=msgFrom;
        try {
            datagramSocket= new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        while (true){
            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);

            try {
                datagramSocket.receive(packet);//阻塞接收
            } catch (IOException e) {
                e.printStackTrace();
            }

            byte[] data = packet.getData();
            String s = new String(data, 0, packet.getLength());
            System.out.println(msgFrom+": "+s);
            if (s.equals("bye"))break;
        }

        datagramSocket.close();
    }
}
public class Talksend implements Runnable {
    private DatagramSocket datagramSocket=null;
    private BufferedReader reader=null;
    private int fromPort;
    private int toPort;
    private String toIp;
    private String msgSender;

    public Talksend(int fromPort, String toIp, int  toPort,String  msgSender) throws SocketException {
        this.fromPort=fromPort;
        this.toIp = toIp;
        this.toPort = toPort;
        this.msgSender=msgSender;

        datagramSocket = new DatagramSocket(fromPort);
        reader = new BufferedReader(new InputStreamReader(System.in));
    }

    @Override
    public void run() {
        try {
            while (true){
                String readLine = reader.readLine();
                byte[] bytes = readLine.getBytes();
                DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length,new InetSocketAddress(this.toIp,this.toPort));
                //send
                datagramSocket.send(packet);
                if (readLine.equals("bye")) break;
            }
            datagramSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class TalkTeacher {
    public static void main(String[] args) throws SocketException {
        new Thread(new Talksend(5555,"localhost",8888,"teacher")).start();
        new Thread(new TalkReceive(9999,"teacher")).start();
    }
}
public class TalkStudent {
    public static void main(String[] args) throws SocketException {
        new Thread(new Talksend(7777,"localhost",9999,"student")).start();
        new Thread(new TalkReceive(8888,"student")).start();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306525.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号