java基础案例9-1模拟微信

package com.itheima;

import java.util.Scanner;
import java.io.*;
import java.net.*;

//接收
class Receive extends Thread{
    public void run(){
        try{
            byte[] buf = new byte[1024];
            DatagramSocket  ds = new DatagramSocket(6666);//对应着对面的端口
            DatagramPacket dp = new DatagramPacket(buf,buf.length);
            while(true) {
                ds.receive(dp);
                String st = new String(dp.getAddress().getHostAddress());
                String str = new String(dp.getData(), 0, dp.getLength());
                System.out.println(st + ": " + str);
                if ("再见".equals(str))
                    break;
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

//发送
class Send extends Thread{
    public void run(){
       try {
           DatagramSocket socket = new DatagramSocket();
           Scanner sc = new Scanner(System.in);
           while (true) {
               String str = sc.nextLine();
               //对应着对面的端口
               DatagramPacket packet = new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("localhost"), 7777);
               socket.send(packet);
               if ("再见".equals(str))
                   break;
           }
       }catch(IOException e)
       {
           e.printStackTrace();
       }
    }
}

public class Main{
    public static void main(String[] args)throws Exception{
        System.out.println("开始聊天");
        new Receive().start();
        new Send().start();
    }
}



package com.itheima;

import java.util.Scanner;
import java.io.*;
import java.net.*;

//接收
class Receive extends Thread{
    public void run(){
        try{
            byte[] buf = new byte[1024];
            DatagramSocket  ds = new DatagramSocket(7777);//(码头)对应着对面的端口
            DatagramPacket dp = new DatagramPacket(buf,buf.length);//集装箱
            while(true) {
                ds.receive(dp);
                String st = new String(dp.getAddress().getHostAddress());
                String str = new String(dp.getData(), 0, dp.getLength());
                System.out.println(st + ": " + str);
                if ("再见".equals(str))
                    break;
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

//发送
class Send extends Thread{
    public void run(){
        try {
            DatagramSocket socket = new DatagramSocket();
            Scanner sc = new Scanner(System.in);
            while (true) {
                String str = sc.nextLine();
                //对应着对面的端口
                DatagramPacket packet = new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("localhost"), 6666);
                socket.send(packet);
                if ("再见".equals(str))
                    break;
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

public class Main{
    public static void main(String[] args)throws Exception{
        System.out.println("开始聊天");
        new Receive().start();
        new Send().start();
    }
}

你可能感兴趣的:(java,java)