一、IP地址
1、唯一标识网络上的每一台计算机 ,目前大部分都是虚拟的,因为255255255*255个不够分;
2、32的,由4个8位二进制组成———>十进制显示
3、每个网络号对应得IP数范围是0~255;超过255就是不存在,最大的IP地址是255.255.255.255
4、例如,210.10.8.1的网络ID是:210.10.8;因为是C类,所以网络ID取三个数
5,查找本机IP地址,—>打开CMD—>ipconfig
6、测网速—>ping—>目标地址
7、基于TCP协议的Socket编程
建立客户端和服务器端,实现通信
客户端
package cn.bdqn.web.TCP;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;public class LoginClient {/** 客户端*/public static void main(String[] args) {Socket socket=null;OutputStream os=null;InputStream is=null;InputStreamReader isr=null;BufferedReader br=null;try {//建立与服务器连接--跨电脑接收,比如,我负责客户端,那么IP地址就要改成负责服务器端电脑的IPsocket=new Socket("localhost",8002);//127可以换成localhostos=socket.getOutputStream();String s="用户名:wangpeng,密码是:123456";os.write(s.getBytes());socket.shutdownOutput();//接收服务器过来的信息is=socket.getInputStream();isr=new InputStreamReader(is);br=new BufferedReader(isr);String s1=null;while(true){s1=br.readLine();if(s1!=null){break;}}System.out.println("客户端接收"+s1);socket.shutdownInput();} catch (IOException e) {e.printStackTrace();}finally{try {if(os!=null){os.close();}if(br!=null){br.close();}if(isr!=null){isr.close();}if(is!=null){is.close();}if(socket!=null){socket.close();}} catch (IOException e) {e.printStackTrace();}}}}
服务器端
package cn.bdqn.web.TCP;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class LoginServer {/** 服务器端接收消息*/public static void main(String[] args) {ServerSocket socket=null;Socket so=null;InputStream is=null;InputStreamReader isr=null;BufferedReader br=null;OutputStream os=null;try {socket=new ServerSocket(8002);//阻断方法,等待客户端发送请求so=socket.accept();//获得输入流is=so.getInputStream();isr=new InputStreamReader(is);//读取输入流br=new BufferedReader(isr);String s=null;while(true){s=br.readLine();if(s!=null){break;}}System.out.println("服务器接收:"+s);so.shutdownInput();//回复一个信息os=so.getOutputStream();os.write("欢迎你,来到socket的世界".getBytes());so.shutdownOutput();} catch (IOException e) {e.printStackTrace();}finally{try {if(br!=null){br.close();}if(isr!=null){isr.close();}if(is!=null){is.close();}if(so!=null){so.close();}if(os!=null){os.close();}if(socket!=null){socket.close();}} catch (Exception e2) {e2.printStackTrace();}}}}
8、优化,利用面向对象以及序列化和反序列化实现
User
package cn.bdqn.web.TCP2;import java.io.Serializable;public class User implements Serializable {private String name;private String pwd;public User(){}public User(String name, String pwd) {this.name = name;this.pwd = pwd;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}}
线程
package cn.bdqn.web.TCP2;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.OutputStream;import java.net.Socket;public class LoginThread extends Thread {Socket socket = null;//构造LoginThread 将socket对象传递进线程public LoginThread(Socket socket){this.socket = socket;}@Overridepublic void run() {InputStream is = null;InputStreamReader isr = null;BufferedReader br = null;OutputStream os = null;ObjectInputStream ois = null;try {String reply = "";is = socket.getInputStream();ois = new ObjectInputStream(is);User user = (User)ois.readObject();if (user != null) {System.out.println(user.getName());System.out.println(user.getPwd());if ("TOM".equals(user.getName()) && "123456".equals(user.getPwd())) {reply = "登录成功,欢迎您";} else {reply = "登录失败,用户名或密码错误";}}// isr = new InputStreamReader(is);// br = new BufferedReader(isr);// String info = null;// while( (info = br.readLine()) != null){// System.out.println("我是服务器接收到:" + info);// }//服务器的回复os = socket.getOutputStream();//String reply = "登录成功,欢迎您";os.write(reply.getBytes());socket.shutdownOutput();} catch (Exception e){e.printStackTrace();} finally{try {if (ois !=null){ois.close();}if (br != null){br.close();}if (isr != null){isr.close();}if (is != null){is.close();}if (os != null){os.close();}}catch (IOException e) {e.printStackTrace();}}}}
客户端
package cn.bdqn.web.TCP2;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;/** 客户端*/public class LoginClient {public static void main(String[] args) {Socket socket = null;OutputStream os = null;InputStream is = null;InputStreamReader isr = null;BufferedReader br = null;ObjectOutputStream oos = null;try {socket = new Socket("localhost",8801);os = socket.getOutputStream();User user = new User("TOM", "123456");oos = new ObjectOutputStream(os);oos.writeObject(user);//String info = "我的姓名是:TOM,我的密码是:123456";//os.write(info.getBytes());socket.shutdownOutput();//接收服务器的回复信息is = socket.getInputStream();isr = new InputStreamReader(is);br = new BufferedReader(isr);String reInfo = null;while( (reInfo = br.readLine()) != null){System.out.println("客户端接收的信息为:" + reInfo);}} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally{try {if (oos != null){oos.close();}if (os != null) {os.close();}if (br != null){br.close();}if (isr != null){isr.close();}if (is != null){is.close();}if (socket != null){socket.close();}} catch (IOException e) {e.printStackTrace();}}}}
服务器端
package cn.bdqn.web.TCP2;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class LoginServer {/** 服务器*/public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;try {serverSocket = new ServerSocket(8801);while(true){//accept 等待客户端发送数据 进行通信socket = serverSocket.accept();Thread t = new LoginThread(socket);t.start();Thread.sleep(200);}} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} finally{try {if (socket != null){socket.close();}if (serverSocket != null){serverSocket.close();}} catch (IOException e) {e.printStackTrace();}}}}
9、TCP和UDP的区别?
二、测试
package cn.bdqn.test;import org.junit.After;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Ignore;import org.junit.Test;import cn.bdqn.thread.MyThread;/** 测试*/public class JunitDemo {//在test之前输出int i=0;@Beforepublic void before(){i=100;System.out.println("--before--");}@Testpublic void test(){System.out.println(i+j);}@Testpublic void test1(){//使用Junit测试框架执行程序,当前线程运行完成即关闭,子线程也会关闭//观察结果需延长当前线程的运行时间来保证线程不会关闭Thread t1=new MyThread();t1.start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}// System.out.println("test1");}//在test之后输出@Afterpublic void after(){System.out.println("--after--");}int j=0;@Ignore//不参与测试的方法public void ignore(){j=50;// System.out.println("--ignore--");}// @BeforeClass// public void BC(){// System.out.println("--BeforeClass--");// }//}
三、XML存储数据
<?xml version="1.0" encoding="UTF-8"?><books><!-- 图书信息 --><book id="bk101"><author><王珊></author><title>.NET高级编程</title><description><![CDATA["包含C#框架和网络编程等"&<''>]]></description></book><book id="bk102"><author>李明明</author><title>XML基础编程</title><description>包含XML基础概念和基本作用</description></book></books>
<?xml version="1.0" encoding="UTF-8"?><students><score name="王显明"><!--<name>王显明</name>--><estimateScore>75</estimateScore><actualScore>80</actualScore></score><score name="宋佳"><!--<name>宋佳</name>--><estimateScore>75</estimateScore><actualScore>88</actualScore></score></students>
<?xml version="1.0" encoding="UTF-8"?><phonrInfo><brand name="华为"><type>Mate30</type><type>P30</type></brand><brand name="苹果"><type>iPhone 12</type><type>iPhone 12pro</type></brand></phonrInfo>
四、DOM4J解析XML
<?xml version="1.0" encoding="UTF-8"?><phonrInfo><brand name="华为"><type>Mate30</type><type>P30</type></brand><brand name="苹果"><type>iPhone 12</type><type>iPhone 12pro</type></brand></phonrInfo>
1、加载
2、展示
3、修改-增加:element和修改:setText()
4、保存到文件
5、删除部分
package cn.bdqn.XML;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Iterator;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;public class Dom4JTest {public static void main(String[] args) {Dom4JTest dt=new Dom4JTest();//加载完成 固定的Document doc=dt.loadXml("src/cn/bdqn/XML/phone.xml");//传入xml文件路径//迭代器遍历展示// dt.showDoc(doc);dt.modifyDoc(doc);//新增小米dt.saveDoc(doc, "src/cn/bdqn/XML/phone2.xml");//保存并生成到当前目录下dt.removeDoc(doc);dt.saveDoc(doc, "src/cn/bdqn/XML/phone3.xml");//保存并生成到当前目录下System.out.println("执行成功");}//删除部分数据public void removeDoc(Document doc){//获得顶层元素,固定的Element root=doc.getRootElement();//遍历找到删除的位置Iterator<Element> it1=root.elementIterator();while(it1.hasNext()){Element e1=it1.next();Iterator<Element> it2=e1.elementIterator();a:while(it2.hasNext()){Element e2=it2.next();if(e2.getText().equals("Mate30")){//修改元素名称e2.setText("Mate40");//删除元素,用父元素删除子元素// e1.remove(e2);// e2.getParent().remove(e2);break a;}}}}//修改完成保存到文件中public void saveDoc(Document doc,String path){//设置编码,固定的OutputFormat opf=OutputFormat.createPrettyPrint();opf.setEncoding("GBK");//填入平台编码XMLWriter writer=null;try {//固定格式writer=new XMLWriter(new FileWriter(path),opf);writer.write(doc);} catch (IOException e) {e.printStackTrace();}finally{if(writer!=null){try {writer.close();} catch (IOException e) {e.printStackTrace();}}}}//修改文件public void modifyDoc(Document doc){//获得顶层元素,固定的Element root=doc.getRootElement();//新增Element newElement=root.addElement("brand");//加一个name属性newElement.addAttribute("name", "小米");//内部还要建typeElement type1=newElement.addElement("type");type1.setText("mi-11");Element type2=newElement.addElement("type");type2.setText("redmik40");//修改完成保存到文件中}//(展示)根据加载的文档输出内容public void showDoc(Document doc){//获得文档顶层元素phoneInfo,拿到顶层才能获得以后的底层Element root=doc.getRootElement();Iterator<Element> it1=root.elementIterator();//迭代器遍历while(it1.hasNext()){//e1相当于拿到了品牌Element e1=it1.next();//获得名字String name=e1.attributeValue("name");System.out.println(name);//遍历型号Iterator<Element> it2=e1.elementIterator();while(it2.hasNext()){Element e2=it2.next();System.out.println("---"+e2.getText());}}}/** 加载XML文档,读取文件返回document文档对象。*///子方法,加载XML文件public Document loadXml(String filePath){//传入路径SAXReader reader=new SAXReader();Document doc=null;try {doc=reader.read(new File(filePath));} catch (DocumentException e) {e.printStackTrace();}return doc;}}
