当前位置: 首页 > news >正文

荆州市做网站的网站怎样制作 优帮云

荆州市做网站的,网站怎样制作 优帮云,软件班级网站建设主题,购物网站logo文章目录 第16章_网络编程拓展练习TCP编程1、学生与老师交互2、查询单词3、拓展#xff1a;查询单词4、图片上传5、拓展#xff1a;图片上传6、多个客户端上传文件7、群聊 UDP编程8、群发消息 第16章_网络编程拓展练习 TCP编程 1、学生与老师交互 案例#xff1a;客户端模… 文章目录 第16章_网络编程拓展练习TCP编程1、学生与老师交互2、查询单词3、拓展查询单词4、图片上传5、拓展图片上传6、多个客户端上传文件7、群聊 UDP编程8、群发消息 第16章_网络编程拓展练习 TCP编程 1、学生与老师交互 案例客户端模拟学生咨询服务器端模拟咨询老师进行交互。 客户端收到信息 ​ 欢迎咨询尚硅谷 ​ 这个月的所有期班都已经满了只能报下一个月的了 服务器端收到信息 ​ 你好我想报名这个月的JavaEE就业班 ​ 好的赶紧给我占个座 提示 1如果是一个客户端与服务器端交互怎么实现 2如果是多个客户端与服务器交互怎么实现 package com.atguigu.exercise1;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket;public class Exercise1Client {public static void main(String[] args) {Socket socket null; // 创建Socket指定ip地址和端口号PrintStream ps null;try {socket new Socket(127.0.0.1, 8888);// 获取输入流BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));System.out.println(br.readLine());// 获取输出流ps new PrintStream(socket.getOutputStream());ps.println(你好我想报名这个月的JavaEE就业班);System.out.println(br.readLine());ps.println(好的赶紧给我占个座);} catch (IOException e) {e.printStackTrace();} finally {if (ps ! null)ps.close();if (socket ! null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}} } 1服务端接收1个客户端访问 package com.atguigu.exercise1;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.time.LocalDateTime;public class Exercise1Server {public static void main(String[] args) {ServerSocket server null;Socket socket null;try {server new ServerSocket(8888);socket server.accept();// 获取输出流PrintStream ps new PrintStream(socket.getOutputStream());ps.println(欢迎咨询尚硅谷!);// 获取输入流BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));System.out.println(socket.getInetAddress().getHostAddress() 留言 LocalDateTime.now());System.out.println(br.readLine() \n);ps.println(这个月的所有期班都已经满了只能报下一个月的了);System.out.println(socket.getInetAddress().getHostAddress() 留言 LocalDateTime.now());System.out.println(br.readLine());} catch (IOException e) {e.printStackTrace();} finally {try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}} } 2服务端接收多个客户端访问 package com.atguigu.exercise1;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.time.LocalDateTime;public class Exercise1 {public static void main(String[] args) {ServerSocket server null;try {server new ServerSocket(8888);boolean flag true;while (flag) {Socket socket server.accept();new Thread() {public void run() {try {// 获取输出流PrintStream ps new PrintStream(socket.getOutputStream());ps.println(欢迎咨询尚硅谷);// 获取输入流BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));System.out.println(socket.getInetAddress().getHostAddress() 留言 LocalDateTime.now());System.out.println(br.readLine() \n);ps.println(这个月的所有期班都已经满了只能报下一个月的了);System.out.println(socket.getInetAddress().getHostAddress() 留言 LocalDateTime.now());System.out.println(br.readLine());socket.close();} catch (IOException e) {e.printStackTrace();}}}.start();}} catch (IOException e) {e.printStackTrace();} finally {try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}} }2、查询单词 案例模拟客户端输入要查询的中文服务器返回对应的英文单词 效果如下 开发提示 1服务器端有一个mapkey是中文词语value是对应的单词 2服务器接收到客户端的词语后从map中get如果可以找到就返回单词如果找不到就返回“没有找到” package com.atguigu.exercise2;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap;public class Exercise2Server {public static void main(String[] args) {ServerSocket server null;Socket socket null;try {HashMapString, String dictionary new HashMapString, String();dictionary.put(星期一, Monday);dictionary.put(星期二, Tuesday);dictionary.put(星期三, Wednesday);dictionary.put(星期四, Thursday);dictionary.put(星期五, Friday);dictionary.put(星期六, Saturday);dictionary.put(星期七, Sunday);//...server new ServerSocket(8888);socket server.accept();// 获取输入流BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));// 获取输出流PrintStream ps new PrintStream(socket.getOutputStream());//接收客户端的中文String key br.readLine();//查询对应的英文单词并返回结果String words dictionary.get(key);if (words ! null) {ps.println(words);} else {ps.println(o(╥﹏╥)o没有找到对应的单词);}} catch (IOException e) {e.printStackTrace();} finally {try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}} } package com.atguigu.exercise2;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.util.Scanner;public class Exercise2Client {public static void main(String[] args) {Socket socket null;Scanner input null;try {// 创建Socket指定ip地址和端口号socket new Socket(127.0.0.1, 8888);input new Scanner(System.in);System.out.print(请输入要查询的词语);String content input.next();// 获取输出流PrintStream ps new PrintStream(socket.getOutputStream());ps.println(content);// 获取输入流BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));System.out.println(查询结果 br.readLine());} catch (IOException e) {e.printStackTrace();} finally {try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}if (input ! null)input.close();}} }3、拓展查询单词 修改前一个题目改为并发版。 服务器端 package com.atguigu.exercise3;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap;public class Exercise3Server {private static HashMapString, String dictionary new HashMapString, String();static {dictionary.put(星期一, Monday);dictionary.put(星期二, Tuesday);dictionary.put(星期三, Wednesday);dictionary.put(星期四, Thursday);dictionary.put(星期五, Friday);dictionary.put(星期六, Saturday);dictionary.put(星期七, Sunday);}public static void main(String[] args) {ServerSocket server null;try {server new ServerSocket(8888);while (true) {Socket socket server.accept();new QueryThread(socket).start();}} catch (IOException e) {e.printStackTrace();} finally {try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}}private static class QueryThread extends Thread {Socket socket;public QueryThread(Socket socket) {this.socket socket;}Overridepublic void run() {BufferedReader br null;PrintStream ps null;try {// 获取输入流br new BufferedReader(new InputStreamReader(socket.getInputStream()));// 获取输出流ps new PrintStream(socket.getOutputStream());//接收客户端的中文String key br.readLine();//查询对应的英文单词并返回结果String words dictionary.get(key);if (words ! null) {ps.println(words);} else {ps.println(o(╥﹏╥)o没有找到对应的单词);}} catch (IOException e) {e.printStackTrace();;} finally {try {if (br ! null)br.close();} catch (IOException e) {e.printStackTrace();}if (ps ! null)ps.close();try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}}}} } 客户端 package com.atguigu.exercise3;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.util.Scanner;public class Exercise3Client {public static void main(String[] args) {Socket socket null;Scanner input null;PrintStream ps null;BufferedReader br null;try {// 创建Socket指定ip地址和端口号socket new Socket(127.0.0.1, 8888);input new Scanner(System.in);System.out.print(请输入要查询的词语);String content input.next();// 获取输出流ps new PrintStream(socket.getOutputStream());ps.println(content);// 获取输入流br new BufferedReader(new InputStreamReader(socket.getInputStream()));System.out.println(查询结果 br.readLine());} catch (IOException e) {e.printStackTrace();} finally {if (ps ! null)ps.close();try {if (br ! null)br.close();} catch (IOException e) {e.printStackTrace();}try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}if (input ! null)input.close();}} }4、图片上传 案例客户端给服务器端上传照片 要求 1客户端上传的照片需要是jpg格式的并且大小在2M含以内的否则不能上传 2要求上传成功后服务器要返回上传成功如果上传失败服务器返回上传失败 3客户端上传到服务器端的照片存储在项目名下photo的文件夹中并且以“照片原文件名时间戳.jpg”命名 效果如下 package com.atguigu.exercise4;import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket;public class Exercise4Server{public static void main(String[] args) {ServerSocket server null;Socket socket null;DataInputStream dis null;PrintStream ps null;FileOutputStream fos null;try {//开启服务器server new ServerSocket(8888);//接收一个客户端的连接socket server.accept();//获取输入流dis new DataInputStream(socket.getInputStream());//获取输出流ps new PrintStream(socket.getOutputStream());//(1)先读取文件名String filename dis.readUTF();//(2)生成唯一的文件名String destfile photo / filename System.currentTimeMillis() .jpg;//(3)读取文件内容并写入目标文件fos new FileOutputStream(destfile);try {byte[] data new byte[1024];int len;while ((len dis.read(data)) ! -1) {fos.write(data, 0, len);}//返回结果给客户端ps.println(接收成功);} catch (Exception e) {//返回结果给客户端ps.println(接收失败);}} catch (IOException e) {e.printStackTrace();} finally {try {if (fos ! null)fos.close();} catch (IOException e) {e.printStackTrace();}ps.close();try {if (dis ! null)dis.close();} catch (IOException e) {e.printStackTrace();}try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}} }package com.atguigu.exercise4;import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.util.Scanner;public class Exercise4Client {public static void main(String[] args)throws IOException {//连接服务器Socket socket new Socket(127.0.0.1,8888);//选择要上传的文件Scanner input new Scanner(System.in);System.out.println(请选择要上传的文件);//例如D:\尚硅谷_0325班_柴林燕_JavaSE\笔记\第14章 IO流.docxString fileStr input.nextLine();File file new File(fileStr);if(!fileStr.endsWith(.jpg)){System.out.println(照片必须是.jpg格式);input.close();socket.close();return;}if(file.length()1024*1024*2){System.out.println(照片必须在2M(含)以内);input.close();socket.close();return;}DataOutputStream dos null;//从file读取内容给服务器发送FileInputStream fis null;try {//获取输出流dos new DataOutputStream(socket.getOutputStream());//先发送文件名dos.writeUTF(file.getName().substring(0, file.getName().lastIndexOf(.)));//发送文件内容fis new FileInputStream(file);byte[] data new byte[1024];int len;while((len fis.read(data)) !-1){dos.write(data, 0, len);}socket.shutdownOutput();//告诉服务器我发送完毕} catch (Exception e) {System.out.println(上传失败);}finally{fis.close();dos.close();}//接收结果BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));String result br.readLine();System.out.println(result);br.close();socket.close();input.close();}} 5、拓展图片上传 相较于上题修改为并发版。 服务器端 package com.atguigu.exercise5;import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket;public class Exercise5Server {public static void main(String[] args) {ServerSocket server null;try {//开启服务器server new ServerSocket(8888);while (true) {//接收一个客户端的连接Socket socket server.accept();new UploadPhotoThread(socket).start();}} catch (IOException e) {e.printStackTrace();} finally {try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}}private static class UploadPhotoThread extends Thread {Socket socket;public UploadPhotoThread(Socket socket) {this.socket socket;}Overridepublic void run() {try (//获取输入流ObjectInputStream dis new ObjectInputStream(socket.getInputStream());//获取输出流PrintStream ps new PrintStream(socket.getOutputStream());){//(1)先读取文件名String filename dis.readUTF();//(2)生成唯一的文件名String destfile photo / filename System.currentTimeMillis() .jpg;//(3)读取文件内容并写入目标文件FileOutputStream fos null;try {fos new FileOutputStream(destfile);byte[] data new byte[1024];int len;while ((len dis.read(data)) ! -1) {fos.write(data, 0, len);}//返回结果给客户端ps.println(接收成功);} catch (Exception e) {//返回结果给客户端ps.println(接收失败);} finally {fos.close();}} catch (IOException e) {e.printStackTrace();} finally {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}} }客户端 package com.atguigu.exercise5;import java.io.*; import java.net.Socket; import java.util.Scanner;public class Exercise5Client {public static void main(String[] args) throws IOException {//连接服务器Socket socket new Socket(127.0.0.1, 8888);//选择要上传的文件Scanner input new Scanner(System.in);System.out.println(请选择要上传的文件);//例如D:\尚硅谷_0325班_柴林燕_JavaSE\笔记\第14章 IO流.docxString fileStr input.nextLine();File file new File(fileStr);if (!fileStr.endsWith(.jpg)) {System.out.println(照片必须是.jpg格式);input.close();socket.close();return;}if (file.length() 1024 * 1024 * 2) {System.out.println(照片必须在2M(含)以内);input.close();socket.close();return;}//获取输出流ObjectOutputStream dos new ObjectOutputStream(socket.getOutputStream());//先发送文件名dos.writeUTF(file.getName().substring(0, file.getName().lastIndexOf(.)));//从file读取内容给服务器发送FileInputStream fis new FileInputStream(file);//发送文件内容byte[] data new byte[1024];int len;while ((len fis.read(data)) ! -1) {dos.write(data, 0, len);}socket.shutdownOutput();//告诉服务器我发送完毕//接收结果BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));String result br.readLine();System.out.println(result);fis.close();dos.close();br.close();socket.close();input.close();}} 6、多个客户端上传文件 需求每一个客户端启动后都可以给服务器上传一个文件服务器接收到文件后保存到一个upload目录中可以同时接收多个客户端的文件上传。 思考分析 1服务器端要“同时”处理多个客户端的请求那么必须使用多线程每一个客户端的通信需要单独的线程来处理。 2服务器保存上传文件的目录只有一个upload而每个客户端给服务器发送的文件可能重名所以需要保证文件名的唯一。我们可以使用“时间戳”作为文件名而后缀名不变 3客户端需要给服务器上传文件名含后缀名以及文件内容。而文件名是字符串文件内容不一定是纯文本的因此选择ObjectOutputStream 和 ObjectInputStream。 服务器示例代码 package com.atguigu.exercise6;import java.net.ServerSocket; import java.net.Socket;public class Exercise6Server {public static void main(String[] args) {ServerSocket server null;try {//服务器在8888端口号监听数据server new ServerSocket(8888);while (true) {//(2)等待连接//这句代码执行一次意味着一个客户端连接Socket accept server.accept();FileUploadThread ft new FileUploadThread(accept);ft.start();}} catch (IOException e) {e.printStackTrace();} finally {try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}} }package com.atguigu.exercise6;import java.io.*; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date;public class FileUploadThread extends Thread {private Socket socket;private String dir upload/;//可以把它放到配置文件中public FileUploadThread(Socket socket) {this.socket socket;}public void run() {FileOutputStream fos null;ObjectInputStream dis null;PrintStream ps null;try {InputStream is socket.getInputStream();dis new ObjectInputStream(is);OutputStream out socket.getOutputStream();ps new PrintStream(out);//读取文件名含后缀名String filename dis.readUTF();int lastIndexOfDot filename.lastIndexOf(.);//截取后缀名String ext filename.substring(lastIndexOfDot);//生成时间戳SimpleDateFormat sf new SimpleDateFormat(yyyyMMddHHmmssSSS);//拼接新文件名 旧文件名 时间戳 后缀名String newFilename filename.substring(0, lastIndexOfDot) sf.format(new Date()) ext;//拼接文件路径String pathname dir File.separator newFilename;//用新文件路径构建文件输出流fos new FileOutputStream(pathname);//接收文件内容byte[] data new byte[1024];long sum 0;int len;while ((len dis.read(data)) ! -1) {fos.write(data, 0, len);fos.flush();sum len;}System.out.println(sum sum);//返回结果ps.println(filename 已上传完毕);} catch (Exception e) {e.printStackTrace();} finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}try {if (dis ! null)dis.close();} catch (IOException e) {e.printStackTrace();}if (ps ! null)ps.close();try {socket.close();} catch (IOException e) {e.printStackTrace();}}} }客户端示例代码 package com.atguigu.exercise6;import java.io.*; import java.net.Socket; import java.util.Scanner;public class Exercise6Client {public static void main(String[] args) {// (1)连接服务器Socket socket null;FileInputStream fis null;ObjectOutputStream dos null;OutputStream out null;Scanner input null;BufferedReader br null;try {socket new Socket(127.0.0.1, 8888);input new Scanner(System.in);out socket.getOutputStream();// 用它的目的是为了既可以单独传一个字符串又可以写字节内容dos new ObjectOutputStream(out);InputStream is socket.getInputStream();InputStreamReader isr new InputStreamReader(is);// 把字节流转成字符流br new BufferedReader(isr);// (2)从键盘输入文件的路径和名称System.out.print(请选择要上传的文件);String path input.nextLine();File file new File(path);// 先发送文件名含后缀名dos.writeUTF(file.getName());// 单独发一个字符串// 还需要一个IO流从文件读取内容给服务器发过去fis new FileInputStream(file);// 3把文件内容给服务器传过去类似与复制文件byte[] data new byte[1024];int len;long sum 0;while ((len fis.read(data)) ! -1) {dos.write(data, 0, len);sum len;dos.flush();}System.out.println(sum sum);socket.shutdownOutput();//数据发送完毕不再发送但是还要接收所以是半关闭// (4)接收服务器返回的结果String result br.readLine();System.out.println(result);} catch (Exception e) {e.printStackTrace();} finally {// (5)关闭try {if (fis ! null)fis.close();} catch (IOException e) {e.printStackTrace();}try {if (dos ! null)dos.close();} catch (IOException e) {e.printStackTrace();}try {if (out ! null)out.close();} catch (IOException e) {e.printStackTrace();}if (input ! null)input.close();try {if (br ! null)br.close();} catch (IOException e) {e.printStackTrace();}try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}}} }7、群聊 需求客户端与服务器连接成功后就可以看到其他客户端的发送的聊天信息当前客户端也可以发送自己的聊天信息。 思考分析 1服务器 要同时接收多个客户端的连接因此需要多线程 服务器这边充当转发角色即在服务器这边的某个客户端的Socket接收到自己客户端发送的消息后要通过服务器端这边其他客户端的Socket将信息转发出去 2客户端 同时能够接收和发送消息因此也要两个线程一个接收一个发送 服务器端示例代码 package com.atguigu.exercise7;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Iterator;public class Exercise7Server {private static ArrayListSocket online new ArrayListSocket();public static void main(String[] args) {ServerSocket server null;try {//1、开启服务器server new ServerSocket(9999);while (true) {//2、接收客户端的连接Socket socket server.accept();//把这个客户端加入到online中online.add(socket);//每一个客户端独立的线程MessageHandler mh new MessageHandler(socket);mh.start();}} catch (IOException e) {e.printStackTrace();} finally {try {if (server ! null)server.close();} catch (IOException e) {e.printStackTrace();}}}private static class MessageHandler extends Thread {private Socket socket;private String ip;public MessageHandler(Socket socket) {super();this.socket socket;this.ip socket.getInetAddress().getHostAddress();}public void run() {BufferedReader br null;PrintStream ps null;try {//接收当前的客户端发送的消息InputStream in socket.getInputStream();InputStreamReader isr new InputStreamReader(in);br new BufferedReader(isr);//这个客户端的一连接成功线程一启动就可以告诉其他人我上线了sendToOthers(ip 上线了);while (true) {String content br.readLine();if (content null) {break;}//收到一句转发一句sendToOthers(ip 说: content);if (bye.equals(content)) {//给自己发一句byeOutputStream out socket.getOutputStream();ps new PrintStream(out);ps.println(bye);break;}}sendToOthers(ip 下线了);} catch (IOException e) {sendToOthers(ip 掉线了);} finally {try {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}try {if (br ! null)br.close();} catch (IOException e) {e.printStackTrace();}if (ps ! null)ps.close();}}//因为转发的代码也很长独立为一个方法public void sendToOthers(String str) {//遍历所有online的客户端IteratorSocket iterator online.iterator();while (iterator.hasNext()) {Socket on iterator.next();if (!on.equals(socket)) {//只给其他客户端转发try {OutputStream out on.getOutputStream();PrintStream ps new PrintStream(out);//不能用try(){}catch因为这里不能关闭流和其他socketps.println(str);} catch (IOException e) {//说明on这个客户端要么下线了要么掉线了iterator.remove();}}}}} } 客户端示例代码 package com.atguigu.exercise7;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner;public class Exercise7Client {public static void main(String[] args) {Socket socket null;try {// 1、连接服务器socket new Socket(127.0.0.1, 9999);// 2、开启两个线程一个收消息一个发消息SendThread st new SendThread(socket);ReceiveThread rt new ReceiveThread(socket);st.start();rt.start();// 等发送线程停下来再往下走st.join();rt.interrupt();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} finally {//关闭sockettry {if (socket ! null)socket.close();} catch (IOException e) {e.printStackTrace();}}}static class SendThread extends Thread {private Socket socket;public SendThread(Socket socket) {super();this.socket socket;}public void run() {Scanner input null;PrintStream ps null;try {// 键盘输入input new Scanner(System.in);OutputStream out socket.getOutputStream();ps new PrintStream(out);while (true) {// 从键盘输入System.out.print(请输入要发送的消息);String content input.nextLine();// 给服务器发送ps.println(content);// 如果bye就结束发送if (bye.equals(content)) {break;}}} catch (IOException e) {e.printStackTrace();} finally {if (input ! null)input.close();if (ps ! null)ps.close();}}}static class ReceiveThread extends Thread {private Socket socket;public ReceiveThread(Socket socket) {super();this.socket socket;}public void run() {BufferedReader br null;try {InputStream in socket.getInputStream();InputStreamReader isr new InputStreamReader(in);br new BufferedReader(isr);while (true) {String line br.readLine();if (bye.equals(line)) {break;}System.out.println(line);}} catch (SocketException e) {System.out.println(退出);} catch (IOException e) {e.printStackTrace();} finally {if (br ! null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}}}} } UDP编程 8、群发消息 案例模拟给全部同学群发“欢迎来到尚硅谷” 开发提示使用UDP群发 package com.atguigu.exercise8;import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress;public class Exercise8Send {public static void main(String[] args) {DatagramSocket ds null;try {//(1)先建立一个DatagramSocketds new DatagramSocket();//(2)准备发送的数据String str 欢迎来到尚硅谷;byte[] data str.getBytes();for (int i 0; i 255; i) {//(3)把数据包装成一个数据报//DatagramPacket(byte[] buf, int length, InetAddress address, int port)/** 第一个参数要发送的数据的字节数组* 第二个参数数组的长度* 第三个参数接收方的IP地址* 第三个参数接收方的端口号** 好比发快递需要填写接收方的IP和端口号*/InetAddress ip InetAddress.getByName(192.168.11. i);int port 8888;DatagramPacket dp new DatagramPacket(data, data.length, ip, port);//(4)发送数据报// 通过socket发送ds.send(dp);}} catch (IOException e) {e.printStackTrace();} finally {//(5)断开if (ds ! null)ds.close();}} } package com.atguigu.exercise8;import java.net.DatagramPacket; import java.net.DatagramSocket;public class Exercise8Receiver {public static void main(String[] args) {DatagramSocket ds null;try {//1、准备一个socket用来接收消息//接收方先确定端口号监听数据的端口号//好比要收到快递需要先确定自己的地址和手机号然后对方才能给你发ds new DatagramSocket(8888);//2、准备一个数据报来接收数据//DatagramPacket(byte[] buf, int length)byte[] data new byte[1024 * 64];//64KDatagramPacket dp new DatagramPacket(data, data.length);//3、接收数据ds.receive(dp);//4、拆包裹byte[] result dp.getData();int len dp.getLength();//实际接收的数据的长度System.out.println(new String(result, 0, len));} catch (IOException e) {e.printStackTrace();} finally {//5、关闭if (ds ! null)ds.close();}} }
文章转载自:
http://www.morning.prxqd.cn.gov.cn.prxqd.cn
http://www.morning.ftsmg.com.gov.cn.ftsmg.com
http://www.morning.tsynj.cn.gov.cn.tsynj.cn
http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn
http://www.morning.ccffs.cn.gov.cn.ccffs.cn
http://www.morning.xcdph.cn.gov.cn.xcdph.cn
http://www.morning.fengnue.com.gov.cn.fengnue.com
http://www.morning.btrfm.cn.gov.cn.btrfm.cn
http://www.morning.qbgff.cn.gov.cn.qbgff.cn
http://www.morning.weiwt.com.gov.cn.weiwt.com
http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn
http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn
http://www.morning.hytqt.cn.gov.cn.hytqt.cn
http://www.morning.tsrg.cn.gov.cn.tsrg.cn
http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn
http://www.morning.gwyml.cn.gov.cn.gwyml.cn
http://www.morning.mxftp.com.gov.cn.mxftp.com
http://www.morning.kjcll.cn.gov.cn.kjcll.cn
http://www.morning.807yy.cn.gov.cn.807yy.cn
http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn
http://www.morning.tgts.cn.gov.cn.tgts.cn
http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn
http://www.morning.xznrk.cn.gov.cn.xznrk.cn
http://www.morning.xkyst.cn.gov.cn.xkyst.cn
http://www.morning.zrnph.cn.gov.cn.zrnph.cn
http://www.morning.drrt.cn.gov.cn.drrt.cn
http://www.morning.xplng.cn.gov.cn.xplng.cn
http://www.morning.blfll.cn.gov.cn.blfll.cn
http://www.morning.khfk.cn.gov.cn.khfk.cn
http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn
http://www.morning.ykmg.cn.gov.cn.ykmg.cn
http://www.morning.dwncg.cn.gov.cn.dwncg.cn
http://www.morning.mywmb.cn.gov.cn.mywmb.cn
http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn
http://www.morning.psdbf.cn.gov.cn.psdbf.cn
http://www.morning.pltbd.cn.gov.cn.pltbd.cn
http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn
http://www.morning.mjctt.cn.gov.cn.mjctt.cn
http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn
http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn
http://www.morning.qfplp.cn.gov.cn.qfplp.cn
http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn
http://www.morning.nkpml.cn.gov.cn.nkpml.cn
http://www.morning.wqcz.cn.gov.cn.wqcz.cn
http://www.morning.csdgt.cn.gov.cn.csdgt.cn
http://www.morning.sfwd.cn.gov.cn.sfwd.cn
http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn
http://www.morning.gfznl.cn.gov.cn.gfznl.cn
http://www.morning.wbysj.cn.gov.cn.wbysj.cn
http://www.morning.fplqh.cn.gov.cn.fplqh.cn
http://www.morning.stprd.cn.gov.cn.stprd.cn
http://www.morning.bplqh.cn.gov.cn.bplqh.cn
http://www.morning.glbnc.cn.gov.cn.glbnc.cn
http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn
http://www.morning.wklrz.cn.gov.cn.wklrz.cn
http://www.morning.mlycx.cn.gov.cn.mlycx.cn
http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn
http://www.morning.xwbld.cn.gov.cn.xwbld.cn
http://www.morning.ltrms.cn.gov.cn.ltrms.cn
http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn
http://www.morning.rwbh.cn.gov.cn.rwbh.cn
http://www.morning.gkgb.cn.gov.cn.gkgb.cn
http://www.morning.bztzm.cn.gov.cn.bztzm.cn
http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn
http://www.morning.cqrenli.com.gov.cn.cqrenli.com
http://www.morning.zsthg.cn.gov.cn.zsthg.cn
http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn
http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn
http://www.morning.fmqng.cn.gov.cn.fmqng.cn
http://www.morning.ffksr.cn.gov.cn.ffksr.cn
http://www.morning.ljcf.cn.gov.cn.ljcf.cn
http://www.morning.qstkk.cn.gov.cn.qstkk.cn
http://www.morning.wktbz.cn.gov.cn.wktbz.cn
http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn
http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn
http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn
http://www.morning.cbqqz.cn.gov.cn.cbqqz.cn
http://www.morning.lflsq.cn.gov.cn.lflsq.cn
http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com
http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn
http://www.tj-hxxt.cn/news/249111.html

