[黑马程序员](第27天)网络编程

来源:互联网 发布:sql server 合计行 编辑:程序博客网 时间:2024/06/09 21:31
------- android培训、java培训、期待与您交流! ----------网络通信协议:
这些连接和通信的规则被称为网络通信协议,它对数据的传输格式、传输速率、传输步骤等作了统一规定,通信双方
必须同时遵守才能完成数据交换。
tcp/ip协议:
应用层:HTTP,ftp,dns
传输层:tcp,udp
网络层:ip
链路层:驱动程序,接口


IP地址和端口号:
IPv4:4个字节
IPv6:16个字节


InetAddress
用于封装一个ip地址


主要方法:InetAddress getByName(String host)---返回指定主机名情况下主机的ip地址(host表示指定的主机)
InetAddress getLocalHost()---返回本地主机的InetAddress对象


UDP和TCP:
UDP:无连接通信协议
TCP:面向连接的通信协议(”三次握手“,安全性)


DatagramPacket:
”集装箱“,用于封装UDP通信中的发送或者接收的数据
DatagramPacket(byte[] buf,int length)---用于接收端
DatagramPacket(byte[] buf,int length , InetAddress addr, int port)---用于发送端
DatagramPacket(byte[] buf,int length)---同第一个,offset参数用于指定接受到的数据在放入buf缓冲数组时是从offset出开始的


主要方法:
byte[] getData()---返回将要发送的数据,或者返回接收到的数据




DatagramSocket:
”码头“,这个类的实例对象可以发送和接收DatagramPacket数据包




主要方法:
void receive(DatagramPacket p)---填充数据到DatagramPacket数据包,在接收到数据之前会一直处于阻塞状态,只有当接受到数据包时,该方法才会返回
void send(DatagramPacket p)---发送DatagramPacket数据包,发送的数据包中包含将要发送的数据,数据的长度,远程主机的IP地址和端口号
暗的、
多态的弊端:
父类引用不能使用子类的特有功能






TCP通信:
ServerSocket:
服务器端程序,常用构造方法:
ServerSocket()---还要结合bind(SocketAddress endpoint)才能使用
ServerSocket(int port)---如果port=0,系统就会分配一个还没有被其他网络程序所使用的端口号
ServerSocket(int port, int backlog)---backlog用于指定在服务器忙时,可以与之保持连接请求的等待
客户数量(default = 50)


常用方法:
Socket accept()
InetAddress getInetAddress()
boolean isClosed()
void bind(SocketAddress endpoint)---endpoint封装了ip地址和端口号




Socket:
用于实现tcp客户端程序


常用方
int getPort()---返回Socket对象与服务器端连接的端口号
InetAddress getLocalAddress()---并将IP地址封装成InetAddress类型的对象返回
InputStream getInputStream()---用于读取服务器端或者客户端的数据
OutputStream getOutputStream()---用于向客户端发送数据,反之,用于向服务器发送数据




tcp案例——文件上传


由于文件上传需要数据的安全性和完整性,很明显需要使用tcp协议来实现。
思路:分别建立客户端和服务器端,然后分别实例化一个线程来实现数据的交流
核心代码:
Socket s = serverSocket.accept()---获得客户端Socket对象
String ip = socket.getInetAddress().getHostAddress()---获取服务器端的ip地址
InputStream in = socket.getInputStream()---获得客户端的数据传入流
OutputStream ops = socket.getOutputStream()---获得服务器端的数据传出流
File parentFile = new File("d:\\upload\\").mkdir();---用于创建文件目录
并且在while(true)无限循环中调用ServerSocket的accept()方法,每当和一个
客户端建立Socket连接后,就开启一个新的线程和这个客户端进行交互


public class ServerSocket {
public static void main(String[] args){
ServerSocket serverSocket = new ServerSocket();
while(true){
Socket s = serverSocket.accept();
new Thread(new myRunnable(s)).start();
}
}
public myRunnable implements Runnable {
public Socket s;
public myRunnable(Socket s){
this.s = s;
}
public void run(){
InputStream in = s.getInputStream();
int count = 0;//count用来避免图片名字的重复
try{
String ip = s.getInetAddress().getHostAddress();
File parentFile = new File("D:\\upload\\");
if(!parentFile.exists()){
parentFile.mkdir();
}
File file  = new File(parentFile,ip+count+".jpg");
while(file.exists()){
file = new File(parentFile,ip+(count++)+".jpg");
}
FileOuputStream fos = new FileOutputStream(file);
//开始读取和存入数据
byte[] bytes = new byte[1024];
int len = 0;
while((len = in.read(bytes))!= -1){
fos.write(bytes,0,len);
}
in.close();
//发送信息给客户端
OutputStream out = s.getOutputStream();
out.write("上传成功!");
catch(Exception e){
e.printStackTrace();
}finally{
out.close();
s.close();
}i
}
}
}


//客户端不用使用线程,服务器端之所以要用多线程是因为会有多个客户端的访问
class Socket{
public static void main(String[] args){
Socket socket = new Socket("168.120.1.70",10001);
OutputStream out = socket.getOutputStream();
FileInputStreawm fis  = new FileInputStream("D:\\1.png");
int len = 0;
byte[] bytes = new byte[1024];
while((fis.read(bytes) = len) != -1){
out.write(bytes,0,len)
}
socket.shutdownOutput();
InputStream ips = socket.getInputStream();
int i = 0;
byte[] b = new bytes[1024];
i = ips.read(b);
System.out.println(new String(b,0,i));//String有一个特殊的构造方法,就是传数组和两个参数
fis.close();
ips.close();
}
}









0 0