毕向东java基础2

毕向东java基础2Day 23 08 Udp 接收端 09 Udp 键盘录入数据方式 10 Udp 聊天 11 TCP 传输 12 TCP 传输 2 13 TCP 练习 14 TCP 复制文件 毕向东 java 基础 208 Udp 接收端 需求 定义一个应用程序 用于接收 udp 协议传输的数据并处理 思路 1 定义 UdpSocket 服务 2 定义一个数据报包 因为要存储接收到的字节数据

大家好,我是讯享网,很高兴认识大家。



Day 23

08 Udp接收端
09 Udp键盘录入数据方式
10 Udp聊天
11 TCP传输
12 TCP传输2
13 TCP练习
14 TCP复制文件

毕向东java基础208 Udp接收端

需求:
定义一个应用程序,用于接收udp协议传输的数据并处理。

思路:
1.定义UdpSocket服务。
2.定义一个数据报包,因为要存储接收到的字节数据,
而数据报包对象中有更多的功能可以提取字节数据中不同的数据信息。
3.通过socket服务的receive方法将接收到的数据存入已经定义好的数据报包中。
4.通过数据报包对象的特有功能。将这些不同的数据取出,打印在控制台上。
5.关闭资源。

 1 import java.net.*;  2 public class UdpReceive  3 {  4 public static void main(String[] args)  5  {  6 try  7  {  8 //1.创建Udp Socket,建立端点  9 DatagramSocket ds=new DatagramSocket(10000); 10 11 //2.定义数据报包,用来存储数据 12 byte[] buf=new byte[1024]; 13 DatagramPacket dp=new DatagramPacket(buf,buf.length); 14 15 //3.通过Socket服务的receive方法接收到的数据存入数据包 16  ds.receive(dp); 17 18 //通过数据报包中的方法获取其中的数据 19 String ip=dp.getAddress().getHostAddress(); 20 String data=new String(dp.getData(),0,dp.getLength()); 21 int port=dp.getPort(); 22 23 System.out.println(ip+"::"+data+"::"+port); 24 //关闭资源 25  ds.close(); 26 27  } 28 catch (Exception e) 29  { 30  e.printStackTrace(); 31  } 32 33  } 34 }
讯享网
View Code

09 Udp键盘录入数据方式

read和receive都是阻塞式方法,如果接收不到数据,就会等待。

java.net包和java.io包通常一起使用。

测试程序如下:

讯享网 1 public class UdpSend2  2 {  3 public static void main(String[] args) throws IOException  4  {  5 DatagramSocket ds=new DatagramSocket(10001);  6 BufferedReader bufr=  7 new BufferedReader(new InputStreamReader(System.in));  8 String line=null;  9 while((line=bufr.readLine())!=null) 10  { 11 byte[] buf=line.getBytes(); 12 13 DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),10000); 14  ds.send(dp); 15  } 16  ds.close(); 17  } 18 }
View Code
 1 import java.net.*;  2 import java.io.*;  3 public class UdpReceived2  4 {  5 public static void main(String[] args) throws Exception  6  {  7 DatagramSocket ds=new DatagramSocket(10000);  8 while(true)  9  { 10 byte[] buf=new byte[1024]; 11 DatagramPacket dp=new DatagramPacket(buf,buf.length); 12  ds.receive(dp); 13 String ip=dp.getAddress().getHostAddress(); 14 String data=new String(buf,0,dp.getLength()); 15 System.out.println(ip+"::"+data); 16  } 17 18  } 19 }
View Code

10 Udp聊天

实现简单的聊天功能,以ip地址辨别不同身份。

讯享网 1 /*  2 聊天  3 */  4 import java.net.*;  5 import java.io.*;  6 class Send implements Runnable  7 {  8 private DatagramSocket s;  9 public Send(DatagramSocket s) 10  { 11 this.s=s; 12  } 13 public void run() 14  { 15 try 16  { 17 BufferedReader br= 18 new BufferedReader(new InputStreamReader(System.in)); 19 String line=null; 20 while((line=br.readLine())!=null) 21  { 22 if("over".equals(line)) 23 break; 24 byte[] buf=line.getBytes(); 25 DatagramPacket dp= 26 new DatagramPacket(buf,buf.length,InetAddress.getByName("172.29.115.1"),10005); 27  s.send(dp); 28 29  } 30  } 31 catch (Exception e) 32  { 33 throw new RuntimeException("发送端失败!"); 34 35  } 36  } 37 } 38 class Receive implements Runnable 39 { 40 private DatagramSocket s; 41 public Receive(DatagramSocket s) 42  { 43 this.s=s; 44  } 45 public void run() 46  { 47 try 48  { 49 while(true) 50  { 51 byte[] buf=new byte[1024]; 52 DatagramPacket dp=new DatagramPacket(buf,buf.length); 53  s.receive(dp); 54 String ip=dp.getAddress().getHostAddress(); 55 String data=new String(dp.getData(),0,dp.getLength()); 56 System.out.println(ip+"::"+data); 57 58  } 59  } 60 catch (Exception e) 61  { 62 throw new RuntimeException("接受端失败!"); 63 64  } 65  } 66 } 67 public class ChatDemo 68 { 69 public static void main(String[] args)throws Exception 70  { 71 DatagramSocket send=new DatagramSocket(); 72 DatagramSocket rece=new DatagramSocket(10005); 73 74 new Thread(new Send(send)).start(); 75 new Thread(new Receive(rece)).start(); 76  } 77 78 }
View Code

11 TCP传输

TCP传输
Socket和ServerSocket
建立客户端和服务器端
建立连接后,通过Socket中的IO流进行数据传输
关闭Socket
同样,服务器端和客户端是两个独立的应用程序。

 1 /*  2 需求:给服务器端发送一个文本数据  3 步骤:  4 1.创建Socket服务,并指定要连接的主机和端口  5 */  6 import java.net.*;  7 import java.io.*;  8 class TcpClient  9 { 10 public static void main(String[] args) 11  { 12 try 13  { 14 //创建客户端的Socket服务,指定目的主机和端口 15 Socket s=new Socket("127.0.0.1",10000); 16 17 //为了发送数据,应该获取Socket中的输出流。 18 OutputStream out=s.getOutputStream(); 19 20 out.write("hahaha".getBytes()); 21  s.close(); 22 23  } 24 catch (Exception e) 25  { 26 throw new RuntimeException("客户端建立失败!"); 27  } 28 29 30  } 31 } 32 /* 33 需求:定义端点接收数据并打印在控制台。 34 服务端: 35 1.建立服务端的socket服务,ServerSocket(); 36 并监听一个端口 37 2.获取连接过来的客户端对象。 38 通过ServerSocket的accept方法。 39 没有连接,就一直等待,所以这是一个阻塞式方法。 40 3.客户端如果发送来数据,那么服务端要使用对应的客户端对象,并获取到该客户端 41 对象的读取流来读取发过来的数据。 42 4.关闭服务端。 43 */ 44 class TcpServer 45 { 46 public static void main(String[] args) 47  { 48 try 49  { 50 ServerSocket ss=new ServerSocket(10000); 51 Socket s=ss.accept(); 52 String ip=s.getInetAddress().getHostAddress(); 53 54 InputStream in=s.getInputStream(); 55 byte[] buf=new byte[1024]; 56 int len=in.read(buf); 57 System.out.print(ip+":"); 58 System.out.println(new String(buf,0,len)); 59 60  s.close(); 61 62  } 63 catch (Exception e) 64  { 65 throw new RuntimeException("服务端建立失败!"); 66  } 67 68 69  } 70 }
View Code


12 TCP传输2

演示tcp的传输的客户端和服务端的互访
需求:客户端给服务端发送数据,服务端收到后,给客户端反馈信息。

客户端:
1.建立socket服务。指定要连接主机和端口。
2.获取socket流中的输出流。将数据写到该流中,通过网络发送给服务端。
3.获取socket流中的输入流,将服务器反馈的数据获取到,并打印。
4.关闭客户端。

 1 import java.net.*;  2 import java.io.*;  3 class TcpClient2  4 {  5 public static void main(String[] args)  6  {  7 try  8  {  9 Socket s=new Socket("172.29.115.1",10000); 10 OutputStream os=s.getOutputStream(); 11 os.write("你好啊,服务端!".getBytes()); 12 13 byte[] buf=new byte[1024]; 14 InputStream is=s.getInputStream(); 15 int len=is.read(buf); 16 System.out.println(new String(buf,0,len)); 17  s.close(); 18  } 19 catch (Exception e) 20  { 21 throw new RuntimeException("客户端建立失败!"); 22  } 23 24  } 25 } 26 class TcpServer2 27 { 28 public static void main(String[] args) 29  { 30 try 31  { 32 ServerSocket ss=new ServerSocket(10000); 33 Socket s=ss.accept(); 34 35 InputStream is=s.getInputStream(); 36 byte[] buf=new byte[1024]; 37 int len=is.read(buf); 38 System.out.println(s.getInetAddress().getHostAddress()+"::"+new String(buf,0,len)); 39 40 OutputStream os=s.getOutputStream(); 41 os.write("你也好,欢迎光临!".getBytes()); 42 43  s.close(); 44  ss.close(); 45 46 47  } 48 catch (Exception e) 49  { 50 throw new RuntimeException("服务端建立失败!"); 51  } 52  } 53 }
View Code


13 TCP练习

需求:建立一个文本形式转换服务器。
客户端给服务端发送文本,服务端会将文本转换成大写再返回给服务端。
而且,客户端可以不断地进行文本发送,当客户端输入over时,转换结束。

分析:
客户端:
既然是操作设备上的数据,那么就可以使用io技术,并且按照io的操作规律来思考。
源:键盘录入
目的:网络设备,网络输出流
而且操作的是文本数据,可以选择字符流。

步骤:
1.建立服务
2.获取键盘录入
3.将数据发送给服务端
4.接受服务端返回的大写数据
5.结束,关闭资源。

 1 import java.net.*;  2 import java.io.*;  3 class TransClient  4 {  5 public static void main(String[] args) throws Exception  6  {  7 //建立socket服务  8 Socket s=new Socket("172.29.115.1",10000);  9 //获取键盘录入 10 BufferedReader bufr= 11 new BufferedReader(new InputStreamReader(System.in)); 12 //向服务端发送数据 13 BufferedWriter bufOut= 14 new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 15 String line=null; 16 //接受服务端返回的数据 17 BufferedReader bufIn= 18 new BufferedReader(new InputStreamReader(s.getInputStream())); 19 while((line=bufr.readLine())!=null) 20  { 21 if("over".equals(line)) 22 break; 23  bufOut.write(line); 24  bufOut.newLine(); 25  bufOut.flush(); 26 27 String str=bufIn.readLine(); 28 System.out.println("server:"+str); 29 30  } 31  bufr.close(); 32  s.close(); 33 34 35 36 37  } 38 } 39 class TransServer 40 { 41 public static void main(String[] args) throws Exception 42  { 43 //建立服务 44 ServerSocket ss=new ServerSocket(10000); 45 Socket s=ss.accept(); 46 47 //接受客户端的数据 48 BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream())); 49 50 //返回给客户端大写的文本 51 BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 52 53 String line=null; 54 55 while((line=bufIn.readLine())!=null) 56  { 57  System.out.println(line); 58  bufOut.write(line.toUpperCase()); 59  bufOut.newLine(); 60  bufOut.flush(); 61  } 62 63  ss.close(); 64 65 66  } 67 }
View Code


14 TCP复制文件

从客户端向服务端上传文件。

 1 import java.net.*;  2 import java.io.*;  3 class TextClient  4 {  5 public static void main(String[] args) throws Exception  6  {  7 Socket s=new Socket("127.0.0.1",10006);  8 BufferedReader bufr=  9 new BufferedReader(new FileReader("ChatDemo.java")); 10 PrintWriter out=new PrintWriter(s.getOutputStream(),true); 11 String line=null; 12 while((line=bufr.readLine())!=null) 13  { 14  out.println(line); 15  } 16  s.shutdownOutput(); 17 BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream())); 18 String str=bufIn.readLine(); 19 System.out.println("server:"+str); 20  bufr.close(); 21  s.close(); 22 23 24  } 25 } 26 class TextServer 27 { 28 public static void main(String[] args) throws Exception 29  { 30 ServerSocket ss=new ServerSocket(10006); 31 Socket s=ss.accept(); 32 System.out.println(s.getInetAddress().getHostAddress()+"....connected"); 33 BufferedReader bufIn= 34 new BufferedReader(new InputStreamReader(s.getInputStream())); 35 PrintWriter out=new PrintWriter(new FileWriter("server.txt"),true); 36 String line=null; 37 while((line=bufIn.readLine())!=null) 38  { 39  out.println(line); 40 41  } 42 PrintWriter pw=new PrintWriter(s.getOutputStream(),true); 43 pw.println("上传成功!"); 44 45  s.close(); 46  ss.close(); 47  out.close(); 48 49  } 50 }
View Code
小讯
上一篇 2025-01-01 19:49
下一篇 2024-12-25 15:23

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/2175.html