1.网络模型
网络模型:OSI(开放系统互联)模型,TCP/IP模型
OSI参考模型:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层
TCP/IP参考模型:应用层,传输层,网际层,主机至网络层
2.网络通讯三要素
网络通讯三要素:IP,端口,协议
IP:InetAddress ip = InetAddress.getLocalHost();
本机IP:127.0.0.1
本机主机名:localHost
端口:0-65535
其中0-1024是系统使用或保留端口
协议:TCP协议,UDP协议
TCP与UDP的区别:
TCP:建立连接,三次握手,安全可靠,可传递大数据量,但是速度慢
UDP:不建立连接,不可靠连接,只能传递64KB以下数据,速度快
3.socket:服务网络的一种机制
(1).通信的发送端和接收端都有socket,网络通信实质上就是socket通信。通过IO流传输。
(2).获取本机ip:String ip = InetAddress.getLocalHost();
获取别的ip:String ip = InetAddress.getByName("aaa");
注:获取别的ip的步骤:
^1:先查看C:\system32\drivers\etc\hosts
^2.如果没有,就再去查看DNS(域名解析器)
4.UDP发送数据
发送端:
public static void main(String[] args) throws Exception{
DatagramSocket ds = new DatagramSocket();
String str = "啊啊啊";
byte[] bys = str.getBytes[];
DatagramPacket dp = new DatagramPacket(bys,bys.length(),InetAddress.getByName("aaa"),10000);
ds.send(dp);
ds.close();
}
接收端:
public static void main(String[] args) throws Exception{
DatagramSocket ds = new DatagramSocket(10000);
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
ds.close();
}
5.UDP实例:聊天室
public static void main(String[] args) throws Exception{
DatagramSocket send = new DatagramSocket(10001);
DatagramSokect rece = new DatagramSocket(10002);
new Thread(Send(send)).start();
new Thread(Rece(rece)).start();
}
class Send implements Runnable{
private DatagramSocket send;
Send(DatagramSocket send){
super();
this.send = send;
}
public void run(){
BufferedReader br = new BufferedReader(new inputStreamReader(System.in));
String line = null;
while((line = br.readline())!=null){
byte[] bys = line.getBytes();
DatagramPacker dp = new DatagramPacket(bys,bys.length,InetAddress.getByName("aa"),10002);
send.send(dp);
if("over".equals(line)) break;
}
send.close();
}
}
class Rece implements Runnable{
private DatagramSocket rece;
Rece(DatagramSocket rece){
super();
this.rece = rece;
}
public void run(){
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
rece.receive(dp);
String ip = dp.getAddress().getHostAddress();
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":"+str);
if("over".equals(str)){
System.out.println(ip+"离开");
}
}
}
6.TCP发送数据
发送端:
public static void main(String[] args) throws Exception{
Socket s = new Socket(10003);
OutputStream out = s.getOutputStream();
out.write("aa".getBytes());
s.close();
}
接收端:
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10003);
Server s = ss.accept();
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
int len = in.read(bys);
String str = new String(bys,0,len);
s.close();
ss.close();
}
7.TCP上传文件
客户端:
public static void main(String[] args) throws Exception{
File file = new File("D:\\1.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
Socket s = new Socket("192.168.11.11",10004);
PrintWriter pw = new PrintWriter(s.getOutputStream(),true); //自动刷新
String line = null;
while((line = br.readline())!=null){
pw.println(line);
}
pw.shutdownOutput(); //告诉服务器发送结束
//将发送的数据显示一下
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = br.readline();
System.out.println(line);
br.close();
pw.close();
s.close();
}
服务端:
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(new FileWriter("D:\\2.txt"),true);
String line = null;
while((line = br.readline())!=null){
pw.write(line);
}
//传给发送端成功消息
PrintWriter pw2 = new PrintWriter(s.getOutputStream(),true);
pw2.println(“上传成功”);
pw.close();
pw2.close;
s.close();
ss.close();
}
8.上传时可能遇到的问题:
并发:用多线程解决
文件重名:新增文件名时做校验
客户端:
public static void main(String[] args) throws Exception{
Socket s = new Socket("192.168.22.22",10005);
File file = new File("D:\\11\\1.jpg");
FileInputStream fis = new FileInputStream(file);
OutputStream out = s.getOutputStream();
byte bys = new byte[1024];
int len = 0;
while((len = fis.read(bys))!=-1){
out.write(bys,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] byIns = new byte[1024];
int lenIn = in.read(byIns);
String str = new String(lenIn,0,lenIn);
System.out.println(str);
fis.close();
close();
}
服务端:
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
new Thread(new UploadPic(s)).start();
}
//重名
public static File getFile(String d,String ip){
File dir = new File(d);
while(!dir.exists()){
dir.mkdirs();
}
int count = 1;
File file = new File(dir,ip+count+"..jpg");
if(file.exists()){
count++;
file = new File(dir,ip+count+"..jpg");
}
}
public class UploadPic implements Runnable{
private Socket s;
UploadPic(Socket s){
this.s = s;
}
public void run(){
String ip = s.getAddress.getHostAddress();
InputStream in = s.getInputStream();
File file = getFile("D:\\11",ip);
FileOutputStream fos = new FileOutputStream(file);
byte[] bys = new byte[1024];
int len = 0;
while((len = in.read(bys))!=-1){
fos.write(bys,0,len);
}
OutputStream out = s.getOutputStream();
out.write("我收到了".getBytes());
fos.close();
s.close();
}
}