网络编程

175 阅读2分钟

软件结构

  • C/S结构:Client/Server结构
  • B/S结构:Browser/Server结构

网络通信协议

TCP/IP协议
1.UDP
无连接通信协议,不能保证数据的完整性,通信效率高,偶尔丢包,数据被限制在64kb内,超出不能发送
2.TCP:
面向连接,可靠无差错(下载文件,网页浏览)   
三次握手:
    1.C向S发送连接请求,等待S确认
    2.S给C一个相应,通知C收到了连接请求
    3.C再次向S发送连接请求,确认连接

网络编程三要素

  • 协议
  • IP地址
  • 端口号
注意:
        1.是逻辑端口
        2.当网络软件打开时,系统为软件分配一个端口号;
        3.端口号由两个字节组成,取值范围0-65535,且小于1024的端口号不可使用
        4.不可重复
常用的端口号:
        1.80:网络端口
        2.数据库  mysql3306m,oracle:1521
        3.tomcat:8080

TCP通信

通信的步骤:

    1.创建一个客户端对象socket,构造方法绑定服务器的IP地址和端口号
    2.使用Socket对象的方法getOutputStream()获取网络字节输出流 OutputStream对象
    3.使用网络字节输出流OutputStream对象中的方法write,给服务器发送数据
    4.使用Socket对象中的getInputStream()获取网络字节输入流InputStream对象
    5.使用网络字节流InputStream对象中的方法read ,读取服务器回写的数据
    6.释放资源(socket.close();)

套接字:包含了IP地址和端口号的网络单位

例子:文件上传
client:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCP_Client {
    public static void main(String[] args) throws UnknownHostException, IOException {

        FileInputStream fis=new FileInputStream("D:\\a.jpg");
        Socket socket=new Socket("192.168.1.19",8545);
        OutputStream os=socket.getOutputStream();

        int len=0;
        byte []b=new byte[1024];

        while((len=fis.read(b))!=-1) {
            os.write(b,0,len);
        }
        socket.shutdownOutput();
        InputStream is=socket.getInputStream();

        while((len=is.read(b))!=-1) {
            System.out.println(new String(b,0,len));
        }
        fis.close();
        socket.close();
    }

}

Server:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class TCP_Server {

    public static void main(String[] args) throws IOException {
        ServerSocket sc=new ServerSocket(8545);

        while(true) {
            Socket socket=sc.accept();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        String fileName="boshy"+System.currentTimeMillis()+new Random().nextInt(888888)+".jpg";
                        InputStream is=socket.getInputStream();
                        File file=new File("D:\\test");
                        if(!file.exists()) {
                            file.mkdir();
                        }

                        FileOutputStream fos=new FileOutputStream(file+"\\"+fileName);
                        byte []b=new byte[1024];
                        int len=0;
                        while((len=is.read(b))!=-1) {
                            fos.write(b,0,len);
                        }
                        socket.getOutputStream().write("上传成功".getBytes());
                        socket.close();
                    }catch(Exception e) {
                        System.out.println(e);
                    }

                }
            }).start();
        }

    }

}