网络编程的基本概念
·网络的概念
·网络连接的功能
·网络编程的三要素
(建议使用10000以上)
IP和端口和Socket含义
·IP地址
·端口号
·Socket
TCP和UDP协议的区别
·TCP
特点:1.面向连接;
2.点到点的通信;
3.高可靠性,三次握手;
4.占用系统资源多,效率低
举例:打电话,发视频
·UDP
特点:
·TCP和UDP的区别
TCP协议数据传递细节
·TCP通信原理
服务器端创建serversocket,在指定端口监听并处理请求;
客户端创建socket,向服务器端发送请求
UDP协议数据传递细节
·UDP协议数据传递细节
InetAddress类_InetSocketAddress类
·InetAddress类
封装计算机IP的地址,不包含端口号。
·InetAddress类常用的方法
·InetSocketAddress类
此类用于实现IP套接字地址(IP地址+端口号),用于socket通信
URL类
·URL类
统一资源定位符,由4部分组成:协议,存放资源的主机的端口,IP,资源文件名。
例如:
URL是指向互联网资源的指针。
·常用方法
·网络爬虫
(需要用到java.net.URL中的openstream()方法)
1 package com.zqf.urlProject;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.io.OutputStreamWriter;
10 import java.net.URL;
11
12 public class TestUrl {
13 public static void main(String[] args) throws IOException {
14 /**网络爬虫
15 * (1)从网络上获取资源 www.baidu.com
16 * (2)存储到本地
17 */
18 //(1)创建URL对象
19 URL url = new URL("http://www.baidu.com"); //主页资源
20 //(2)获取字节输入流
21 InputStream is=url.openStream();
22 //(3)缓冲流
23 BufferedReader br= new BufferedReader(new InputStreamReader(is,"utf-8"));
24 //(4)存储到本地
25 BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream("index.html"),"utf-8"));
26 //(5)边读边写
27 String line = null;
28 while ((line=br.readLine())!=null){
29 bw.write(line);
30 bw.newLine();
31 bw.flush();
32 }
33 //(6)关资源
34 bw.close();
35 br.close();
36 }
37 }
基于TCP协议的socket编程_单次通信_实现单次请求与响应
·传递单个字符
1 package com.zqf.serverProject;
2
3 import java.io.OutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.ServerSocket;
7 import java.net.Socket;
8
9 public class server {
10 public static void main(String[] args) throws IOException {
11 System.out.println("-----服务器端以及启动---------");
12 //(1)创建ServerSocket对象
13 ServerSocket server = new ServerSocket(9999);
14 //(2)监听是否有客服端来请求连接
15 Socket client = server.accept();
16 //(3)获取输入流
17 InputStream is = client.getInputStream();
18 System.out.println((char)is.read());
19 //(4)获取输出流
20 //“收到了”
21 OutputStream os = client.getOutputStream();
22 os.write("收到了!".getBytes());
23 //(5)关闭流,关闭Socket
24 if(os!=null){
25 os.close();
26 }
27 if(is!=null){
28 is.close();
29 }
30 if(client!=null){
31 client.close();
32 }
33 }
34
35 }
1 package com.zqf.clientProject;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.Socket;
7
8 public class client {
9 public static void main(String[] args) throws IOException {
10 //(1)创建Socket对象
11 Socket client = new Socket("192.168.6.1",9999);
12 //(2)获取输出流
13 OutputStream os = client.getOutputStream();
14 os.write('a');
15 //(3)获取输入流
16 InputStream is = client.getInputStream();
17 byte [] buf = new byte[1024]; //存到byte类型的数组当中, (中转站)
18 int len = 0 ; // 读到的字节的个数
19 while((len=is.read(buf))!=-1){
20 System.out.println(new String (buf,0,len));
21 }
22 //(4)关闭流
23 if(is!=null){
24 is.close();
25 }
26 if(os!=null){
27 os.close();}
28 if(client!=null){
29 client.close();}
30 }
31 }
·传递字符串
1 package com.zqf.serverProject;
2
3 import java.io.OutputStream;
4 import java.io.DataInput;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.net.ServerSocket;
10 import java.net.Socket;
11
12 public class TestServer {
13 public static void main(String[] args) throws IOException {
14 System.out.println("-----服务器端以及启动---------");
15 //(1)创建ServerSocket对象
16 ServerSocket server = new ServerSocket(9999);
17 //(2)监听是否有客服端来请求连接
18 Socket client = server.accept();
19 //(3)获取输入流
20 DataInputStream dis = new DataInputStream(client.getInputStream());
21 System.out.println(dis.readUTF());
22 //(4)获取输出流
23 //“收到了”
24 DataOutputStream dos = new DataOutputStream(client.getOutputStream());
25 dos.writeUTF("收到了");
26 //(5)关闭流,关闭Socket
27 if(dos!=null){
28 dos.close();
29 }
30 if(dis!=null){
31 dis.close();
32 }
33 if(client!=null){
34 client.close();
35 }
36 }
37
38 }
1 package com.zqf.clientProject;
2
3 import java.io.DataInput;
4 import java.io.DataInputStream;
5 import java.io.DataOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.net.Socket;
10
11 public class TestClient {
12 public static void main(String[] args) throws IOException {
13 //(1)创建Socket对象
14 Socket client = new Socket("192.168.6.1",9999);
15 //(2)获取输出流
16 DataOutputStream dos = new DataOutputStream(client.getOutputStream());
17 dos.writeUTF("helloworle");
18 //(3)获取输入流
19 DataInputStream dis = new DataInputStream(client.getInputStream());
20 System.out.println(dis.readUTF());
21 //(4)关闭流
22 if(dis!=null){
23 dis.close();
24 }
25 if(dos!=null){
26 dos.close();}
27 if(client!=null){
28 client.close();}
29 }
30 }
基于TCP协议的Socket编程_双向通信_实现模拟用户登录
·多用户登录是实现:
1 package com.bjsxt.entity;
2
3 import java.io.DataInputStream;
4 import java.io.IOException;
5 import java.io.ObjectOutputStream;
6 import java.net.Socket;
7 import java.util.Scanner;
8
9 public class Client {
10 public static void main(String[] args) throws IOException {
11 //(1)创建Socket对象,用于连接服务器
12 Socket client=new Socket("localhost", 8888);
13 //(2)获取输出流 (对象流)
14 ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
15 //(3)创建User对象
16 //调用获取用户对象的方法
17 User u=getUser();//new User("bjsxt", "bjsxt");
18 //(4)User对象发送到服务器
19 oos.writeObject(u);
20
21 //(5)获取输入流(数据流)
22 DataInputStream dis=new DataInputStream(client.getInputStream());
23 System.out.println(dis.readUTF());
24 //(6)关闭流
25 if(dis!=null){
26 dis.close();
27 }
28 if(oos!=null){
29 oos.close();
30 }
31 if(client!=null){
32 client.close();
33 }
34 }
35 //获取用户对象的方法
36 public static User getUser(){
37 Scanner input=new Scanner(System.in);
38 System.out.println("请输入用户名:");
39 String userName=input.next();
40 System.out.println("请输入密码:");
41 String password=input.next();
42 //封装成User对象
43 return new User(userName,password);
44 }
45 }
1 package com.bjsxt.entity;
2
3 import java.io.Serializable;
4
5 public class User implements Serializable {//用于封装用户名和密码,
6
7 /**
8 *
9 */
10 private static final long serialVersionUID = -2618388865354437739L;
11 private String userName;
12 private String password;
13 public String getUserName() {
14 return userName;
15 }
16 public void setUserName(String userName) {
17 this.userName = userName;
18 }
19 public String getPassword() {
20 return password;
21 }
22 public void setPassword(String password) {
23 this.password = password;
24 }
25 public User(String userName, String password) {
26 super();
27 this.userName = userName;
28 this.password = password;
29 }
30 public User() {
31 super();
32 }
33
34 }
1 package com.bjsxt.server;
2
3 import java.io.DataOutputStream;
4 import java.io.IOException;
5 import java.io.ObjectInputStream;
6 import java.net.ServerSocket;
7 import java.net.Socket;
8
9 import com.bjsxt.entity.User;
10 import com.bjsxt.thread.ServerThread;
11
12 public class Server {
13 public static void main(String[] args) throws IOException, ClassNotFoundException {
14 System.out.println("----------------------服务器端已启动---------------------");
15 //(1)创建ServerSocket对象
16 ServerSocket server=new ServerSocket(8888);
17
18 while(true){
19 Socket socket=server.accept();
20 //创建线程类的对象,并启动线程
21 ServerThread st=new ServerThread(socket);
22 //启动线程
23 new Thread(st).start();
24 }
25 }
26 }
1 package com.bjsxt.thread;
2
3 import java.io.DataOutputStream;
4 import java.io.IOException;
5 import java.io.ObjectInputStream;
6 import java.net.Socket;
7
8 import com.bjsxt.entity.User;
9
10 public class ServerThread implements Runnable{
11 private Socket socket;//成员变量
12 public ServerThread(Socket socket){
13 this.socket=socket;
14 }
15 @Override
16 public void run() {
17 System.out.println(Thread.currentThread().getName()+"请求登录");
18 //(2)获取输入流--(对象流)
19 ObjectInputStream ois=null;
20 //(4)获取输出流(数据流)
21 DataOutputStream dos=null;
22 try {
23 ois = new ObjectInputStream(socket.getInputStream());
24 User user=(User) ois.readObject();
25
26 System.out.println(socket.getInetAddress().getHostAddress()+"请求登录:用户名"+user.getUserName()+"\t密码:"+user.getPassword());
27 String str="";
28 //(3)对用户名和密码进行验证
29 if("bjsxt".equals(user.getUserName())&&"bjsxt".equals(user.getPassword())){
30 str="登录成功";
31 }else{
32 str="对不起,账号号密码不正确";
33 }
34 dos = new DataOutputStream(socket.getOutputStream());
35 dos.writeUTF(str);
36 } catch (ClassNotFoundException e) {
37 // TODO Auto-generated catch block
38 e.printStackTrace();
39 } catch (IOException e) {
40 // TODO Auto-generated catch block
41 e.printStackTrace();
42 }finally{
43 //(5)关闭流
44 if(dos!=null){
45 try {
46 dos.close();
47 } catch (IOException e) {
48 // TODO Auto-generated catch block
49 e.printStackTrace();
50 }
51 }
52 if(ois!=null){
53 try {
54 ois.close();
55 } catch (IOException e) {
56 // TODO Auto-generated catch block
57 e.printStackTrace();
58 }
59 }
60 if(socket!=null){
61 try {
62 socket.close();
63 } catch (IOException e) {
64 // TODO Auto-generated catch block
65 e.printStackTrace();
66 }
67 }
68 }
69
70 }
71 }
基于TCP协议的Socket编程_聊天室_客户端多线程