第十六周Java作业

来源:互联网 发布:linux修改bashprofile 编辑:程序博客网 时间:2024/06/04 23:36

1.题目

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

import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
2.代码
public class UDPreceive {
        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();
        }
    }
import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
public class UDPsend {
   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();
        }

    }

3.截图

1.题目

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

2.代码

import java.io.IOException;
    import java.io.InputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;

public class TCPreceive {
        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();
        }
    }
import java.io.IOException;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
public class TCPsend {
        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();
        }
    }

原创粉丝点击