相关文章:

  • 石家庄做网站最好的公司有哪些不要域名能建网站么
  • 手机用什么软件做网站网站的不足之处
  • 企业商务网站 建设方案wordpress没有幻灯片
  • 滁州网站建设设计网站建设 科技公司
  • amp网站建设百度网盘资源搜索
  • 备案的网站名称能重复备案吗把手机网站做成app
  • 女人与马做受网站怎么样把自己的产品网上推广
  • 网站建设基本范例网站模板 兼容
  • 免费网站建设服务山东郓城住房和城乡建设厅网站
  • wordpress国内视频网站吗抽奖网站怎么制作
  • 网站开发app开发免费给我推广
  • 网站域名哪里买厦门建设局人员名单
  • 做网站找模版好吗不属于网络营销的推广手段是什么
  • wap网站生成茂名免费自助建站模板
  • 动漫视频网站模板寻找网站建设 网站外包
  • 四川省建设厅资格注册中心网站青岛做网站公司
  • 简单的个人主页网站制作百度网站域名注册
  • 千度网站建设银行成都市第九支行 网站
  • 可以打开的wap网站龙华网站制作公司
  • asp做的网站怎么运行做评测系统网站首先要干嘛
  • 休闲吧网站建设如果修改wordpress后台登录域名
  • wordpress获取文章中的图片不显示优化网站界面的工具
  • 上海网站搭建公司动漫主题WordPress
  • 国外做论坛网站上海科技网络公司
  • 自建网站和第三方平台国外购物网站排行榜
  • 企业网站制作建设的框架有哪几种长沙网络科技有限公司有哪些
  • 峨眉山移动网站建设搭建网站软件
  • 网站设计外包协议房地产微网站模板
  • 国内搜索引擎网站文化传媒网站建设
  • 龙岩做网站哪家好校园推广的方式有哪些