实训23 2018.4.27
生活随笔
收集整理的這篇文章主要介紹了
实训23 2018.4.27
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
UDP通信
//UDPReceive.javaimport java.net.*;public class UDPReceive{public static void main(String [] args)throws Exception{//1.創建數據包傳輸對象DatagramSocketDatagramSocket ds=new DatagramSocket(8888);//2.創建字節數組,接收傳來的對象byte[] b=new byte[1024];//3.創建數據包對象DatagramPacket dp=new DatagramPacket(b,b.length);//4.調用receive方法接收數據包ds.receive(dp);//5.拆包//(1)獲取ip地址String ip=dp.getAddress().getHostAddress();//(2)獲取端口號int port =dp.getPort();//(3)獲取數據長度int length=dp.getLength();System.out.println(ip+":"+port+" "+new String(b,0,length));//6.關閉資源ds.close();} } //UDPSend.javaimport java.net.*; import java.util.Scanner;public class UDPSend{public static void main(String []args)throws Exception{InetAddress inet=InetAddress.getByName("127.0.0.1");DatagramSocket ds=new DatagramSocket();String mes=new Scanner(System.in).next(Line();byte[] b =mes.getBytes();DatagramPacket dp=new DatagramPacket(b,b.length,inet,8888);ds.send(dp);ds.close();} }TCP通信
//TCPClient.javaimport java.io.*; import java.net.*;public class TCPClient{public static void main(String [] args)throws Exception{//1.創建Socket對象,連接服務器Socket socket=new Socket("127.0.0.1",8888);//2.通過客戶端的套接字對象,調用獲取輸出流方法,將數據寫向服務器OutputStream out=socket.getOutputStream();out.write("server".getBytes());//接收服務器端回復InputStream in =socket.getInputStream();byte[] b=new byte[1024];int len=in.read(b);System.out.print(socket.getInetAddress+":"+socket.getPort()+" "+new String(b,0,len));//3.關閉資源socket.close();} } //TCPServer.java public class TCPServer{public static void main(String [] args)throws Exception{//1.創建服務器對象ServerSocket socketServer=new ServerSocket(8888);//2.調用accept方法接收客戶端socketSocket socket=secketServer.accept();//3.獲取輸入流InputStream in =socket.getInputStream();//4.讀取信息byte[] b=new byte[1024];int len =in.read(b);System.out.println(socket.getInetAddress()+":"+socket.getPort()+" "+new String(b,0,len));//回復信息OutputStream os=socket.getOutputStream();os.writer("received".getBytes());//5.關閉資源in.close;socket.close();serverSocket.close();} } //TCPPictureClient.javaimport java.io.*; import java.net.*;public class TCPPictureClient(){public static void main(String [] args)throws Exception{Socket socket=new Socket("127.0.0.1",8888);FileInputStream fis=new FileInputStream("F:\\0.jpg");int i=0;byte[] b=new byte[1024];OutputStream out=socket.getOutputStream();while((i=fis.read(b))!=-1){out.write(b,0,i);}socket.shutdownOutput();InputStream in=socket.getInputStream();byte[] bs=new byte[1024];int len=in.read(bs);System.out.println(socket.getInetAddress() + ":" + socket.getPort() );socket.close();fis.close();in.close();} } //TCPThread.javaimport java.io.*; import java.net.*;public class TCPThread{public static void main(String [] args)throws Exception{while(true){ServerSocket =new ServerSocket(8888);new Thread(new Upload(socket)).start();}} }class Upload implements Runnable{private Socket socket;public Upload(Socket socket){this.socket=socket;}public void run(){//這里需要處理異常,但是我沒有try{InputStream in=socket.getInputStream();FileOutputStream fos=new FileOutputStream("G:\\a"+System.currentTimeMillis()+".jpg");byte[] b = new byte[1024];int len = 0;while ((len = in.read(b)) != -1) {fos.write(b, 0, len);}System.out.println(socket.getInetAddress() + ":" + socket.getPort());OutputStream os = socket.getOutputStream();os.write("received".getBytes());in.close();fos.close();socket.close();}catch(Exception e){throw new RuntimeException();}} }轉載于:https://www.cnblogs.com/goxxiv/p/8962923.html
總結
以上是生活随笔為你收集整理的实训23 2018.4.27的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P1484 种树
- 下一篇: MyBatis自动生成代码之genera