网络类 UDP TCP Socket

来源:互联网 发布:隋文帝杨坚 知乎 编辑:程序博客网 时间:2024/04/30 04:57

UDP协议:数据报 的发送和接收,

 

/* * DatagramSocket 就相当于是一个 应用服务 * DatagramPacket 作为send()参数时,需要有目标主机的IP和端口号 *      作为receive()参数时,这时操作dp对象的ds需要和send()指定的端口 相同  */public class UDP {public static void main(String[] args) throws Exception {DatagramSocket ds = new DatagramSocket(56789,InetAddress.getByName("192.168.1.123"));DatagramPacket dp = new DatagramPacket("www.baidu.com".getBytes(),"www.baidu.com".getBytes().length,InetAddress.getByName("127.0.0.1"),9999);ds.send(dp);//发送到dp的指定ip和端口//System.out.println(dp.getPort());}}class Udpreceive {public static void main(String[] args) throws Exception {DatagramSocket ds = new DatagramSocket(9999,InetAddress.getByName("192.168.1.123"));byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf, buf.length);ds.receive(dp);//接收String info = new String(dp.getData(),0,dp.getLength()) + ":" + dp.getAddress().getHostAddress();System.out.println(info);System.out.println(dp.getSocketAddress());/*getAddress - DatagramPacket/192.168.1.123 * getHostAddress - InetAddress192.168.1.123 *       getSocketAddress - DatagramPacket   /192.168.1.123:56789 */}}
 

TCP协议:有连接的,点对点传输,

 

/* * ServerSocket 服务器端 * Socket 客户端,需要指定连接的服务器的IP和端口 * socket的输入流    相对于   server的输出流 * socket的输出流    相对于   server的输入流 */public class TcpServer implements Runnable {Socket s;TcpServer(Socket s) {this.s = s;}public static void main(String[] args) throws Exception {String port = null;File file = new File("src/demo/net/port.txt");Properties pro = new Properties();pro.load(new FileInputStream(file));String temp = pro.getProperty("value");if (temp != null) {System.out.println("是否启用上次用的端口?(y/n)");Scanner input = new Scanner(System.in);String str= input.next();if (str.equals("y")) {port = temp;} else {System.out.println("请输入 服务器端口号:");port = input.next();System.out.println(port);pro.setProperty("value", port);pro.store(new FileOutputStream(file), "this is port");}} else {System.out.println("请输入 服务器端口号:");Scanner input = new Scanner(System.in);port = input.next();pro.setProperty("value", port);pro.store(new FileOutputStream(file), "this is port");}ServerSocket server = new ServerSocket(Integer.parseInt(port));while (true) {Socket soc = server.accept();new Thread(new TcpServer(soc)).start();}}@Overridepublic void run() {// TODO Auto-generated method stubInputStream is;try {is = s.getInputStream();OutputStream os = s.getOutputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));DataOutputStream dos = new DataOutputStream(os);while (true) {String start = br.readLine();if (start.equalsIgnoreCase("quit")) {break;}String str = new StringBuilder(start).reverse().toString();System.out.println(str);dos.writeBytes(start + "----->" + str+ System.getProperty("line.separator"));}dos.close();br.close();s.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}class TcpCLient {public static void main(String[] args) throws Exception {Socket s = new Socket("127.0.0.1", 18888);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();DataOutputStream dos = new DataOutputStream(os);BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedReader brByServer = new BufferedReader(new InputStreamReader(is));while (true) {String word = br.readLine();dos.writeBytes(word + System.getProperty("line.separator"));if (word.equalsIgnoreCase("quit")) {break;}System.out.println(brByServer.readLine());}dos.close();br.close();brByServer.close();s.close();}}



原创粉丝点击