标签:
△客户端使用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(); } }
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 channel=SocketChannel.open(new InetSocketAddress(host,port)); //获取通道 Scanner in=new Scanner(channel); //获取输入流,或者Channel.newInputStream()方法获取字节输入流 OutputStream out=Channels.newOutputStream(channel); //获取输出流
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 ); } } } }
标签:
原文地址:http://www.cnblogs.com/hlhdidi/p/5635917.html