网络编程
1.网络编程的概念
- 程序1—程序2的连接
2.网络编程和网站编程的区别
login.jsp——–网络协议——-tomcat——–loginServlet
- 网络协议
- 7层模型
- 4层模型
TCP:传输控制协议,可靠连接
UDP:不可靠连接
IP:网络协议,提供了唯一的IP地址
网络编程
程序1连到程序2,程序1有一个ip,程序2有一个ip
3.端口
- 程序在机器上的地址
4.套接字
- 套接字=ip+端口
5.网络编程的实现
程序A通过java.net包获得IP+端口号发送给程序B,程序B可以获取到程序A的IP+端口号
客户端:
指定要连接的套接字,通过java实现网络编程的类发送请求
服务器端:(ServerSoket(监听请求)/Socket(处理请求))
一直打开监听,是否有客户端的请求到来
如果有请求到来回启动一个新的去处理请求
服务器端的套接字都是成对的,一个接听请求,一个处理请求
本质:对流的处理
登录案例
user实体类
package domin;
import java.io.Serializable;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id ;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
服务端发送请求,并获取服务器端返回结果
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 domin.User;
/**
* 界面----service----dao
* @author Administrator
*
*/
public class LoginClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1",5000);
//界面得到的数据
User user = new User();
user.setName("test");
//发送user到服务器
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();
}
}
服务器端接收客户端的请求,并返回请求结果
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import domin.User;
public class LoginServer {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//侦听
ServerSocket serverSocket = new ServerSocket(5000);
while(true){
Socket socket = serverSocket.accept();
//验证登录:得到客户端的User对象
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
User user = (User) inputStream.readObject();
String msg = "登录失败";
if("test".equals(user.getName())){
msg="登录成功";
}
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.println(msg);
printWriter.close();
}
}
}
文件上传案例
发送文件到服务器端,并接受服务器返回结果
package client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
public class Client_2 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",5000);
//发送文件到服务器端
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//读取文件后再进行各发送
BufferedReader br = new BufferedReader(new FileReader("F:\\file\\1.txt"));
String line = "";
while((line = br.readLine())!= null){
writer.write(line);
}
br.close();
writer.flush();
socket.shutdownOutput();
//接收服务器端的消息是否上传成功
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = in.readLine();
System.out.println("是否长传成功?"+msg);
in.close();
socket.close();
}
}
服务器读取文件
package server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Server_2 {
public static void main(String[] args) throws UnknownHostException, IOException {
ServerSocket serverSocket = new ServerSocket(5000);
while (true) {
Socket socket = serverSocket.accept();
//接收服务器端上传的文件放置到服务器目录下
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//接收网络流后再写入文件
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line = "";
while((line=br.readLine())!=null){
writer.write(line);
}
writer.close();
//给服务器端发送信息上传成功
PrintWriter pw = new PrintWriter(socket.getOutputStream());
String msg = "上传成功";
pw.println(msg);
pw.close();
}
}
}