第十六周java作业

来源:互联网 发布:福特基金会 知乎 编辑:程序博客网 时间:2024/06/05 06:08


1. 使用UDP协议编写一个网络程序,设置接收端程序的监听端口是8001,发送端发送的数据是“Hello, world”。

UDPReceiveTest .java

import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;public class UDPReceiveTest {    /*     * 1. 使用UDP协议编写一个网络程序,设置接收端程序的监听端口是8001,发送端发送的数据是“Hello, world”。     */    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub    byte[] buf = new byte[1024];    DatagramSocket ds = new DatagramSocket(8001);    DatagramPacket dp = new DatagramPacket(buf, 1024);    System.out.println("等待接收数据");    ds.receive(dp);    String str = new String(dp.getData(), 0, dp.getLength()) + "from "     + dp.getAddress().getHostAddress() + ": " + dp.getPort();    System.out.println(str);    ds.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

UDPSendTest.java

import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UDPSendTest {    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub        DatagramSocket ds = new DatagramSocket(3000);        String str = "Hello,world";        DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),                InetAddress.getByName("localhost"),8001);        System.out.println("发送信息");        ds.send(dp);        ds.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2. 使用TCP协议编写一个网络程序,设置服务器端的监听端口是8002,当与客户端建立连接后,服务器端向客户端发送数据“Hello, world”,客户端收到数据后打印输出。

TCPReceiveTest.java

import java.io.IOException;import java.io.InputStream;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;public class TCPReceiveTest {    /*     * 2. 使用TCP协议编写一个网络程序,设置服务器端的监听端口是8002,当与客户端建立连接后,服务器端向客户端发送数据“Hello, world”,     * 客户端收到数据后打印输出。     */    public static void main(String[] args) throws UnknownHostException, IOException {        // TODO Auto-generated method stub        new TCPClient().connect();    }}class TCPClient{    private static final int PORT = 8002;    public void connect() throws UnknownHostException, IOException{        Socket client = new Socket(InetAddress.getLocalHost(), PORT);        InputStream is = client.getInputStream();        byte[] buf = new byte[1024];        int len = is.read(buf);        System.out.println(new String(buf, 0, len));        client.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

TCPSendTest.java

import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class TCPSendTest {    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub        new TCPServer().listen();    }}class TCPServer{    private static final int PORT = 8002;    public void listen() throws IOException{        ServerSocket serverSocket = new ServerSocket(PORT);        Socket client = serverSocket.accept();        OutputStream os = client.getOutputStream();        System.out.println("开始与客户端交互数据");        String str = "Hello,world";        os.write(str.getBytes());        System.out.println("结束与客户端交互数据");        os.close();        client.close();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

运行截图:(两个作业的合在一起了)
这里写图片描述

这里写图片描述

  这里因为eclipse中只有一个控制台窗口,而发送端与接收端又分属两个文件所以不好查看运行的结果,还是在cmd下用javac、java命令运行的好,这里需要注意,你可能javac命令没问题,但是java命令一直提示找不到主类。
  原因是你的.java文件在项目中创建时存在于某个包下,而java命令下有包和无包是不太一样的,具体自己百度去,最简单的方法是把.java文件中的package语句删掉,然后根据自己学的java命令使用方式就可以正常的运行看到结果了
  另外发送端和接收端要分别在两个把不同的cmd下