1public static void main(String[] args) throws IOException {
2 //創(chuàng)建數(shù)據(jù)包對(duì)象,封裝要發(fā)送的數(shù)據(jù),接收端Ip,端口號(hào)
3 byte[] data="123udp".getBytes();
4 //創(chuàng)建一個(gè)InetAddress對(duì)象,封裝自己的Ip地址
5 InetAddress inetAddress =InetAddress.getByName("127.0.0.1");
6 DatagramPacket dp =new DatagramPacket(data, data.length,inetAddress,8899);
7 //創(chuàng)建DatagramSocket對(duì)象,
8 DatagramSocket datagramSocket=new DatagramSocket();
9 datagramSocket.send(dp);
10 datagramSocket.close();
11}
1public static void main(String[] args) throws IOException {
2 //創(chuàng)建數(shù)據(jù)包傳輸對(duì)象,并綁定端口號(hào)
3 DatagramSocket ds =new DatagramSocket(8899);
4 //創(chuàng)建字節(jié)數(shù)組
5 byte[] buf=new byte[1024];
6 //創(chuàng)建數(shù)據(jù)包對(duì)象傳遞字節(jié)數(shù)組
7 DatagramPacket dp =new DatagramPacket(buf, buf.length);
8 //調(diào)用ds的receive傳遞數(shù)組
9 ds.receive(dp);
10 String ip =dp.getAddress().getHostAddress();
11 int port =dp.getPort();
12 int length=dp.getLength();
13 System.out.println(new String(buf,0,length)+"..."+ip+"..."+port);
14 ds.close();
15}
1public static void main(String[] args) throws IOException {
2 Scanner sc=new Scanner(System.in);
3 DatagramSocket datagramSocket=new DatagramSocket();
4 InetAddress inetAddress =InetAddress.getByName("127.0.0.1");
5 while(true){
6 String message=sc.nextLine();
7 byte[] data=message.getBytes();
8 DatagramPacket dp =new DatagramPacket(data, data.length,inetAddress,8899);
9 datagramSocket.send(dp);
10 }
11}
1public static void main(String[] args) throws IOException {
2 //創(chuàng)建數(shù)據(jù)包傳輸對(duì)象,并綁定端口號(hào)
3 DatagramSocket ds =new DatagramSocket(8899);
4 //創(chuàng)建字節(jié)數(shù)組
5 byte[] buf=new byte[1024];
6 //創(chuàng)建數(shù)據(jù)包對(duì)象傳遞字節(jié)數(shù)組
7 while(true){
8 DatagramPacket dp =new DatagramPacket(buf, buf.length);
9 //調(diào)用ds的receive傳遞數(shù)組
10 ds.receive(dp);
11 String ip =dp.getAddress().getHostAddress();
12 int port =dp.getPort();
13 int length=dp.getLength();
14 System.out.println(new String(buf,0,length)+"..."+ip+"..."+port);
15 }
16}
1public static void main(String[] args) throws IOException {
2 Socket socket=new Socket("120.27.60.73", 8899);
3 OutputStream outStream=socket.getOutputStream();
4 outStream.write("789456".getBytes());
5 socket.close();
6}
1public static void main(String[] args) throws IOException {
2 ServerSocket serverSocket=new ServerSocket(8899);
3 Socket socket=serverSocket.accept();
4 InputStream inputStream=socket.getInputStream();
5 byte[] buf=new byte[1024];
6 int len=inputStream.read(buf);
7 System.out.println(new String(buf,0,len));
8 //服務(wù)器返回?cái)?shù)據(jù)
9 OutputStream out=socket.getOutputStream();
10 out.write("nihao".getBytes());
11 socket.close();
12 serverSocket.close();
13}
1public class TCPClient {
2 public static void main(String[] args) throws IOException{
3 Socket socket = new Socket("127.0.0.1", 8000);
4 //獲取字節(jié)輸出流,圖片寫(xiě)到服務(wù)器
5 OutputStream out = socket.getOutputStream();
6 //創(chuàng)建字節(jié)輸入流,讀取本機(jī)上的數(shù)據(jù)源圖片
7 FileInputStream fis = new FileInputStream("c:\\t.jpg");
8 //開(kāi)始讀寫(xiě)字節(jié)數(shù)組
9 int len = 0 ;
10 byte[] bytes = new byte[1024];
11 while((len = fis.read(bytes))!=-1){
12 out.write(bytes, 0, len);
13 }
14 //給服務(wù)器寫(xiě)終止序列
15 socket.shutdownOutput();
16
17 //獲取字節(jié)輸入流,讀取服務(wù)器的上傳成功
18 InputStream in = socket.getInputStream();
19
20 len = in.read(bytes);
21 System.out.println(new String(bytes,0,len));
22
23 fis.close();
24 socket.close();
25 }
26}
1public class Upload implements Runnable{
2
3 private Socket socket;
4
5 public Upload(Socket socket){this.socket=socket;}
6
7 public void run() {
8 try{
9 //通過(guò)客戶(hù)端連接對(duì)象,獲取字節(jié)輸入流,讀取客戶(hù)端圖片
10 InputStream in = socket.getInputStream();
11 //將目的文件夾封裝到File對(duì)象
12 File upload = new File("d:\\upload");
13 if(!upload.exists())
14 upload.mkdirs();
15
16 //防止文件同名被覆蓋,從新定義文件名字
17 //規(guī)則: 域名+毫秒值+6位隨機(jī)數(shù)
18 String filename="itcast"+System.currentTimeMillis()+new Random().nextInt(999999)+".jpg";
19 //創(chuàng)建字節(jié)輸出流,將圖片寫(xiě)入到目的文件夾中
20 FileOutputStream fos = new FileOutputStream(upload+File.separator+filename);
21 //讀寫(xiě)字節(jié)數(shù)組
22 byte[] bytes = new byte[1024];
23 int len = 0 ;
24 while((len = in.read(bytes))!=-1){
25 fos.write(bytes, 0, len);
26 }
27 //通過(guò)客戶(hù)端連接對(duì)象獲取字節(jié)輸出流
28 //上傳成功寫(xiě)回客戶(hù)端
29 socket.getOutputStream().write("上傳成功".getBytes());
30
31 fos.close();
32 socket.close();
33 }catch(Exception ex){
34
35 }
36 }
37
38}
1public class TCPThreadServer {
2 public static void main(String[] args) throws IOException{
3 ServerSocket server = new ServerSocket(8000);
4 while(true){
5 //獲取到一個(gè)客戶(hù)端,必須開(kāi)啟新線(xiàn)程
6 Socket socket = server.accept();
7 new Thread( new Upload(socket) ).start();
8 }
9
10 }
11}
1String fileName="xdclass"+System.currentTimeMillis()+new Ranadow.nextInt(999999)+".jpg";
1File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");
新聞名稱(chēng):java基礎(chǔ)學(xué)習(xí)筆記:一文帶你搞懂網(wǎng)絡(luò)編程
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article28/iiiecp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、定制網(wǎng)站、網(wǎng)站收錄、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)