本文主要是介绍Java高级Day38-网络编程作业,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
112.网络编程作业
//1.使用字符流的方式,编写一个客户端程序和服务器端程序 //2.客户端发送"name",服务器端接收到后,返回"我是nova" //3.客户端发送"hobby",服务器端接收到后,返回"编写java程序" //4.不是这两个问题,回复"你说啥呢" ===============//客户端//=============== public class SocketTCPClient {public static void main(String[] args) throws Exception {//1.连接服务端(ip,端口)//解读:连接本机的 9999端口,如果连接成功,返回Socket对象Socket socket = new Socket(InetAddress.getLocalHost(), 9999);//因为在本机监听,所以用LocalHostSystem.out.println("客户端 socket返回=" + socket.getClass());//2.连接上后,生成Socket,通过socket.getOutputStream()// 得到 和 socket关联的输出流对象OutputStream outputStream = socket.getOutputStream();//3.通过输出流,写入数据 到 数据通道,使用字符流BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); //从键盘读取用户的问题Scanner scanner = new Scanner(System.in);System.out.println("请输入你的问题");String question = scanner.next(); bufferedWriter.write(question);bufferedWriter.newLine();//插入一个换行符,表示写入结束,注意:要求对方使用readLine()bufferedWriter.flush();//如果使用字符流,需要手动刷新,否则数据不会写入数据通道// 设置写入结束标记socket.shutdownOutput(); //4.获取和socket相关联的输入流,读取数据并显示InputStream inputStream = socket.getInputStream();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));String s = bufferedReader.readLine();System.out.println(s); //5.关闭流对象和socket,必须关闭outputStream.close();socket.close();bufferedReader.close();//关闭外层流System.out.println("客户端退出了");} } ===============//服务端//=============== public class SocketTCPServer {public static void main(String[] args) throws Exception {//1.在本机的 9999端口 监听,等待连接// 细节:要求在本机没有其他服务在监听9999// 细节:这个 ServerSocket 可以通过accpet() 返回多个Socket[多并发]ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务端,在9999端口监听,等待连接...");//2.当没有客户端连接9999端口时,程序会 阻塞,等待连接// 如果有客户端连接,则会返回Socket对象,程序继续Socket socket = serverSocket.accept(); //3.通过socket.getInputStream() 读取客户端写入到数据通道的数据,显示InputStream inputStream = socket.getInputStream();//4.IO读取,使用字符流,用转换流(InputStreamReader)将InputStream转换成字符流BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));String s = bufferedReader.readLine();String answer = "";if ("name".equals(s)){answer = "我是nova";}else if ("hobby".equals(s)){answer = "编写java程序";}else {answer = "你说啥呢";} //5.获取socket相关联的输出流OutputStream outputStream = socket.getOutputStream();//使用字符输出流的方式回复信息BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));bufferedWriter.write(answer);bufferedWriter.newLine();//插入换行流,表示回复内容结束bufferedWriter.flush();//需要手动刷新//6.关闭流和socketbufferedWriter.close();socket.close();serverSocket.close();bufferedReader.close();System.out.println("服务器端退出了");} }
//1.编写一个接收端A和一个发送端B,使用UDP协议完成 //2.接收端在8888端口等待接收数据(receive) //3.发送端向接收端发送数据"四大名著是那些" //4.接收端接收到发送端发送的问题后,返回"四大名著是'红楼梦'...",否则返回what? //5.接收端和发送端程序退出 ===============//ReceiverA//=============== public class UDPReceiverA {public static void main(String[] args) throws Exception {//1.创建一个DatagramSocket对象,准备在8888端口接收数据DatagramSocket socket = new DatagramSocket(8888);//2.构造一个 DatagramPacket对象// 一个数据包,最大是64kbyte[] buf = new byte[64 * 1024];DatagramPacket packet = new DatagramPacket(buf, buf.length);//3.调用 接受方法,将通过网络传输的 DatagramPacket对象填充到packet对象System.out.println("接收端 等待接收问题");socket.receive(packet);//4.可以把packet进行拆包,取出数据,并显示int length = packet.getLength();//实际接收到的数据字节长度byte[] data = packet.getData();//接收到数据String s = new String(data, 0, length);System.out.println(s);//判断接收到的信息是什么String answer = "";if("四大名著是那些".equals(s)){answer = "四大名著是'红楼梦'....";}else {answer = "what?";}//==回复信息给B端//将需要发送的数据,封装到DatagramPacket对象data = answer.getBytes();//封装的 DatagramPacket对象 data内容字节数组,data.length,主机(IP),端口packet = new DatagramPacket(data, data.length, InetAddress.getLocalHost(),8888);socket.send(packet);//发送//5.关闭资源socket.close();} } ===============//SenderB//=============== public class UDPSenderB {public static void main(String[] args) throws Exception {//1.创建 DatagreamSocket 对象,准备在8888端口 接收数据DatagramSocket socket = new DatagramSocket(8888);//同一个机器没有两个相同的端口,所以用9998//2.将需要发送的数据,封装到DatagramPacket对象Scanner scanner = new Scanner(System.in);System.out.println("请输入你的问题?")String question = scanner.next();byte[] data = question.getBytes();//封装的 DatagramPacket对象 data内容字节数组,data.length,主机(IP),端口DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getLocalHost(),8888);socket.send(packet);//3.==接收从A端回复的信息// (1)构造一个 DatagramPacket对象// 一个数据包,最大是64kbyte[] buf = new byte[64 * 1024];packet = new DatagramPacket(buf, buf.length);// (2)调用 接受方法,将通过网络传输的 DatagramPacket对象填充到packet对象// 当有数据包发送到9998端口时,就会接收到数据,如果没有数据包发送到,就会阻塞等待socket.receive(packet);// (3)可以把packet进行拆包,取出数据,并显示int length = packet.getLength();//实际接收到的数据字节长度data = packet.getData();//接收到数据String s = new String(data, 0, length);System.out.println(s);//关闭资源socket.close();System.out.println("B端退出");} }
113.TCP文件下载
//1.编写客户端程序和服务端程序 //2.客户端可以输入一个音乐文件名,比如高山流水,服务端收到音乐名后,可以给客户端返回这个音乐文件,如果服务器没有这个文件,返回一个默认的音乐即可 //3.客户端收到文件后,保存到本地e:\\ ===============//SocketTCPServer//=============== public class SocketTCPServer {public static void main(String[] args) throws Exception {//1.监听 9999端口ServerSocket serverSocket = new ServerSocket(9999);//2.等待客户端连接Socket socket = serverSocket.accept();//3.读取客户端发送的要下载的文件名// 这里的while循环,是考虑将来客户端发送的数据较大的情况InputStream inputStream = socket.getInputStream();byte[] b = new byte[1024];int len = 0;String downLoadFileName = "";while ((len = inputStream.read(b)) != -1){downLoadFileName += new String(b, 0, len);}System.out.println("客户端希望下载的文件名=" + downLoadFileName); //在服务器上有两个文件,无名.mp3 和 高山流水.mp3//若下载高山流水,就返回该文件,否则一律返回无名 String resFileName = "";if ("高山流水".equals(downLoadFileName)){resFileName = "src\\高山流水.mp3";}else {resFileName = "src\\无名.mp3";} //4.创建一个输入流,读取文件BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(resFileName)); //5.使用工具类StreamUtils,读取文件到一个字节数组byte[] bytes = StremUtils.streamToByArray(bis);//6.得到Socket关联的输出流BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());//7.写入到数据通道,返回给客户端bos.write(bytes);socket.shutdownOutput();//8.关闭相关资源bis.close();inputStream.close();socket.close();serverSocket.close();System.out.println("服务端退出...");} } ===============//SocketTCPClient//=============== public class SocketTCPClient {public static void main(String[] args) throws Exception {//1.接收用户输入,指定下载文件名Scanner scanner = new Scanner(System.in);System.out.println("请输入下载文件名");String downloadFileName = scanner.next(); //2.客户端连接服务端,准备发射Socket socket = new Socket(InetAddress.getLocalHost(), 9999);//3.获取和Socket关联的输出流OutputStream outputStream = socket.getOutputStream();outputStream.write(downloadFileName.getBytes());// 设置写入结束的标注socket.shutdownOutput(); //4.读取服务端返回的文件(字节数据)BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); byte[] bytes = StreamUtils.streamToByteArray(bis);//5.得到一个输出流,准备将bytes写入到磁盘文件String filePath = "e:\\" + downloadFileName + ".mp3";BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));bos.write(bytes);//6.关闭相关资源bos.close();bis.close();outputStream.close();socket.close();System.out.println("客户端下载完毕,正在推出...");} }
这篇关于Java高级Day38-网络编程作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!