日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程下的udp和tcp局域网聊天

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程下的udp和tcp局域网聊天 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

多線程,繼承Thread類,重寫run方法。
udp下的聊天。 兩方,每一方都可發,可收。

import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner;class Write extends Thread {DatagramSocket send = null;Scanner sc = null;int port = -1;public Write() {}public Write(DatagramSocket send, int port, Scanner sc) {this.send = send;this.port = port;this.sc = sc;}// 重寫run方法public void run() {try {while (true) {//System.out.print("我:");byte[] b = sc.next().getBytes();DatagramPacket p = new DatagramPacket(b, b.length, InetAddress.getByName("127.0.01"), port);send.send(p);}} catch (Exception e) {e.printStackTrace();} finally {sc.close();send.close();}} }class Read extends Thread {DatagramSocket socket = null;public Read() {}public Read(DatagramSocket receive) {this.socket = receive;}public void run() {while (true) { try {byte[] b = new byte[1024];DatagramPacket p = new DatagramPacket(b, b.length);socket.receive(p);byte[] data = p.getData();System.out.println("來自" + p.getPort() + "端口的消息: " + new String(data, 0, p.getLength()));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }

tcp下的聊天

package com.fun.test;import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.util.Scanner;public class MadeRun {/** @author echo lovely* * @Date 2020/1/7* * */public static void main(String[] args) throws Exception {Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 10011);System.out.println("我是客戶端, pls answer me.");WriteThread wt = new WriteThread(s, new Scanner(System.in)); wt.start();ReadThread rt = new ReadThread(s);rt.start();} } // write class WriteThread extends Thread {Socket s = null;Scanner sc = null;public WriteThread() {}public WriteThread(Socket s, Scanner sc) {this.s = s;this.sc = sc;}public void run() {try { while (true) {OutputStream out = s.getOutputStream();byte[] b = sc.next().getBytes();out.write(b);} } catch (IOException e) {e.printStackTrace();}} }// read class ReadThread extends Thread {Socket s = null;public ReadThread() {}public ReadThread(Socket s) {this.s = s;}public void run() {try { while (true) { InputStream in = s.getInputStream();byte[] b = new byte[1024];int len = in.read(b);if (len != -1) {System.out.println(getName() + new String(b, 0, len));}} } catch (IOException e) {e.printStackTrace();}} } package com.fun.test;import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner;public class Test {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(10011);System.out.println("服務端開啟啦");Socket s = ss.accept();// 10個線程池。int i = 0;for (i = 0; i < 10; i ++) {WriteThread wt = new WriteThread(s, new Scanner(System.in)); wt.start();ReadThread rt = new ReadThread(s);rt.start();} ss.close();} }

tcp下的線程聊天的小bug,文字太長,不能全部收到。
好像多是廢話垃圾代碼。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的线程下的udp和tcp局域网聊天的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。