两类传输协议:
TCP:
    可靠传输协议 socket<====>socket
UDP
    不可靠传输协议  (数据报包含完整的地址信息,不一定能安全传输得到)
基于Socket的网络
1)创建socket
2)打开连接到socket的输入/输出流
3)按照一定的协议对socket进行读写操作
4)关闭socket
---------------------------------------------------------------------------------------------------------------------------------------
创建socket:
构造方法:
 
Socket(InetAddress address, int port);  
Socket(InetAddress address, int port, boolean stream);  
Socket(String host, int prot);  
Socket(String host, int prot, boolean stream);  
Socket(SocketImpl impl)  
Socket(String host, int port, InetAddress localAddr, int localPort)  
Socket(InetAddress address, int port, InetAddress localAddr, intlocalPort)  
ServerSocket(int port);  
ServerSocket(int port, int backlog);  
ServerSocket(int port, int backlog, InetAddress bindAddr)

 

 
Client:
 
package com.ehealth.net;

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

public class client {

    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1",4700);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));//我们在键盘输入的东西,读取到内存
            PrintWriter os=new PrintWriter(socket.getOutputStream());//从内存插入一个输出的管子
            BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));//从内存插入一个输入的管子
            String readLine;
            readLine=sin.readLine();
            while(!readLine.equals("bye")){
                os.println(readLine);
                os.flush();
                System.out.println("Client:"+readLine);
                System.out.println("Server:"+is.readLine());
                readLine=sin.readLine();
                 
            }
            os.close();
            is.close();
            socket.close();
        } catch (IOException e) {
            System.out.println("Error"+e);
        }
    }

}

 

Server:
 
package com.ehealth.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;

public class Server {

    public static void main(String[] args) {
        ServerSocket server=null;//创建一个服务器的socket
        try {
            server=new ServerSocket(4700);//接受4700接口
        } catch (IOException e) {
            e.printStackTrace();
        }
        Socket socket=null;//socket插座
        try {
            socket=server.accept();//插座插上
        } catch (IOException e) {
            e.printStackTrace();
        }
         
        String line;
        try {
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));//插入管子,读取
            PrintWriter os=new PrintWriter(socket.getOutputStream());//插入管子,写入
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));//插入管子 读取键盘输入信息
            System.out.println("Client:"+is.readLine());//这里的is是用socket去get的,所以是先读取客户端的信息
            line=sin.readLine();//这里的sin是通过获取键盘输入的
            while(!line.equals("bye")){
                os.println(line);
                os.flush();
                System.out.println("server:"+line);
                System.out.println("Client:"+is.readLine());
                line=sin.readLine();
            }
            os.close();
            is.close();
            socket.close();
            server.close();
        } catch (IOException e) {
            System.out.println("Error:"+e);
        }
         
    }

}