16、sockect

一、局域网因特网

  服务器是指提供信息的计算机或程序,客户机是指请求信息的计算机或程序,而网络用于连接服务器与客户机,实现两者之间的通信。但有时在某个网络中很难将服务器和客户机区分开。我们通常说的“局域网(Local Area Network  LAN)”就是一群通过一定形式连接起来的计算机。它可以由两台计算机组成,也可以由同一个区域内的上千台计算机组成。由LAN延伸到更大的范围,这样的网络称为“广域网”(Wide Area Network WAN)。我们熟悉的因特网这是由无数的LAN和WAN组成。

二、网络协议

  网络协议规定了计算机之间连接的物理、机械、电器等特征以及计算机之间的相互荀子规则、数据发送冲突的解决、长的数据如何分段传送与接收等。就像不同国家有不同的法律一样,目前网络协议也有多种。

  

三、端口与套接字

  端口:

  一般而言,一台计算机只有单一的连到网络的“物理连接”,所有的数据都通过此连接对内、对外送达特定的计算机。这就是端口。网络程序设计中的端口(port)并非真实的物理存在,而是一个假想的链接装置。端口被规定为一个在0~65535之间的整数。HTTP服务一般使用80端口,FTP使用21端口。加入一台计算机提供了HTTP、FTP等多种服务,那么客户机通过不同的端口来确定链接到服务器的哪项服务上。前1000个是系统的端口,其他的占其他的。

  套接字:

  网络程序中套接字(Socket)用于将应用程序与端口链接起来。套接字是一个假想的链接装置,就像插插头的设备“插座”,用于链接电器与电线。

过程:

1、建立套接字对象其中包含端口,客户端要加上要连接的服务器的IP地址;建立客户端对象,使用serversocket.accept()等待客户端连接。

2、建立文件对象(建立)

3、发送方:使用输入流,将文件或文字从硬盘读到内存中

4、发送方:使用输出流,将内存中的东西发送到了接收方

5、接收方建立套接字对象包含端口

6、接收方:使用输入流,将接收到的名字或文件或信息读取到内存中

7、接收方:使用输出流,将内存中缓存的名字,信息写到硬盘中。

8、先打开的后关闭,后打开的先关闭。

聊天器代码:

客户端代码:

PrintWriter()的作用是设置输出流和是否自动刷新输出

package com.zxc.O;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
 * Created by Administrator on 2018/2/8 0008.
 */
public class MyClient {
    public static void main(String[] args) {
        Socket client=null;

        try {
            client=new Socket("127.0.0.1",9081);
            PrintWriter pw=new PrintWriter(client.getOutputStream(),true);
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext()){
                String line=sc.nextLine();
                pw.println(line);
            }
            sc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

服务器代码:

package com.zxc.O;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Administrator on 2018/2/8 0008.
 */
public class MyServer {
    public static void main(String[] args) {
        ServerSocket ss=null;
        Socket client=null;
        BufferedReader br=null;
        try {
            ss=new ServerSocket(9081);
            client=ss.accept();
            br=new BufferedReader(new InputStreamReader(client.getInputStream()));
            boolean flag=true;
            while(flag){
                String line=br.readLine();
                if(line.trim().equals("exit")) {
                    flag = false;
                    System.out.println("结束");
                }else{
                    System.out.println("女孩:"+line);
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

飞鸽下载文件

服务器代码:

package com.zxc.O;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * Created by Administrator on 2018/2/8 0008.
 */
public class MyfileServer {
    public static void main(String[] args) {
        ServerSocket ss=null;
        Socket client=null;
        DataInputStream dis=null;
        DataOutputStream dos=null;
        try{
            ss=new ServerSocket(8213);
            File file=new File("d://456.txt");
            dis=new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
            client=ss.accept();
            dos=new DataOutputStream(client.getOutputStream());
            dos.writeUTF(file.getName());
            dos.flush();
            int n;
            byte[] buffer=new byte[1024];
            while((n=dis.read(buffer))!=-1){
                dos.write(buffer,0,n);
                dos.flush();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                dos.close();
                dis.close();
                client.close();
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

客户端代码:

package com.zxc.O;

import java.io.*;
import java.net.Socket;

/**
 * Created by Administrator on 2018/2/8 0008.
 */
public class MyfileClient {
    public static void main(String[] args) {
        Socket client=null;
        DataInputStream dis=null;
        DataOutputStream dos=null;
        try{
            client=new Socket("127.0.0.1",8213);
            dis=new DataInputStream(client.getInputStream());
            String filename=dis.readUTF();
            File file=new File("e://"+filename);
            dos=new DataOutputStream(new FileOutputStream(file));
            int n=-1;
            byte []buffer=new byte[1024];
            while((n=dis.read(buffer))!=-1){
                dos.write(buffer,0,n);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                client.close();
                dos.close();
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  

原文地址:https://www.cnblogs.com/television/p/8432978.html