udp编程:一对一咨询

package com.sundear.demo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
 * 发送端:使用面向对象封装
 */
public class TalkSend implements Runnable{
    private DatagramSocket client;
    private BufferedReader reader;
    private String toIP;
    private int toPort;
    public TalkSend(int port,String toIP,int toPort){
        this.toIP=toIP;
        this.toPort =toPort;
      try{
          client = new DatagramSocket(port);
          reader = new BufferedReader(new InputStreamReader(System.in));
      }catch (SocketException e){
          e.printStackTrace();
      }
    }
    @Override
    public void run() {
        while(true) {
            String data;
            try {
                data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
                client.send(packet);
                if(data.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}

 

package com.sundear.demo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class TalkReceive implements Runnable{
    private DatagramSocket server;
    private String from;
    public TalkReceive(int port,String from){
        this.from= from;
        try {
            server = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        byte[] container=new byte[1024*60];
        while(true) {
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            try {
                server.receive(packet);
                byte[] datas = packet.getData();
                String string = new String(datas, 0, packet.getLength());
                System.out.println(this.from+":"+string);
                if(string.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        server.close();
    }
}

  

package com.sundear.demo.util;

public class TalkStudent {
    public static void main(String[] args) {
        new Thread(new TalkSend(7777,"localhost",9999)).start();//发送

        new Thread(new TalkReceive(8888,"老师")).start();//接收

    }
}

 

package com.sundear.demo.util;

public class TalkTeacher {
    public static void main(String[] args) {
        new Thread(new TalkReceive(9999,"学生")).start();//接收
        new Thread(new TalkSend(5555,"localhost",8888)).start();
    }
}

 老师和学生通过控制台一对一聊天

udp编程:一对一咨询

上一篇:P01_前言-入门-变量


下一篇:006.Delphi插件之QPlugins,多服务演示