网络编程之Socket

Socket 是一个通信链路的端点,它提供给应用程序互相访问的接口,它处在java.net包下.
InetAddress 类:IP地基于TCP的Socket编程用到的类

网络编程之Socket_第1张图片
QQ截图20170822221401.png

测试InetAdress的上述函数

package com.qf.demo;

import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 * 测试本机 的地址
 * localhost
 * 127.0.0.1
 * 
 * @author Administrator
 *
 */
public class Test {

    public static void main(String[] args) {
        
        try {
            // 得到本机的信息
            InetAddress inetAddress = InetAddress.getLocalHost();
            // 得到主机名
            String name = inetAddress.getHostName();
            System.out.println(name);
           // 得到主机的ip地址
            String address = inetAddress.getHostAddress();

            System.out.println(address);
            
            
            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2.getHostName());
            System.out.println(inetAddress2.getHostAddress());
            
            
            InetAddress inetAddress3 =InetAddress.getByName("localhost");
            // 如果获取不到主机名  主机名是展示的是ip地址
            System.out.println(inetAddress3.getHostName());

            System.out.println(inetAddress3.getHostAddress());
            
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Socket应用

  • 客户端发送数据给服务端
  • 1 创建Socket
  • 2 准备发送的数据
  • 3 将数据放到socket中
  • 4 (调配物流车, 物流车送货)
  • 5 关闭socket
    *注意: 先运行 服务端 在运行客户端

客户端代码

public class Client {

    public static void main(String[] args) {
        //1 创建Socket
        Socket socket =  null;
        OutputStream os =null;
        try {
            System.out.println("客户端起来了");
            socket = new Socket("127.0.0.1", 6666);
//          2 准备发送的数据
            String string = "hello world";
//           3 将数据放到socket中
            os = socket.getOutputStream();
            // 将数据交给快递员写给快点点
            os.write(string.getBytes());
            os.flush();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
}

  • 服务端接收数据
  • 1 创建服务端的快递点
  • 2 等待接收数据
  • 3 接收到数据 , 就可以从快递点拿出数据
  • 4 关socket

服务器端代码

public class Server {

    public static void main(String[] args) {
        //  1 创建服务端的快递点
        ServerSocket serverSocket =null;
        Socket socket =  null;
        InputStream is =null;
        try {
            System.out.println("服务端起来了");
             serverSocket = new ServerSocket(6666);
            // 2 等待接收数据
             socket = serverSocket.accept();// 如果没有接收到数据, 阻塞程序执行, 直到接收到数据
            // 3 接收到数据 , 就可以从快递点拿出数据
            is = socket.getInputStream();
            byte[] bs= new byte[1024];
            int num = is.read(bs);
            String string = new String(bs, 0, num);
            System.out.println("服务端接收到的数据是: "+string);
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

当我们运行服务器的时候服务器会等着接受Socket,如果没有则进入阻塞状态
当我们运行客户端的时候将数据给Socket那么服务端就会由阻塞进入就绪状态,然后进入执行状态执行下面的代码
那么服务端可不可以和客户端进行互相通信呢
下面的例子就是互相通信的
客户端

package com.qf.demo4;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import com.qf.demo3.Util;

/**
 * 控制台循环输入数据 发送给 服务端
 * 
 * @author Administrator
 *
 */
public class Client {

    public static void main(String[] args) {
        // 1 创建快递点
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        try {

            socket = new Socket("10.0.143.51", 8888);
            // 2 准备要发送的数据
            Scanner scanner = new Scanner(System.in);
            // 3 获得快递员
            os = socket.getOutputStream();
            is = socket.getInputStream();
            while (true) {
                String data = scanner.next();
                os.write(data.getBytes());
                os.flush();
                if ("over".equals(data)) {
                    break;
                }

                // 收到回信
                byte[] bs = new byte[1024];
                int num = is.read(bs);
                String reault = new String(bs, 0, num);
                System.out.println("服务器回复的数据是  : " + reault);
                if ("over".equals(reault)) {
                    break;
                }
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            Util.closed(null, socket, is, os);
        }

    }
}

服务端

package com.qf.demo4;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import com.qf.demo3.Util;

public class Server {

    public static void main(String[] args) {
        // 1 创建 服务端的socket
        ServerSocket serverSocket = null;
        Socket socket =null;
        InputStream is =null;
        OutputStream os  = null;
        try {
             serverSocket = new ServerSocket(8888);
            // 2 等待接收客户端的数据
             socket = serverSocket.accept();
             is = socket.getInputStream();
             os  =  socket.getOutputStream();
            Scanner scanner = new Scanner(System.in);
            // 3  读取信息
            while(true){
                byte[] bs = new byte[1024];
                int num = is.read(bs);
                String string = new String(bs, 0, num);
                System.out.println("客户端发送了: "+string);
                if("over".equals(string)){
                    break;
                }
                
                // 以下是回复数据
                String result = scanner.next();
                os.write(result.getBytes());
                os.flush();
                if("over".equals(result)){
                    break;
                }
                
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            Util.closed(serverSocket, socket, is, os);
        }
        
    }
}

你可能感兴趣的:(网络编程之Socket)