java网络编程02

public static void main(String[] args) {
        new Client().startup();
    }
    
    public void startup(){
        Socket s=null;
        try {
            s=new Socket("127.0.0.1",6666);
            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter out=new PrintWriter(s.getOutputStream(),true);
            
            while(true){
            String str=br.readLine();
            System.out.println(str);
            out.println("receive:"+str);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(s!=null) s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    server.java
    public class Server {
    
    private List<ServerThread> cs=null;

    public static void main(String[] args) {
        new Server().statrup();
    }
    
    public void statrup(){
        ServerSocket ss=null;
        try {
            ss=new ServerSocket(6666);
            System.out.println("server is run");
            while(true){
                Socket s=ss.accept();
                new Thread(new ServerThread(s)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private class ServerThread implements Runnable{
        private Socket s;
        private BufferedReader br;
        private PrintWriter out;
        private String name;
        private boolean flag=true;
        
        public ServerThread(Socket s) throws IOException{
            this.s=s;
            br=new BufferedReader(new InputStreamReader(this.s.getInputStream()));
            out=new PrintWriter(this.s.getOutputStream(),true);
            name=this.s.getInetAddress().getHostAddress()+":"+this.s.getPort();
            System.out.println(name+"has connected!");
            cs.add(this);
        }
        
        private void send(String msg){
            for(ServerThread st:cs){
                System.out.println(msg);
            }
        }
        
        private void stop(){
            System.out.println(name+"is out line");
            cs.remove(this);
            flag=false;
        }
        @Override
        public void run() {
            try {
                while(true){
                    if(!flag) break;
                    send("abc");
                    String str=br.readLine();
                    System.out.println(str) ;
                }
            }catch (SocketException e) {
                stop();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

////////////////////////////udp
public class Receiver {

    /*chat为TCP通信,好处在于安全,数据不丢失,
     * 但不满足高效通信,例如视频,
     * UDP允许丢包的协议*/
    public static void main(String[] args) {
        DatagramSocket ds=null;
        try {
            ds=new DatagramSocket(9999);//建立UDP接收端连接
            byte[] buf=new byte[1024];//将UDP的数据接收后放在buf中
            DatagramPacket dp=new DatagramPacket(buf,buf.length);
            while(true){
                //定义一个UDP的数据接收包
                ds.receive(dp);
                //接收数据包
                String str=new String(dp.getData(),0,dp.getLength());
                System.out.println(str);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(ds!=null) ds.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }

}

public class Sender {

    public static void main(String[] args) {
        DatagramSocket ds=null;
        try {
            //定义一个UDP的Socket来发送数据
            ds=new DatagramSocket();
            //假设发送的数据是一个字符串
            String hello="Hello World UDP!";
            //定义一个UDP的数据发送包来发送数据,InetSocketAddress表示发送到的地址
            DatagramPacket dp=new DatagramPacket(hello.getBytes(),
                    hello.getBytes().length,new InetSocketAddress("127.0.0.1",9999));
            for(int i=0;i<10;i++){
                Thread.sleep(1000);
                ds.send(dp);
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(ds!=null) ds.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }

}


转载于:https://my.oschina.net/686991/blog/509113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值