TCP通信
简单的通信
客户端
public class TCPClientDemo {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//服务器的ip
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//创建Socket连接
socket = new Socket(serverIP, port);
//发送消息流
os = socket.getOutputStream();
os.write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端
public class TCPServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream bos = null;
try {
//创建服务Socket
serverSocket =new ServerSocket(9999);
while(true){
//等待用户连接
socket = serverSocket.accept();
//读取客户端消息
is = socket.getInputStream();
//利用管道流接收,避免buffer溢出
bos = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len;
while((len = is.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
System.out.println(bos.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件上传
客户端
public class TCPFileClientDemo {
public static void main(String[] args) throws IOException {
//创建Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//创建输出流
OutputStream os = socket.getOutputStream();
//读文件
FileInputStream fis = new FileInputStream(new File("head.jpg"));
//写出文件
byte[] buffer = new byte[2048];
int len;
while((len = fis.read(buffer)) != -1){
os.write(buffer, 0 ,len);
}
socket.shutdownOutput();//关闭输出流
InputStream is = socket.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[2048];
int len2;
while ((len2 = is.read(buffer2)) != -1){
bos.write(buffer2, 0, len2);
}
System.out.println(bos.toString());
//关闭资源
fis.close();
os.close();
socket.close();
}
}
接收端
public class TCPFileServerDemo {
public static void main(String[] args) throws IOException {
//创建服务
ServerSocket ss = new ServerSocket(9000);
//监听客户端连接
Socket socket = ss.accept();
//获取输入流
InputStream is = socket.getInputStream();
//输出文件
FileOutputStream fos = new FileOutputStream(new File("head_2.jpg"));
byte[] buffer = new byte[2048];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
OutputStream os = socket.getOutputStream();
os.write("接收完成!".getBytes());
//关闭资源
fos.close();
is.close();
socket.close();
ss.close();
}
}
UDP通信
简单的字符通信
发送端
public class UDPDemo01 {
public static void main(String[] args) throws Exception {
//建立一个数据报Socket连接
DatagramSocket ds = new DatagramSocket();
String msg = "hello udp";
//建立一个数据包
DatagramPacket dp = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length,
InetAddress.getByName("127.0.0.1"), 9000);
//发送数据报
ds.send(dp);
//关闭流
ds.close();
}
}
接收端
public class UDPDemo02 {
public static void main(String[] args) throws Exception {
//建立一个数据报连接并开放端口
DatagramSocket ds = new DatagramSocket(9000);
//接收数据报
byte[] buffer = new byte[2048];
DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length);
ds.receive(dp);
System.out.println(new String(dp.getData(), 0, dp.getLength()));
//关闭流
ds.close();
}
}
URL下载网络资源
public class URLDemo {
public static void main(String[] args) throws Exception {
//url下载地址
URL url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584747495037&di=722414a50eac5e185f9e1235d6aa6dfc&imgtype=0&src=http%3A%2F%2Fimg.25pp.com%2Fuploadfile%2Fyouxi%2Fimages%2F2013%2F0201%2F20130201100918698.jpg");
//连接到这个资源
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("fisher.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
fos.close();
is.close();
urlConnection.disconnect();
}
}