public static void main(String[] args) throws Exception {
//1、创建一个socket连接
Socket socket = new Socket(InetAddress.getByName(“127.0.0.1”),9000);
//2、创建一个输出流
OutputStream os = socket.getOutputStream();
//3、读取文件
FileInputStream fis = new FileInputStream(new File(“01.png”));
//4、写入文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//5.关闭资源
fis.close();
os.close();
socket.close();
}
}
3、测试
三、UDP实现数据上传
1、UDP接收端
package com.tcpip;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServerTest {
public static void main(String[] args) throws Exception {
//1、开放端口
DatagramSocket socket = new DatagramSocket(9090);
//2、接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getData().length));
//3、关闭连接
socket.close();
}
}
2、UDP发送端
package com.tcpip;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpClientTest {
public static void main(String[] args) throws Exception {
//1、建立一个socket
DatagramSocket socket = new DatagramSocket();
//2、建个包
String msg = “UDP 测试”;
InetAddress localhost = InetAddress.getByName(“localhost”);
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
//3、发送包
socket.send(packet);
//4、关闭资源
socket.close();
}
}
3、测试
四、UDP实现多线程在线咨询
1、发送端
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
package com.tcpip;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
String data = null;
try {
data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if(data.equals(“bye”)){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
2、接收端
package com.tcpip;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
//准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
//阻塞式接收包裹
socket.receive(packet);
//断开连接bye
byte[] data = packet.getData();
String receiveData = new String(data,0,data.length);
System.out.println(msgFrom + “:” + receiveData);
if(receiveData.equals(“bye”)){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
3、学生
package com.tcpip;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,“localhost”,9999)).start();
new Thread(new TalkReceive(8888,“老师”)).start();
}
}
4、老师
package com.tcpip;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,“localhost”,8888)).start();
new Thread(new TalkReceive(9999,“学生”)).start();
}
}
5、开始聊天啦,模拟请假
五、Java下载URL网络资源(图片、音乐、视频、电影)
一、前言
今天学习了一下FTP/IP的相关知识,发现一个强大的功能,用Java下载URL网络资源,据说收费的也能下! 学编程真好,Java真好。
二、代码实例
package com.tcpip;
import java.io.FileOutputStream;
import java.io.InputStream;
|