赞
踩
- DatagramSocket socket = new DatagramSocket(6666); //创建socket相当于创建码头
- DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); //创建packet相当于创建集装箱
-
- while(true) {
- socket.receive(packet); //接收货物
- byte[] arr = packet.getData();
- int len = packet.getLength();
- String ip = packet.getAddress().getHostAddress();
- System.out.println(ip + ":" + new String(arr,0,len));
- }
发送端Send
- DatagramSocket socket = new DatagramSocket(); //创建socket相当于创建码头
- Scanner sc = new Scanner(System.in);
-
- while(true) {
- String str = sc.nextLine();
- if("quit".equals(str))
- break;
- DatagramPacket packet = //创建packet相当于创建集装箱
- new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
- socket.send(packet); //发货
- }
- socket.close();
A发送和接收在一个窗口完成
- public class Demo3_MoreThread {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- new Receive().start();
-
- new Send().start();
- }
-
- }
-
- class Receive extends Thread {
- public void run() {
- try {
- DatagramSocket socket = new DatagramSocket(6666); //创建socket相当于创建码头
- DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); //创建packet相当于创建集装箱
-
- while(true) {
- socket.receive(packet); //接收货物
- byte[] arr = packet.getData();
- int len = packet.getLength();
- String ip = packet.getAddress().getHostAddress();
- System.out.println(ip + ":" + new String(arr,0,len));
- }
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
-
- class Send extends Thread {
- public void run() {
- try {
- DatagramSocket socket = new DatagramSocket(); //创建socket相当于创建码头
- Scanner sc = new Scanner(System.in);
-
- while(true) {
- String str = sc.nextLine();
- if("quit".equals(str))
- break;
- DatagramPacket packet = //创建packet相当于创建集装箱
- new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
- socket.send(packet); //发货
- }
- socket.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
客户端
- Socket socket = new Socket("127.0.0.1", 9999); //创建Socket指定ip地址和端口号
- InputStream is = socket.getInputStream(); //获取输入流
- OutputStream os = socket.getOutputStream(); //获取输出流
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- PrintStream ps = new PrintStream(os);
-
- System.out.println(br.readLine());
- ps.println("我想报名就业班");
- System.out.println(br.readLine());
- ps.println("爷不学了");
- socket.close();
服务端
- ServerSocket server = new ServerSocket(9999); //创建服务器
- Socket socket = server.accept(); //接受客户端的请求
- InputStream is = socket.getInputStream(); //获取输入流
- OutputStream os = socket.getOutputStream(); //获取输出流
-
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- PrintStream ps = new PrintStream(os);
-
- ps.println("欢迎咨询传智播客");
- System.out.println(br.readLine());
- ps.println("报满了,请报下一期吧");
- System.out.println(br.readLine());
- server.close();
- socket.close();
- ServerSocket server = new ServerSocket(9999); //创建服务器
- while(true) {
- final Socket socket = server.accept(); //接受客户端的请求
- new Thread() {
- public void run() {
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintStream ps = new PrintStream(socket.getOutputStream());
- ps.println("欢迎咨询传智播客");
- System.out.println(br.readLine());
- ps.println("报满了,请报下一期吧");
- System.out.println(br.readLine());
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。