21---网络编程(补充)

 

java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向连接到client和server;
TCP端口和UDP端口是分开的,每个都有65536个;
首先启动server在启动client---这时应该启动两个dos窗口,一个编译执行ServerSocket一个编译执行Socket

====================服务器端======================
  import java.net.*;
  public class TCPServer{
   public static void main(String args[])throws Exception{
    //相当于起了个服务,并且监听的是6666---即该服务占得事6666这个端口
    ServerSocket ss=new ServerSocket(6666);
    /**
      *对于服务器端的该服务,可能有n个客户端去连接,所以服务器应该不断的去接
      *收(ss.accept())----服务器端一般写个死循环,这样可以不断的接受客户端的
      *请求;服务器端接受一个请求,就创建一个Socket,用这个Socket来与客户端
      *进行交互;
     */ 
     while(true){//死循环
        //接受客户端的连接,accept是堵塞的,没有连接就等待
        Socket s=ss.accept();
        //有连接上面就执行,下面就跟着执行,所以回输出下面的内容
      System.out.println("A client connect!");
       }
    }
   }
====================客户端======================
  import java.net.*;
  public class TCPClient{
   public static void main(String args[]) throws Exception{
    //创建到指定ip和端口的连接,客户端的这个服务也占端口的
    //该端口系统随机分配
    Socket s=new Socket("127.0.0.1",6666);
    }
   }
=========================================================================================================
  import java.net.*;
  import java.io.*;
  public class TCPServer{
   public static void main(String args[])throws Exception{
    //相当于起了个服务,并且监听的是6666---即该服务占得事6666这个端口
    ServerSocket ss=new ServerSocket(6666);
    /**
      *对于服务器端的该服务,可能有n个客户端去连接,所以服务器应该不断的去接
      *收(ss.accept())----服务器端一般写个死循环,这样可以不断的接受客户端的
      *请求;服务器端接受一个请求,就创建一个Socket,用这个Socket来与客户端
      *进行交互;
     */ 
     while(true){//死循环
        //接受客户端的连接,accept是堵塞的,没有连接就等待
        Socket s=ss.accept();
        //相当于创建一个来自网络连接的输入流,并被处理流包装一下
        DataInputStream dis=new DataInputStream(s.getInputStream());
        //readUTF也是堵塞式的,如果没写东西,则会等待,知道写了为止
        System.out.print(dis.readUTF());
        dis.close();
        s.close();
       }
    }
   }

------------------------------------------------------------------------------------------------------
  import java.net.*;
  import java.io.*;
  public class TCPClient{
   public static void main(String args[]) throws Exception{
    //创建到指定ip和端口的连接,客户端的这个服务也占端口的
    //该端口系统随机分配
    Socket s=new Socket("127.0.0.1",6666);
    //相当于创建一个指向TCP连接的输出流
    OutputStream os=s.getOutputStream();
    //包装一下字节流
    DataOutputStream dos=new DataOutputStream(os);
    dos.writeUTF("hello server!");
    
    dos.flush();
    dos.close();
    s.close();
    }
   }
*****************************************************服务器与客户端的交互**************************************
  import java.net.*;
  import java.io.*;
  public class TCPServer{
   public static void main(String args[])throws Exception{
    ServerSocket ss=new ServerSocket(6666);
    InputStream in=null;
    OutputStream out=null;
     try{
        Socket s=ss.accept();
        DataInputStream dis=new DataInputStream(s.getInputStream());
        DataOutputStream dos=new DataOutputStream(s.getOutputStream());
      String str=null;
      if((str=dis.readUTF())!=null){
       System.out.println(str);
       System.out.println("From:"+s.getInetAddress());
       System.out.println("Port:"+s.getPort());
       }
        
        dos.writeUTF("hi,client!"); 
        dis.close(); 
        dos.close();
        
        s.close();
      }catch(IOException e){e.printStackTrace();}
    }
   }
---------------------------------------------------------------------------------------------------------
  import java.net.*;
  import java.io.*;
  public class TCPClient{
   public static void main(String args[]) throws Exception{  
    Socket s=new Socket("127.0.0.1",6666);
    OutputStream os=s.getOutputStream();
    InputStream in=s.getInputStream();
    DataOutputStream dos=new DataOutputStream(os);
    DataInputStream dis=new DataInputStream(in);
    dos.writeUTF("hello server!");
    System.out.println(dis.readUTF());
    
    dos.flush();
    dos.close();
    dis.close();
    s.close();
    }
   }

 

===========================================================================================
  import java.net.*;
  public class UDPServer{
   public static void main(String args[]) throws Exception{
    byte buf[]=new byte[1024];
    //创建数据包,而数据包中真正存数据的是上面的数组
    DatagramPacket dp=new DatagramPacket(buf,buf.length);
    //创建一个DatagramSocket(数据包插座)---接受数据,并放到数据包中
    DatagramSocket ds=new DatagramSocket(6666);
    
    while(true){
     //接受数据存到数据包中
     ds.receive(dp);
     System.out.println(new String(buf,0,dp.getLength()));
     }
    }
   }
--------------------------------------------------------------------------------------
  import java.net.*;
  public class UDPClient{
   public static void main(String args[]) throws Exception{
    byte[] buf=(new String("hello")).getBytes();
    DatagramPacket dp=new DatagramPacket(buf,buf.length,
     new InetSocketAddress("127.0.0.1",6666));
    DatagramSocket ds=new DatagramSocket(9999);
    ds.send(dp);
    ds.close();
    }
   }

====================================================================================================
  import java.net.*;
  import java.io.*;
  public class UDPServer{
   public static void main(String args[]) throws Exception{
    byte buf[]=new byte[1024];
    //创建数据包,而数据包中真正存数据的是上面的数组
    DatagramPacket dp=new DatagramPacket(buf,buf.length);
    //创建一个DatagramSocket(数据包插座)---接受数据,并放到数据包中
    DatagramSocket ds=new DatagramSocket(6666);
    
    while(true){
     //接受数据存到数据包中
     ds.receive(dp);
     ByteArrayInputStream bais=new ByteArrayInputStream(buf);
     DataInputStream dis=new DataInputStream(bais);
     long n=dis.readLong();
     System.out.println(n);
     }
    }
   }
---------------------------------------------------------------------------------------------------
  import java.net.*;
  import java.io.*;
  public class UDPClient{
   public static void main(String args[]) throws Exception{
    long n=10000L;
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    DataOutputStream dos=new DataOutputStream(baos);
    dos.writeLong(n);
    
    byte[] buf=baos.toByteArray();
    DatagramPacket dp=new DatagramPacket(buf,buf.length,
     new InetSocketAddress("127.0.0.1",6666));
    DatagramSocket ds=new DatagramSocket(9999);
    ds.send(dp);
    ds.close();
    }
   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bzuld

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值