<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>
(尚硅谷)22 网络编程UDP URL
最新推荐文章于 2022-01-22 16:10:40 发布