2021级-JAVA14网络编程

文章提供了两个Java程序,分别展示了TCP和UDP协议的网络通信。TCP服务器和客户端通过输入输出流进行数据交换,当接收到特定结束符时关闭连接。UDP服务器接收数据包,回传消息,同样在接收到end时停止。
摘要由CSDN通过智能技术生成

目录

8-1 sdut-网络编程-1 TCP协议通信

8-2 sdut-网络编程-2 UDP协议通信


8-1 sdut-网络编程-1 TCP协议通信


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8888);
        while(true)
        {
            Socket socket = ss.accept();
            InputStream is = socket.getInputStream();
            byte[] bytes = new byte[10];
            int len = 0;
            StringBuffer buffer = new StringBuffer();
            while((len = is.read(bytes)) != -1)
            {
                buffer.append(new String(bytes, 0,len));
            }
            System.out.println("I have receive" + buffer.toString());

            OutputStream os = socket.getOutputStream();
            os.write(("Message from Server is:" + buffer.toString()).getBytes());
            is.close();
            os.close();
            socket.close();
            if("end".equals(buffer.toString()))
            {
                break;
            }
        }
        ss.close();
    }
}
import javax.swing.plaf.InputMapUIResource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class TCPClient {
    public static void main(String[] args) throws IOException {
        Scanner cin = new Scanner(System.in);
        InetAddress ip = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
        Socket socket = new Socket(ip, 8888);
        OutputStream os = socket.getOutputStream();
        String line = cin.nextLine();
        os.write(line.getBytes());
        socket.shutdownOutput();


        InputStream is = socket.getInputStream();
        int len = 0;
        byte[] bytes = new byte[10];
        StringBuffer buffer = new StringBuffer();
        while((len = is.read(bytes)) != -1)
        {
            buffer.append(new String(bytes, 0, len));
        }
        System.out.println("Client has received:" + buffer.toString());
        is.close();
        os.close();
        socket.close();
    }
}


8-2 sdut-网络编程-2 UDP协议通信

import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UDPServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(9999);
        while(true)
        {
            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
            ds.receive(dp);
            byte[] data = dp.getData();
            String str = new String(data, 0, dp.getLength());
            System.out.println("I have received" + str);

            byte[] dataout = ("Message from Serve is:" + str).getBytes();
            DatagramPacket dp1 = new DatagramPacket(dataout, dataout.length,
                    dp.getAddress(), dp.getPort());
            ds.send(dp1);
            if("end".equals(str))
                break;
        }
        ds.close();
    }
}

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UDPClient {
    public static void main(String[] args) throws IOException {
        Scanner cin = new Scanner(System.in);
        DatagramSocket ds = new DatagramSocket();
        String line = cin.nextLine();
        DatagramPacket dp = new DatagramPacket(line.getBytes(), line.length(),
                InetAddress.getByAddress(new byte[]{127, 0, 0, 1}),
                9999);
        ds.send(dp);

        byte[] bytes = new byte[1024];
        DatagramPacket dp1 = new DatagramPacket(bytes, bytes.length);
        ds.receive(dp1);

        byte[] data = dp1.getData();
        int len = dp1.getLength();
        System.out.println("Client has received:");
        System.out.println(new String(data, 0, len));

        ds.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值