Socket编程java[简单易懂],附带Android Studio 项目聊天实例

服务器端

package socket_util;

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

public class Server {
    public void run() throws IOException{
        //服务端socket对象
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress("127.0.0.1",1234));

        while(true) {
            Socket socket = serverSocket.accept();
            //接收客户端发来的数据
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String str = null;
            if( (str = br.readLine()) != null) {
                System.out.println(str);
            }
            socket.close();
        }
    }

    public static void main(String[] args) {
        Server s = new Server();
        System.out.println("服务器端开启");
        try {
            s.run();
        } catch (IOException e) {
            System.out.println("IO异常");
            e.printStackTrace();
        }
    }
}

客户端

package socket_util;

import java.io.IOException;
import java.net.Socket;

public class Client {
    public void send(String data) throws IOException {
        //创建socket对象,指定ip、port
        Socket socket = new Socket("127.0.0.1", 1234);
        //将数据传到输出管道字节流发送到服务器端
        socket.getOutputStream().write(data.getBytes());
        //清空IO管道重的缓存数据
        socket.getOutputStream().flush();
        socket.close();
    }

    public static void main(String[] args) {
        Client c = new Client();
        try {
            c.send("123456789,nimabinijiehunlema");
            System.out.println("发送成功");
        } catch (IOException e) {
            System.out.println("IO异常");
            e.printStackTrace();
        }
    }
}

扫描服务器端是否开启

package socket_util;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/*
 * 必须在服务器端开启的时候才能扫描成功
 */
public class ScanPorts {
    private int minPort, maxPort;

    public ScanPorts(int minPort, int maxPort) {
        this.minPort = minPort;
        this.maxPort = maxPort;
    }
    public void run() throws IOException {
        for(int i = minPort; i <= maxPort; i++) {
            Socket socket = new Socket();
            SocketAddress socketAddress = new InetSocketAddress("127.0.0.1", i);
            socket.connect(socketAddress, 50);
            System.out.println(i + ":ok");
            socket.close();
        }
    }

    public static void main(String[] args) {
        ScanPorts sc = new ScanPorts(1234, 1234);
        try {
            sc.run();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

运行结果截图

  • 运行服务器端Server.java
    这里写图片描述

  • 运行扫描端ScanPorts.java
    这里写图片描述

  • 运行服务器端Client.java
    这里写图片描述

  • 这是打开之前服务器端的控制台输出,会显示发送来的数据
    这里写图片描述

项目聊天APP实例

具体不介绍了,链接如下:
https://github.com/wangleihitcs/MyChat

你可能感兴趣的:(Android,Studio)