黑马程序员 java UDP DatagramSocket Thread Runnable 模拟qq聊天
来源:互联网 发布:淘宝五钻店铺值多少钱 编辑:程序博客网 时间:2024/05/07 05:36
package itcast.video.network;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class I_02_UDP_Demo {
/**
* UDP协议传输数据
*
* @param args
*/
public static void main(String[] args) throws Exception {
DatagramSocket send = new DatagramSocket();
DatagramSocket receive = new DatagramSocket(10000);
new Thread(new Send(send)).start();
new Thread(new Receive(receive)).start();
}
}
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
BufferedReader bw = null;
try {
bw = new BufferedReader(new InputStreamReader(System.in));
DatagramPacket dp = null;
String line = null;
int len = 0;
byte[] buf = null;
while ((line = bw.readLine()) != null) {
if ("886".equals(line))
break;
buf = line.getBytes();
dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("127.0.0.1"), 10000);
ds.send(dp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null)
ds.close();
}
}
}
}
class Receive implements Runnable {
private DatagramSocket ds;
public Receive(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
DatagramPacket dp = null;
String line = null;
int len = 0;
byte[] buf = new byte[1024];
while (true) {
dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String address = dp.getAddress().getHostAddress();
line = new String(dp.getData(),0,dp.getLength());
System.out.println(address+" : "+line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ds != null)
ds.close();
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class I_02_UDP_Demo {
/**
* UDP协议传输数据
*
* @param args
*/
public static void main(String[] args) throws Exception {
DatagramSocket send = new DatagramSocket();
DatagramSocket receive = new DatagramSocket(10000);
new Thread(new Send(send)).start();
new Thread(new Receive(receive)).start();
}
}
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
BufferedReader bw = null;
try {
bw = new BufferedReader(new InputStreamReader(System.in));
DatagramPacket dp = null;
String line = null;
int len = 0;
byte[] buf = null;
while ((line = bw.readLine()) != null) {
if ("886".equals(line))
break;
buf = line.getBytes();
dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("127.0.0.1"), 10000);
ds.send(dp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null)
ds.close();
}
}
}
}
class Receive implements Runnable {
private DatagramSocket ds;
public Receive(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
DatagramPacket dp = null;
String line = null;
int len = 0;
byte[] buf = new byte[1024];
while (true) {
dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String address = dp.getAddress().getHostAddress();
line = new String(dp.getData(),0,dp.getLength());
System.out.println(address+" : "+line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ds != null)
ds.close();
}
}
}
0 0
- 黑马程序员 java UDP DatagramSocket Thread Runnable 模拟qq聊天
- 黑马程序员 java IO TCP Socket ServerSocket Thread Runnable 模拟qq聊天
- 黑马程序员 【】java学习之路——UDP之模拟聊天
- 黑马程序员_(UDP-聊天)
- 黑马程序员-UDP聊天程序
- 《黑马程序员》socket---udp聊天
- 黑马程序员 java基础---UDP协议实现聊天功能
- Java.UDP.DatagramSocket-DatagramPacket
- Java.UDP.DatagramSocket-DatagramPacket
- 黑马程序员 ---------- Java 网络基础 基于UDP 模仿QQ
- JAVA学习第六十课 — UDP协议 &基于多线程模拟简单的QQ聊天程序
- 黑马程序员JAVA 模拟QQ聊天工具 CA…
- 黑马程序员--Java网络编程(InetAddress、DatagramSocket、DatagramPacket)
- 4.Java Networking: UDP DatagramSocket--java网络:UDP DatagramSocket
- linux 下简单的模拟QQ 聊天过程 UDP 通讯
- 黑马程序员——Thread与Runnable实现卖票程序
- 黑马程序员-关于线程继承Thread和Runnable的对比
- 黑马程序员——多线程之Runnable与Thread
- 杭电 1287 破译密码
- sdut Emergency
- Google_codejam2014_Problem A: Magic Trick
- 黑马程序员—Java面向对象(抽象类、模板方法设计模式、接口、多态、内部类)
- 杭电acm 2070
- 黑马程序员 java UDP DatagramSocket Thread Runnable 模拟qq聊天
- 压力测试工具——Galting
- 递归实现strlen函数
- 中国语言的博大精深
- IBM称5年内电脑可具有5种感觉能力
- 第五(六)周作业 p1
- Vim编辑器常用命令
- 第六周作业
- href="#"与href="javascript:void(0)"的区别