(尚硅谷)22 网络编程UDP URL

<span style="font-size:18px;">//TCP图片传输

package com.atguigu.ACTIVE;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class TCP {
	@Test
	public void TestClinder(){
		Socket socket = null;
		BufferedInputStream in =null;
		OutputStream out = null;
		try {
			socket = new Socket (InetAddress.getByName("127.0.0.1"),9090);
			in = new BufferedInputStream(new FileInputStream("34.jpg"));
			out = socket.getOutputStream();
			byte b[] = new byte[20];
			int len ;
			while((len = in.read(b))!=-1){
				out.write(b, 0, len);
				out.flush();
			}
			System.out.println("传输完成");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void Test2(){
		Socket socket = null;
		InputStream in = null;
		BufferedOutputStream out = null;
		try {
			ServerSocket serverSocket = new ServerSocket(9090);
			System.out.println("初始化web");
			socket = serverSocket.accept();
			in = socket.getInputStream();
			out = new BufferedOutputStream(new FileOutputStream("342.jpg"));
			byte [] b = new byte [20];
			int len;
			while((len = in.read(b))!=-1){
				out.write(b, 0, len);
			}
			System.out.println("输出完成");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
/*************************************************************
package com.atguigu.ACTIVE;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import org.junit.Test;
public class UDP {
	@Test
	public void send() {
		DatagramSocket socket =null;
		try {
			socket = new DatagramSocket();
			byte[] b = "hellow word!".getBytes();
			DatagramPacket pack = new DatagramPacket(b, 0, b.length,
					InetAddress.getByName("127.0.0.1"), 9090);
			socket.send(pack);
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(socket!=null){
				socket.close();
			}
		}
	}
	@Test
	public void Test(){
		DatagramSocket socket = null;
		try {
			socket = new DatagramSocket(9090);
			byte [] b = new byte [1024];
			DatagramPacket packet = new DatagramPacket(b, 0 , b.length);
			socket.receive(packet);
			String str = new String (packet.getData() , 0 ,packet.getLength());
			System.out.println(str);
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(socket!=null){
				socket.close();
			}
		}
	}
}
/******************************************************
package com.atguigu.ACTIVE;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL {
	public static void main(String[] args) throws Exception {
		URL url = new URL("");
		System.out.println(url.getProtocol());//获取URL的协议名
		System.out.println(url.getHost());//获取url的主机名
		System.out.println(url.getPort());//获取URL的端口号
		System.out.println(url.getPath());//获取URL的文件路径
		System.out.println(url.getFile());//获取URL文件名
		System.out.println(url.getRef());//获取URL文件的相对位置
		System.out.println(url.getQuery());//获取URL的文件中的位置
	}
}
</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值