java网络编程小结

ip对应的对象

	try {
			InetAddress inet1=InetAddress.getByName("10.7.85.64");
			System.out.println(inet1);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

package com.homework;
import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class FileSystem implements Serializable {
    @Test
    public void client() throws IOException {
        Socket socket = null;
        InetAddress inet = InetAddress.getByName("127.0.0.1");
        OutputStream os = null;
        try {
            socket = new Socket(inet, 8888);
            os = socket.getOutputStream();
            os.write("你好我是客户端mm".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public  void serveClient() throws IOException {
        Socket socket=null;
        ServerSocket serverSocket=new ServerSocket(8888);
        socket=serverSocket.accept();
        InputStream is=socket.getInputStream();
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte []buffer=new byte[100];
        int len;
        while((len=is.read(buffer)) != -1){
            baos.write(buffer,0,len);
        }
        System.out.println(baos.toString());
        //此处省略各种关闭流
    }
}

UDP的发送方式

    @Test
    public void sent() throws IOException {
        DatagramSocket socket=new DatagramSocket();
        String str ="我是UDP方式发送的导弹";
        byte[]data=str.getBytes();
        InetAddress inet=InetAddress.getByName("127.0.0.1");
        DatagramPacket packet=new DatagramPacket(data,0,data.length,inet,9090);
        socket.send(packet);
        socket.close();
    }
    @Test
    public  void receiver() throws IOException {
      DatagramSocket socket=new DatagramSocket(9090);
      byte[]buffer=new byte[100];
     DatagramPacket packet= new DatagramPacket(buffer,0,buffer.length);
     socket.receive(packet);
        //此处省略各种关闭流
        System.out.println(new String(packet.getData(),0,packet.getLength()));
    }
        四种抽象基类     四种文件流           四种缓冲流
        InputStream    FileInputStream    BufferedInputStream
        OutputStream   FileOutputStream   BufferedOutputStream
        Reader         FileReader         BufferedReader
        Writer         FileWriter         BufferedWriter
        补充:
        InputStreamReader:父类是Reader
        RandomAccessFile 随机存储文件流直接继承于java.lang.object
package com.homework;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileDemo{
	public static void main(String args[]) throws IOException {
        RandomAccessFile raf1= null;
        RandomAccessFile raf2= null;
        try {
            raf1 = new RandomAccessFile(new File("D:/background.jpg"),"r");
//            r-只读,w-只写,rw-可读写
            raf2 = new RandomAccessFile(new File("D:/background2.jpg"),"rw");
            byte []buffer=new byte[1024];
            int len;
            while((len=raf1.read(buffer))!=-1){
                raf2.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(raf1!=null) {
                try {
                    raf1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(raf2!=null) {
                try {
                    raf2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
}
package com.homework;
import java.io.*;
public class FileDemo {
   public static void main(String args[]) throws IOException, ClassNotFoundException {
       ObjectOutputStream oos=null;
       ObjectInputStream  ois=null;
       try {
           FileSystem file1=new FileSystem(1);
           oos=new ObjectOutputStream(new FileOutputStream("D:/object.dat"));
           oos.writeObject(file1);
           oos.flush();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           if(oos!=null){
               try {
                   oos.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       try {
           ois=new ObjectInputStream(new FileInputStream("D:/object.dat"));
           Object obj=ois.readObject();
           String str=obj.toString();
           System.out.println(str);
       }
       catch (IOException e) {
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       } finally {
           if(ois!=null){
               try {
                   ois.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值