赞
踩
两端无需建立连接,不可靠连接,效率高,数据可能会丢失,数据大小有限制,一次发送64kb的数据包
以下是模拟客户端发消息,服务器端收消息
(其实服务器端是负责转发消息的,用户不与服务器通信)
[之后再写聊天室项目]
客户端向服务器端发一条消息
UDPCilent客户端
package com.westos.csdn; import java.io.IOException; import java.net.*; import java.util.Scanner; public class UDPCilent { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(); Scanner scanner = new Scanner(System.in); System.out.println("输入你要发送的字符串"); String data = scanner.nextLine(); byte[] str = data.getBytes(); InetAddress inetAddress = InetAddress.getByName("192.168.6.116"); DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8888); ds.send(dp); } }
UDPServer服务器端
package com.westos.csdn; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPServer { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(8888); byte[] bytes = new byte[1024]; DatagramPacket dp = new DatagramPacket(bytes,bytes.length); ds.receive(dp); byte[] data = dp.getData();; String string = new String(data, 0,dp.getLength()); System.out.println(string); } }
客户端向服务器端循环发消息
UDPCilent客户端代码
package com.jingfei.csdn1; import java.io.IOException; import java.net.*; import java.util.Scanner; public class UDPCilent { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(); Scanner scanner = new Scanner(System.in); System.out.println("输入你要发送的字符串"); while (true){ String data = scanner.nextLine(); if (":EXIT".equals(data))break; byte[] str = data.getBytes(); InetAddress inetAddress = InetAddress.getByName("192.168.6.116"); DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8888); ds.send(dp); } ds.close(); } }
UDPServer服务器端
package com.jingfei.csdn1; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPServer { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(8888); while (true){ byte[] bytes = new byte[1024]; DatagramPacket dp = new DatagramPacket(bytes,bytes.length); ds.receive(dp); byte[] data = dp.getData(); String string = new String(data, 0,dp.getLength()); //if (":EXIT".equals(string))break; System.out.println(string); } } }
客户端与服务器端通信相互发消息
用到线程,一边既开启服务器端接收消息,又当作客户端发送消息,端口和ip绑定相互对应,这边客户端对应那边服务器端,这边服务器端对应那边客户端,(我用的同一台电脑所以ip相同,你可以换成对方的ip)
将接受消息的代码放在子线程中,将输入消息的代码依旧在主线程中(好像输入消息也可以放在另一个子线程中,但有时好像不行)
UDPServer 服务器端
package com.jingfei.csdn2; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class UDPServer { public static void main(String[] args) throws IOException { new Thread() { @Override public void run() { try { DatagramSocket ds = new DatagramSocket(8887); while (true) { byte[] bytes = new byte[1024]; DatagramPacket dp = new DatagramPacket(bytes, bytes.length); ds.receive(dp); byte[] data = dp.getData(); String string = new String(data, 0, dp.getLength()); //if (":EXIT".equals(string))break; String ip = dp.getAddress().getHostAddress(); System.out.println(ip + "发来的信息为:" + string); } } catch (IOException e) { e.printStackTrace(); } } }.start(); DatagramSocket ds = new DatagramSocket(); Scanner scanner = new Scanner(System.in); System.out.println("输入你要发送的字符串"); while (true) { String data = scanner.nextLine(); if (":EXIT".equals(data)) break; byte[] str = data.getBytes(); InetAddress inetAddress = InetAddress.getByName("192.168.6.116"); DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8888); ds.send(dp); } ds.close(); } }
UDPCilent客户端
package com.jingfei.csdn2; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class UDPCilent { public static void main(String[] args) throws IOException { new Thread(){ @Override public void run() { try { DatagramSocket ds = new DatagramSocket(8888); while (true){ byte[] bytes = new byte[1024]; DatagramPacket dp = new DatagramPacket(bytes,bytes.length); ds.receive(dp); byte[] data = dp.getData(); String string = new String(data, 0,dp.getLength()); //if (":EXIT".equals(string))break; String ip = dp.getAddress().getHostAddress(); System.out.println(ip+"发来的信息为:"+string); } } catch (IOException e) { e.printStackTrace(); } } }.start(); DatagramSocket ds = new DatagramSocket(); Scanner scanner = new Scanner(System.in); System.out.println("输入你要发送的字符串"); while (true){ String data = scanner.nextLine(); if (":EXIT".equals(data))break; byte[] str = data.getBytes(); InetAddress inetAddress = InetAddress.getByName("192.168.6.116"); DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8887); ds.send(dp); } ds.close(); } }
两端需要建立连接,可靠连接,相对效率低一点,数据大小无限制.
TCP连接循环向服务器发送数据
TCPCilent客户端
package com.jingfei.csdn; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; public class TCPCilent { public static void main(String[] args) throws IOException { Socket socket = new Socket("192.168.43.209", 8787); OutputStream out = socket.getOutputStream(); while (true) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); if (":EXIT".equals(s)) { out.write(s.getBytes()); break; } out.write(s.getBytes()); } socket.close(); } }
TCPSever服务器端
package com.jingfei.csdn; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TCPSever { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(8787); Socket s = ss.accept(); InputStream in = s.getInputStream(); while (true) { byte[] bytes = new byte[1024]; int len = in.read(bytes); String string = new String(bytes, 0, len); if (":EXIT".equals(string)) { break; } System.out.println(string); } ss.close(); } }
TCP连接客户端与服务器通信互发信息
用到线程,一边既开启服务器端接收消息,又当作客户端发送消息,端口和ip绑定相同,TCP连接既可以获取输入流,也可以获取输出流,将接受消息的代码放在子线程中,将输入消息的代码依旧在主线程中(好像输入消息也可以放在另一个子线程中,但有时好像不行)
TCPCilent客户端
package com.jingfei.csdn3; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; public class TCPCilent { public static void main(String[] args) throws IOException { Socket socket = new Socket("192.168.43.209", 8787); OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); new Thread(){ @Override public void run() { try { while (true) { byte[] bytes = new byte[1024]; int len = in.read(bytes); String string = new String(bytes, 0, len); if (":EXIT".equals(string)) { break; } String ip = socket.getInetAddress().getHostAddress(); System.out.println(ip+"发来的消息为:"+string); } } catch (IOException e) { e.printStackTrace(); } } }.start(); while (true) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); if (":EXIT".equals(s)) { out.write(s.getBytes()); break; } out.write(s.getBytes()); } socket.shutdownOutput(); socket.close(); } }
TCPSever服务器端
package com.jingfei.csdn3; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class TCPSever { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(8787); Socket s = ss.accept(); InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); new Thread(){ @Override public void run() { try { while (true) { byte[] bytes = new byte[1024]; int len = in.read(bytes); String string = new String(bytes, 0, len); if (":EXIT".equals(string)) { break; } String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+"发来的消息为:"+string); } s.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); while (true) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); if (":EXIT".equals(str)) { out.write(str.getBytes()); break; } out.write(str.getBytes()); } s.shutdownOutput(); s.close(); } }
谢谢!
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。