java基础—网络编程———建立聊天的形式
2015-12-14 09:46
609 查看
接收器和发射器的简单演示
import java.io.*; import java.net.*; public class SocketDemo { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.out.println("发送端启动"); System.out.println(InetAddress.getLocalHost().getHostAddress()); DatagramSocket ds = new DatagramSocket(); String str = "发送端来了了了"; byte[] buf = str.getBytes(); DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.3.4"),10101); ds.send(dp); ds.close(); } }
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class SocketDemo2 { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.out.println("接收端启动"); DatagramSocket ds = new DatagramSocket(10101); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf,buf.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); String text = new String(dp.getData(),0,dp.getLength()); System.out.println(ip+" "+port); System.out.println(text); ds.close(); /* * 因为UDP协议数据传输,仅仅管发送数据。而无论接收是否可以接收到数据,因此应该先启动接收端程序,再启动发送端程序*/ } }
双窗体聊天模式:
建立发送端:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class SocketChatDemoInput { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.out.println("发送端启动"); DatagramSocket ds = new DatagramSocket(); BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = bufr.readLine())!=null) { byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.3.4"),10102); ds.send(dp); if("over".equals(line)) break; } ds.close(); } }
建立接收端
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class SocketChatOutput { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.out.println("接收端启动"); DatagramSocket ds = new DatagramSocket(10102); while(true) { byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf,buf.length); ds.receive(dp); String ip =dp.getAddress().getHostAddress(); int inpor = dp.getPort(); String text = new String(dp.getData(),0,dp.getLength()); System.out.println(ip+" "+ inpor+" \n"+text); } } }
单窗体聊天模式
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; class Send implements Runnable { private DatagramSocket ds ; public Send(DatagramSocket ds) { this.ds= ds; } public void run() { try { System.out.println("发送端启动"); BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = bufr.readLine())!=null) { byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.3.4"),10103); ds.send(dp); if("over".equals(line)) { System.out.println("聊天程序退出"); break; } } ds.close(); }catch(Exception e) { System.out.println(e.toString()); } } } class Rece implements Runnable { DatagramSocket ds; Rece(DatagramSocket ds) { this.ds = ds; } public void run() { while(true) { System.out.println("接收端启动"); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf,buf.length); try { ds.receive(dp); } catch (IOException e) { e.printStackTrace(); } String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); String text = new String(dp.getData(),0,dp.getLength()); System.out.println(ip+" "+ port+" \n"+text); } } } public class SocketGroupChat { public static void main(String[] args) throws Exception { // 单窗体聊天模式 DatagramSocket send = new DatagramSocket(); DatagramSocket rece = new DatagramSocket(10103); Send s = new Send(send); Rece r = new Rece(rece); new Thread(s).start(); new Thread(r).start(); } }
相关文章推荐
- 运行BP神经网络预测误差大
- 网络编程
- 10-《电子入门趣谈》第二章_电子电路的神经网络-2.1.1电阻
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- 用tomcat配置https自签名证书,解决 ios7.1以上系统, 苹果inHouse发布
- HTTP状态查询
- centos 安装 使用本地图像文件来安装网络安装
- HttpURLConnection和HttpClient连接网络
- TCP/IP协议思维导图
- 如何在 Apache 中启用 HTTP/2
- TCP/IP
- http
- android 使用http get 与post 访问网络
- tcp/ip协议中消息传输对帧消息的操作
- HTTP请求模型和头信息
- android http 通信实例(未能正常运行,待后续完善)
- 在tcp/ip协议中以BigEndian方式的编码与解码
- http下载文件
- 自定义的投票协议的通讯(tcp/ip 与 udp示例)
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包