文件下载:https://download.csdn.net/download/OwemShu/83611710
阅读讲义,并将源代码(UdpSend.java和UdpRecv.java)在机器上编译运行通过(注意:要根据自己的机器IP地址修改源代码)。
【程序源代码】
import java.net.*; public class UdpSend { public static void main(String[] args)throws Exception { DatagramSocket ds = new DatagramSocket(); String str = "Hello World"; DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("169.254.189.121"), 9000); ds.send(dp); ds.close(); } }
import java.net.*; public class UdpRecv { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(9000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp); String strRecv = new String(dp.getData(), 0, dp.getLength()) + "from" + dp.getAddress().getHostAddress() + ":" + dp.getPort(); System.out.println(strRecv); ds.close(); } }
【实验结果与分析】
阅读讲义,并将源代码(TcpServer.java)在机器上编译运行,然后通过在命令行中输入“telnet 自己的IP地址 8001”进行测试。
【程序源代码】
import java.net.*; import java.io.*; public class TcpServer { public static void main(String [] args){ try { ServerSocket ss=new ServerSocket(8001); Socket s=ss.accept(); InputStream ips=s.getInputStream(); OutputStream ops=s.getOutputStream(); ops.write("welcome to www.baidu.com!".getBytes()); byte [] buf=new byte[1024]; int len=ips.read(buf); System.out.println(new String(buf,0,len));ips.close(); ops.close(); s.close(); ss.close(); }catch(Exception e){e.printStackTrace();} } }
【实验结果与分析】
【程序源代码】
import java.net.*; import java.io.*; class Servicer implements Runnable { Socket s; public Servicer(Socket s) { this.s = s; } public void run() { try { InputStream ips = s.getInputStream(); OutputStream ops = s.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(ips)); DataOutputStream dos = new DataOutputStream(ops); while (true) { String strWord = br.readLine(); System.out.println(strWord + ":" + strWord.length()); if (strWord.equalsIgnoreCase("quit")) break; String strEcho = (new StringBuffer(strWord).reverse()).toString(); dos.writeBytes(strWord + "————>" + strEcho + System.getProperty("line.separator")); } br.close(); dos.close(); s.close(); } catch (Exception e) { e.printStackTrace(); } } }
import java.net.ServerSocket; import java.net.Socket; class Tcp_Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8001); while (true) { Socket s = ss.accept(); new Thread(new Servicer(s)).start(); } } catch (Exception e) { e.printStackTrace(); } } }
class TcpClient { public static void main(String[] args) { try { if (args.length < 2) { System.out.println("Usage:java TcpClient ServerIP ServerPort"); return; } Socket s = new Socket(InetAddress.getByName(args[0]), Integer.parseInt(args[1])); InputStream ips = s.getInputStream(); OutputStream ops = s.getOutputStream(); BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in)); DataOutputStream dos = new DataOutputStream(ops); BufferedReader brNet = new BufferedReader(new InputStreamReader(ips)); while (true) { String strWord = brKey.readLine(); dos.writeBytes(strWord + System.getProperty("line.separator")); if (strWord.equalsIgnoreCase("quit")) break; else System.out.println(brNet.readLine()); } dos.close(); brNet.close(); brKey.close(); s.close(); } catch (Exception e) { e.printStackTrace(); } } }
【实验结果与分析】
用Wireshark软件截获上面三个程序运行时客户机和服务器之间发送的数据包,并且根据截获的数据包内容进行分析。
【实验结果与分析】
记录实验感受、上机过程中遇到的困难及解决办法、遗留的问题、意见和建议等。
本次通过学习Java Socket进行通信的UDP和TCP客户端与服务器积累了相关经验,拓宽视野,深化了对相关知识的理解;