网络编程

获取本机ip地址

public class Demo1 {
    public static void main(String[] args) throws Exception{
        InetAddress ia1=InetAddress.getByName( "192.168.137.1" ) ;
        InetAddress ia2=InetAddress.getByName("localhost");
        System.out.println(ia1.getHostAddress());
        System.out.println(ia2.getHostAddress());
        System.out.println("---------------------------------");
        InetAddress inetAddress3=InetAddress.getByName( "10.9.21.189" );
        System.out.println(inetAddress3.getHostAddress()+inetAddress3.getHostName());
        System.out.println("--------------------------------------------------------------------------");
        InetAddress i4=InetAddress.getByName( "www.baidu.com" );
        System.out.println(i4.getHostAddress()+i4.getHostName());
        InetAddress[]all= InetAddress.getAllByName( "www.baidu.com" );
        for (InetAddress inetAddress : all) {
            System.out.println(inetAddress.getHostAddress());
        }
    }
}

客户端+服务端

public class Demo5 {
    public static void main(String[] args)throws Exception{
        ServerSocket list=new ServerSocket(10001);
        Socket socket=list.accept();
        BufferedReader br=new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
        BufferedWriter bw=new BufferedWriter( new OutputStreamWriter( socket.getOutputStream() ) );
        String data=br.readLine();
        System.out.println("客户端说"+data);
        bw.write( "好久不见" );
        bw.newLine();
        bw.flush();
        socket.close();
        list.close();
    }
}

public class Demo6 {
    public static void main(String[] args)throws Exception{
        Socket socket=new Socket( "192.168.137.1" ,10001);
        BufferedReader br=new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
        BufferedWriter bw=new BufferedWriter( new OutputStreamWriter( socket.getOutputStream() ) );
        bw.write( "甚是想念" );
        bw.newLine();
        bw.flush();
        String reply=br.readLine();
        System.out.println(reply);
        bw.close();
        br.close();
        socket.close();

    }
}

服务端+客户端+传送图片

public class Demo7 {
    public static void main(String[] args)throws Exception{
        ServerSocket lisn=new ServerSocket(8888);
        Socket socket = lisn.accept();
        InputStream is=socket.getInputStream();
        FileOutputStream fis=new FileOutputStream("a.png");
        byte[] buf=new byte[1024];
        int len=-1;
        while ((len=is.read( buf ))!=-1) {
            fis.write( buf, 0, len );
        }
fis.close();
        is.close();
        socket.close();
        lisn.close();
        System.out.println("接收完毕");

    }
}

public class Demp8 {
    public static void main(String[] args)throws Exception{
        Socket socket=new Socket( "192.168.137.1",8888 );
        OutputStream os=socket.getOutputStream();
        FileInputStream fis=new FileInputStream( "d:\\qq截图\\无标题.png" );
        byte [] buf=new byte[1024];
        int len=-1;
        while ((len=fis.read( buf ))!=-1) {
            os.write( buf, 0, len );
            os.flush();
        }
    fis.close();
    os.close();
    socket.close();
            System.out.println("ok");

        }
    }

多线程版本

public class Demo9 {
    public static void main(String[] args) throws Exception{
        ServerSocket list = new ServerSocket( 9998 );
        System.out.println("服务器已经启动");

        try {
            while (true) {
                Socket socket = list.accept();
                new Demo10(socket).start();
            }
        } catch (IOException e) {
            System.out.println("聊天结束了");
        }finally {
            list.close();
        }
    }
}

public class Demo10 extends Thread {
    private Socket socket;

    public Demo10(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        if (socket != null) {
            InputStream is = null;
            try {
                InputStream as = socket.getInputStream();
                br = new BufferedReader( new InputStreamReader( as ) );
                while (true) {
                    String data = br.readLine();
                    System.out.println( socket.getInetAddress().getHostAddress() + "shuo" + data );
                    if (data.equals( "baibai" ) || data.equals( "over" )||data.equals( "886" )) {
                        System.out.println( socket.getInetAddress().getHostAddress() + "退出" );
                        break;
                    }
                }
            } catch (Exception e) {
                System.out.println( socket.getInetAddress().getHostAddress() + "异常退出了" );
            } finally {
                try {
                    br.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class Demo11 {
    public static void main(String[] args) throws Exception{
        Socket socket=new Socket( "192.168.137.1",9998 );
        Scanner input=new Scanner( System.in );
        OutputStream os=socket.getOutputStream();
        BufferedWriter bw=new BufferedWriter( new OutputStreamWriter( os ) );
        while (true){
            String data=input.next();
            bw.write(data  );
            bw.newLine();
            bw.flush();
            if(data.equals("baibai")||data.equals("over")||data.equals("886")){
                break;
            }
        }
        bw.close();
        socket.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值