1.什么是网络编程
连接到外面的世界
程序1 —-程序2
2.网络编程/网站编程
Login.jsp—-网络协议—-tomcat—-Loginservlet
Java.net包
网络协议(对信息封装)
7层模型
4层模型
TCP协议:传输控制协议,可靠连接(比如打电话,可以知道连通没)
UDP:不可靠连接(比如发短信,不知道是否连通)
IP协议:网络协议,提供了唯一的IP地址
网络编程
程序1————–程序2
3.端口
程序在机器上的地址
是实现网络通信的抽象概念,是软件虚拟化出来的,而不是物理设备
4.套接字
套接字=IP+端口
5.网络编程的实现
(1)单方向
package client;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client_1 {
public static void main(String[] args) {
try {
Socket client = new Socket("127.0.0.1",5000);
PrintWriter pw = new PrintWriter(client.getOutputStream());
String msg = "我是客户端";
pw.println(msg);
pw.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server_1 {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
while(true){
Socket server = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream()));
String msg = br.readLine();
System.out.println("服务器得到消息:"+msg);
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(2)登录
package client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import admin.User;
public class Client_2 {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",6000);
User user = new User();
user.setName("test");
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(user);
out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = br.readLine();
System.out.println("登录结果:"+msg);
br.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import admin.User;
public class Server_2 {
public static void main(String[] args){
try {
ServerSocket serverSocket = new ServerSocket(6000);
while(true){
Socket socket = serverSocket.accept();
ObjectInputStream oin = new ObjectInputStream(socket.getInputStream());
User user = (User) oin.readObject();
String msg = "登录失败";
if("test".equals(user.getName())){
msg = "登录成功";
}
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println(msg);
pw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(3)双方向
Eg:
package client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client_1 {
public static void main(String[] args) {
try {
Socket client = new Socket("127.0.0.1",5000);
PrintWriter pw = new PrintWriter(client.getOutputStream());
String msg = "我是客户端";
pw.println(msg);
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
String msg2 = br.readLine();
System.out.println("客户端得到的信息:"+msg2);
br.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server_1 {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
while(true){
Socket server = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream()));
String msg = br.readLine();
System.out.println("服务器得到消息:"+msg);
PrintWriter pw = new PrintWriter(server.getOutputStream());
pw.println(msg);
pw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
请关注“知了堂学习社区”,地址:http://www.zhiliaotang.com/portal.php