关于Socket

1.新建一个ServerSocket的对象,并为他绑定一个端口
ServerSocket server = new ServerSocket();
server.bind(new InetSocketAddress("IP地址",端口)),也可以不设置IP地址,只监听某个端口
(new一个InetSocketAddress对象为他设置端口,然后为他设置一个端口,例如8888,并将InetSocketAddress对象传给QQSocket对象作为接口)
2.server.accept()方法可以拿到一个Socket对象,Socket这个对象可以获得输入流。
3.Socket的对象可以输出数据到特定的端口,但是server.accept获得的对象无法使用再去绑定一个端口或IP。
(Socket socket = new Socket(); socket.connect(new InetSocketAddress(IP,端口))去获得连接,
再使用socket.getOutputStream()方法输出想要的数据到刚刚设置的地址。
package study01;

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

public class QQThread extends Thread {
    int a = 100;

    @Override
    public void run() {
        while (true){
            if (post()){
                break;
            }
        }
    }

    private boolean post(){
        Socket socket = null;
        OutputStream os = null;
        try {
            socket = new Socket();
            Thread.sleep(100);
            socket.connect(new InetSocketAddress("localhost",8848));
            os = socket.getOutputStream();//设置输出流
            String s = "Hello Server"+(100-(--a));
            os.write(s.getBytes());//
            os.flush();
            os.close();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return a == 0;
        }

    }
}
package study01;


import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class QQ {
    public static void main(String[] args) {
        QQThread thread = new QQThread();
        thread.start();
        QQServer(thread.a);
    }

    private static void QQServer(int a) {
        ServerSocket server = null;//ServerSocket用于监听某个端口或者某个IP的端口
        Socket socket = null;//Socket用于对流的处理
        InputStream is = null;
        try {
            server = new ServerSocket();
            //设置监听的端口和IP
            server.bind(new InetSocketAddress(8848));
            while (a>0){
                //等待端口的数据,如果没有就会一直在这等待
                socket = server.accept();
                //获取端口的数据的输入流
                is = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int len;
//                System.out.println(is.available());//0
                while ((len = is.read(bytes))>0){//这里用available方法拿不到byte数组的值,是因为网络分段传输的问题
                    System.out.println(new String(bytes,0,len));
                }
                a--;
//                System.out.println(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{//关闭各种连接
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (server!=null){
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

posted @ 2021-03-09 18:57  温柔Rarry  阅读(121)  评论(0)    收藏  举报