编写一个聊天程序:有接收数据部分,和发数据的部分, 这两部分需要同时执行,使用多线程实现,一个控制接收,一个控制发送
2016-03-23 12:31
961 查看
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* 编写一个聊天程序:有接收数据部分,和发数据的部分,
* 这两部分需要同时执行,那就需要使用多线程
* 一个控制接收,一个控制发送
*
*/
public class ChartDemo {
public static void main(String[] args) {
try {
DatagramSocket sendSocket =new DatagramSocket();
DatagramSocket receSocket =new DatagramSocket(8888);
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(sendSocket)).start();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
//发送方法
class Send implements Runnable{
private DatagramSocket ds;
public Send(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in ));
String line=null;
try {
while ((line=bufr.readLine())!=null) {
if ("886".equals(line)) {
break;
}
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket
(buf, buf.length,InetAddress.getByName("127.0.0.1"),8888);
ds.send(dp);
}
} catch (IOException e) {
throw new RuntimeException("发送端失败");
}
}
}
//接收方法
class Rece implements Runnable{
private DatagramSocket ds;
public Rece (DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
try {
while (true) {
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(), 0, dp.getLength());
System.out.println(ip+"::::"+data);
}
} catch (Exception e) {
throw new RuntimeException("接收端失败");
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* 编写一个聊天程序:有接收数据部分,和发数据的部分,
* 这两部分需要同时执行,那就需要使用多线程
* 一个控制接收,一个控制发送
*
*/
public class ChartDemo {
public static void main(String[] args) {
try {
DatagramSocket sendSocket =new DatagramSocket();
DatagramSocket receSocket =new DatagramSocket(8888);
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(sendSocket)).start();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
//发送方法
class Send implements Runnable{
private DatagramSocket ds;
public Send(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in ));
String line=null;
try {
while ((line=bufr.readLine())!=null) {
if ("886".equals(line)) {
break;
}
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket
(buf, buf.length,InetAddress.getByName("127.0.0.1"),8888);
ds.send(dp);
}
} catch (IOException e) {
throw new RuntimeException("发送端失败");
}
}
}
//接收方法
class Rece implements Runnable{
private DatagramSocket ds;
public Rece (DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
try {
while (true) {
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(), 0, dp.getLength());
System.out.println(ip+"::::"+data);
}
} catch (Exception e) {
throw new RuntimeException("接收端失败");
}
}
}
相关文章推荐
- python混合笔记(二)
- Problem A: 删除区间内的元素(线性表)
- 一个即将毕业彷徨流浪者的内心独白
- 案例:演示TCP传输过程的实现
- TCP通信案例分析
- Css3之高级-3 Css多列属性(分隔列、列间隔、列规则、浏览器兼容性)
- PHP 简介
- CSS学习笔记
- fasta文件拆分与合并
- juery给所有ID属性相同的div绑定一个事件
- 建立一个文本转换服务器
- kafka学习
- 夜间模式 (文字不改变)
- 使用内容提供者和xml备份联系人
- 共享你的控件 -- 用NuGet包装自己的控件
- Problem B-1001
- codeforces->653A. Bear and Three Balls
- ToolBar的封装
- OpenCV学习笔记(二十八)——光流法对运动目标跟踪Video
- Python debug(二)