JavaSE复习_12 Socket网络编程

△客户端使用Scanner与BufferedReader的异同,Scanner在客户端调用s.shutdownoutput的时候,将会因为读不到行而报异常,但是BufferedReader的readline方法则会正常终止.因此应当根据实际情况选择客户端服务端的流输入.

△实例:根据DatagramSocket和DatagramPacket建立发送端和接收端。

 接收端:

public class Receive {

        public static void main(String[] args) throws IOException {
              System. out.println("接收端启动……" );
//            建立socket流
              DatagramSocket ds=new DatagramSocket(9999);
               //将要接受的数据封装到数据包中
               byte[] b =new byte[1024];
              DatagramPacket dp= new DatagramPacket(b ,b .length );
               //接受数据
               ds.receive( dp);
               //获取数据中 的信息
              String s= new String(dp .getData(),0,dp.getLength());
              System. out.println(dp .getAddress().getHostAddress()+"::"+ dp.getPort()+ "..."+s );
       }

}

发送端:

public class Send {

        public static void main(String[] args) throws IOException {
              System. out.println("发送端启动。。。。。。。。。。" );
              String s= "hello world!" ;
               byte[] b =s .getBytes();
               //建立socket连接
              DatagramSocket ds= new DatagramSocket();
               //将要发送的数据信息封装到数据包中
              DatagramPacket dp= new DatagramPacket(b,b .length ,InetAddress.getByName ("192.168.0.103" ),9999);
               //将数据包的数据发送出去
               ds.send( dp);
               //关闭socket
               ds.close();
       }

}
△TCP客户端和服务端练习时,要注意以下两点:
 1.输出流需要及时刷新,保证这一点可以让PrintWriter参数列表里有true。
 2.readline方法必须要读取到换行符,否则会一直处于阻塞状态
 3.在文件读写时,当服务端读写客户端的输入流时,可以在客户端用shutdownoutput方法告诉服务端,(半关闭状态)客户端的读写已经结束。
△TCP传输也可以使用带资源的try语句(方便自动关闭):
public class Server1 {
     public static void main(String[] args) throws IOException {
         ServerSocket ss =new ServerSocket(9998);
          try (Socket s =ss .accept();) {

         PrintWriter pw= new PrintWriter(s .getOutputStream(), true);
//       Scanner scanner=new Scanner(s.getInputStream());
          try (BufferedReader bufr = new BufferedReader(new InputStreamReader(s .getInputStream()));) {
         String str= null ;
          while ((str =bufr .readLine())!= null) {
              pw.println( str.toUpperCase());
         }
         }
         }
    }
}

△TCP服务端是多线程的:在接受到一个Socket后,在run方法内执行操作(开启一条线程操作,主线程继续等待接受)标准代码如下:

public class Server2 {
     public static void main(String[] args) throws IOException {
         ServerSocket ss =new ServerSocket(9999);
          while (true ) {
         Socket s= ss.accept();
         Runnable r=new RunnableImplements(s);
         Thread t=new Thread(r);
         t.start(); 
         }
    }
}
class RunnableImplements implements Runnable {
     private Socket incoming;
     public RunnableImplements(Socket i) {
          incoming=i;
     }
     public void run() {
          System.out.println("任务开始");
           //任务
     }
}
△可中断套接字:
 当通过套接字读写数据的时候,当前线程会被阻塞直到操作或产生超时为止.为了中断套接字操作,可以使用SocketChannel类.如下:
SocketChannel channel=SocketChannel.open(new InetSocketAddress(host,port));        //获取通道
Scanner in=new Scanner(channel);            //获取输入流,或者Channel.newInputStream()方法获取字节输入流
OutputStream out=Channels.newOutputStream(channel);     //获取输出流
 这时候,如果服务器线程正在执行打开或者读取或写入操作,此时如果线程中断,那么这些操作将不会陷入阻塞,而是已抛出异常的形式结束.
△URL类可以封装统一资源定位符,URL类可以打开一个到资源的流,可以调用openConnection方法,建立连接,调用getHeadFields方法获取Map对象类型的相应头.下面是示例(将输出响应头):
public class Demo1 {
     public static void main(String[] args) throws IOException {
         URL url= new URL("http://www.baidu.com" );
         URLConnection con= url.openConnection();
          con.connect();
         Map<String,List<String>> headers =con .getHeaderFields();
          for (Map.Entry<String,List<String>> entry :headers .entrySet()) {
             String key= entry.getKey();
              for (String value :entry .getValue()) {
                 System. out .println(key +"::" +value );
             }
         }
    }
}
△URL和URLConnection实现提交表单数据:
1.URL封装资源定位符
2.打开连接,采用openConncetion方法.
3.connection.setDoOutput(true);建立可以输出的连接
4.PrintWriter out=new PrintWriter(connection.getOutPutStream()); 获取可以输出数据的流
5.调用Out的打印方法,发送数据.
原文地址:https://www.cnblogs.com/hlhdidi/p/5635917.html