日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实训23 2018.4.27

發(fā)布時間:2025/4/9 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实训23 2018.4.27 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

UDP通信

//UDPReceive.javaimport java.net.*;public class UDPReceive{public static void main(String [] args)throws Exception{//1.創(chuàng)建數(shù)據(jù)包傳輸對象DatagramSocketDatagramSocket ds=new DatagramSocket(8888);//2.創(chuàng)建字節(jié)數(shù)組,接收傳來的對象byte[] b=new byte[1024];//3.創(chuàng)建數(shù)據(jù)包對象DatagramPacket dp=new DatagramPacket(b,b.length);//4.調(diào)用receive方法接收數(shù)據(jù)包ds.receive(dp);//5.拆包//(1)獲取ip地址String ip=dp.getAddress().getHostAddress();//(2)獲取端口號int port =dp.getPort();//(3)獲取數(shù)據(jù)長度int length=dp.getLength();System.out.println(ip+":"+port+" "+new String(b,0,length));//6.關(guān)閉資源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.創(chuàng)建Socket對象,連接服務(wù)器Socket socket=new Socket("127.0.0.1",8888);//2.通過客戶端的套接字對象,調(diào)用獲取輸出流方法,將數(shù)據(jù)寫向服務(wù)器OutputStream out=socket.getOutputStream();out.write("server".getBytes());//接收服務(wù)器端回復(fù)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.關(guān)閉資源socket.close();} } //TCPServer.java public class TCPServer{public static void main(String [] args)throws Exception{//1.創(chuàng)建服務(wù)器對象ServerSocket socketServer=new ServerSocket(8888);//2.調(diào)用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));//回復(fù)信息OutputStream os=socket.getOutputStream();os.writer("received".getBytes());//5.關(guān)閉資源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();}} }

轉(zhuǎn)載于:https://www.cnblogs.com/goxxiv/p/8962923.html

總結(jié)

以上是生活随笔為你收集整理的实训23 2018.4.27的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。