码迷,mamicode.com
首页 > 其他好文 > 详细

4月14日网络编程学习

时间:2019-04-15 00:28:16      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:pcl   知识   通道   write   执行   eth   理解   ram   current   

今日学习了网络基础和udp(用户数据报协议)编程和tcp(传输控制协议)编程

相比较之下udp 是面向无连接的 信息不可靠 但是效率高

tcp 是面向连接的 信息可靠 但是效率不够高

tcp 也要比udp 学起来复杂一点,现在我还有点没搞明白,还需要继续的复习。

附代码(以下有一些代码是老师课件中下载的,我只是阅读了代码理解大概意思):

 1 import java.net.*;
 2 public class UdpRecv
 3 {
 4     public static void main(String[] args) throws Exception
 5     {
 6         DatagramSocket    ds=new DatagramSocket(3000);
 7         byte [] buf=new byte[1024];
 8         DatagramPacket dp=new DatagramPacket(buf,1024);
 9         
10         System.out.println("UdpRecv: 我在等待信息");
11         ds.receive(dp);
12         System.out.println("UdpRecv: 我接收到信息");
13         String strRecv=new String(dp.getData(),0,dp.getLength()) +
14         " from " + dp.getAddress().getHostAddress()+":"+dp.getPort(); 
15         System.out.println(strRecv);
16         
17         Thread.sleep(1000);
18         System.out.println("UdpRecv: 我要发送信息");
19         String str="hello world 222";
20         DatagramPacket dp2=new DatagramPacket(str.getBytes(),str.length(), 
21                 InetAddress.getByName("127.0.0.1"),dp.getPort());
22         ds.send(dp2);
23         System.out.println("UdpRecv: 我发送信息结束");
24         ds.close();
25     }
26 }
 1 import java.net.*;
 2 public class UdpSend
 3 {
 4     public static void main(String [] args) throws Exception
 5     {
 6         DatagramSocket ds=new DatagramSocket();
 7         String str="hello world";
 8         DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),
 9                 InetAddress.getByName("127.0.0.1"),3000);
10         
11         System.out.println("UdpSend: 我要发送信息");
12         ds.send(dp);
13         System.out.println("UdpSend: 我发送信息结束");
14         
15         Thread.sleep(1000);
16         byte [] buf=new byte[1024];
17         DatagramPacket dp2=new DatagramPacket(buf,1024);
18         System.out.println("UdpSend: 我在等待信息");
19         ds.receive(dp2);
20         System.out.println("UdpSend: 我接收到信息");
21         String str2=new String(dp2.getData(),0,dp2.getLength()) +
22                 " from " + dp2.getAddress().getHostAddress()+":"+dp2.getPort(); 
23         System.out.println(str2);
24                 
25         ds.close();
26     }
27 }
 1 package com.wsy.tcp;
 2 import java.net.*;
 3 import java.io.*;
 4 
 5 public class TcpClient {
 6     public static void main(String[] args) {
 7         try {
 8             Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 8001); //需要服务端先开启
 9             
10             //同一个通道,服务端的输出流就是客户端的输入流;服务端的输入流就是客户端的输出流
11             InputStream ips = s.getInputStream();    //开启通道的输入流
12             BufferedReader brNet = new BufferedReader(new InputStreamReader(ips));
13             
14             OutputStream ops = s.getOutputStream();  //开启通道的输出流
15             DataOutputStream dos = new DataOutputStream(ops);            
16 
17             BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));
18             while (true) 
19             {
20                 String strWord = brKey.readLine();
21                 if (strWord.equalsIgnoreCase("quit"))
22                 {
23                     break;
24                 }
25                 else
26                 {
27                     System.out.println("I want to send: " + strWord);
28                     dos.writeBytes(strWord + System.getProperty("line.separator"));
29                     
30                     System.out.println("Server said: " + brNet.readLine());
31                 }
32                 
33             }
34             
35             dos.close();
36             brNet.close();
37             brKey.close();
38             s.close();
39         } catch (Exception e) {
40             e.printStackTrace();
41         }
42     }
43 }
 1 package com.wsy.tcp;
 2 
 3 import java.net.*;
 4 import java.io.*;
 5 public class TcpServer
 6 {
 7     public static void main(String [] args) 
 8     {
 9         try
10         {
11             ServerSocket ss=new ServerSocket(8001); //驻守在8001端口
12             Socket s=ss.accept();                   //阻塞,等到有客户端连接上来
13             System.out.println("welcome to the java world");
14             InputStream ips=s.getInputStream();     //有人连上来,打开输入流
15             OutputStream ops=s.getOutputStream();   //打开输出流
16             //同一个通道,服务端的输出流就是客户端的输入流;服务端的输入流就是客户端的输出流
17             
18             ops.write("Hello, Client!".getBytes());  //输出一句话给客户端
19             
20             
21             BufferedReader br = new BufferedReader(new InputStreamReader(ips));
22             //从客户端读取一句话            
23             System.out.println("Client said: " + br.readLine());
24             
25             
26             ips.close();          
27             ops.close();
28             s.close();
29             ss.close();
30         }
31         catch(Exception e)
32         {
33             e.printStackTrace();
34         }
35     }
36 }
 1 package com.wsy.tcp;
 2 import java.net.*;
 3 import java.util.concurrent.Executors;
 4 import java.util.concurrent.ThreadPoolExecutor;
 5 public class TcpServer2
 6 {
 7     public static void main(String [] args)
 8     {
 9         try
10         {
11             ServerSocket ss=new ServerSocket(8001);
12             while(true)
13             {
14                 Socket s=ss.accept();
15                 System.out.println("来了一个client");
16 //                new Thread(new Worker(s)).start();
17                 Worker work = new Worker(s);
18                 ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
19                 //这里遇到了之前的多线程知识,卡壳了一下 现在还有个问题就是 我固定线程5个 但是TcpClient 可以执行超过5个
20                 pool.execute(work);
21             }
22             //ss.close();
23         }
24         catch(Exception e)
25         {
26             e.printStackTrace();
27         }
28     }
29 }
 1 package com.wsy.tcp;
 2 
 3 import java.net.*;
 4 import java.io.*;
 5 
 6 class Worker implements Runnable {
 7     Socket s;
 8 
 9     public Worker(Socket s) {
10         this.s = s;
11     }
12 
13     public void run() {
14         try {
15             System.out.println("服务人员已经启动");
16             InputStream ips = s.getInputStream();
17             OutputStream ops = s.getOutputStream();
18 
19             BufferedReader br = new BufferedReader(new InputStreamReader(ips));
20             DataOutputStream dos = new DataOutputStream(ops);
21             while (true) {
22                 String strWord = br.readLine();
23                 System.out.println("client said:" + strWord +":" + strWord.length());
24                 if (strWord.equalsIgnoreCase("quit"))
25                     break;
26                 String strEcho = strWord + " 666";
27                 // dos.writeBytes(strWord +"---->"+ strEcho +"\r\n");
28                 System.out.println("server said:" + strWord + "---->" + strEcho);
29                 dos.writeBytes(strWord + "---->" + strEcho + System.getProperty("line.separator"));
30             }
31             br.close();
32             // 关闭包装类,会自动关闭包装类中所包装的底层类。所以不用调用ips.close()
33             dos.close();
34             s.close();
35         } catch (Exception e) {
36             e.printStackTrace();
37         }
38     }
39 }

 

4月14日网络编程学习

标签:pcl   知识   通道   write   执行   eth   理解   ram   current   

原文地址:https://www.cnblogs.com/sucker/p/10708114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!