网络编程
package com.sgl.lession01.lession2;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TextTextDemo01{
public static void main(String[] args) throws UnknownHostException {
//查询本机地址
InetAddress byName = InetAddress.getByName("127.0.0.1");
InetAddress byName1 = InetAddress.getByName("localhost");
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(byName);
System.out.println(byName1);
System.out.println(localHost);
//查询网站ip地址
InetAddress byName2 = InetAddress.getByName("www.baidu.com");
System.out.println(byName2);
}
}
TCP
客户端
1. 连接服务器Socket
2. 发送消息
package com.sgl.lession01.lession2;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;public class TextComboboxDemo01{ public static void main(String[] args) { Socket socket = null; OutputStream os = null; try { //服务器地址和端口号 InetAddress serverIP = InetAddress.getByName("127.0.0.1"); int port = 9999; //创建一个socket连接 socket = new Socket(serverIP,port); //发送消息,IO流 os = socket.getOutputStream(); os.write("欢迎小朋友".getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { if (os!=null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
服务端
1. 建立服务的端口ServerSocket2. 等待用户的连接3. 接受用户的消息
package com.sgl.lession01.lession2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TextTextDemo01{
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//地址
serverSocket = new ServerSocket(9999);
while (true) {
//等待客户连接
socket = serverSocket.accept();
//读取客户端的信息
is = socket.getInputStream();
//管道流
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
System.out.println(byteArrayOutputStream.toString());
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (byteArrayOutputStream!=null) {
try {
byteArrayOutputStream.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();
}
}
}
}
}
文件上传
客户端
package com.sgl.lession01.lession2;import java.io.*;import java.net.InetAddress;import java.net.Socket;public class TextComboboxDemo01 { public static void main(String[] args) throws Exception { //创建Socket连接 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000); //创建一个输出流 OutputStream os = socket.getOutputStream(); //读取文件 FileInputStream fis = new FileInputStream(new File("sgl.jpg")); //写出文件 byte[] buffer = new byte[1024]; int len; while ((len= fis.read(buffer))!=-1) { os.write(buffer, 0, len); } //通知服务器,我已经结束了 socket.shutdownOutput(); //确定服务器接受完毕,才能断开连接 InputStream inputStream = socket.getInputStream(); //String byte[] ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[1024]; int len2; while ((len2=inputStream.read(buffer2))!=-1){ baos.write(buffer2,0,len2); } System.out.println(baos.toString()); //关闭资源 baos.close(); inputStream.close(); fis.close(); os.close(); socket.close(); }}
服务端
package com.sgl.lession01.lession2;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.nio.charset.StandardCharsets;public class TextTextDemo01{ public static void main(String[] args) throws Exception { //创建服务 ServerSocket serverSocket = new ServerSocket(9000); //监听客户端的连接 Socket socket = serverSocket.accept();//阻塞式连接,一直等待客户端连接 //获取输入流 InputStream is = socket.getInputStream(); //文件输出 FileOutputStream fos = new FileOutputStream(new File("receive.jpg")); byte[] buffer = new byte[1024]; int len; while ((len=is.read(buffer))!=-1){ fos.write(buffer,0,len); } //通知客户端,接收完毕 OutputStream os = socket.getOutputStream(); os.write("接收完毕,可以断开连接".getBytes(StandardCharsets.UTF_8)); //关闭资源 os.close(); fos.close(); is.close(); socket.close(); serverSocket.close(); }}
UDP
发短信:不用连接,需要直到对方地址
发送端
package com.sgl.lession03;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.nio.charset.StandardCharsets;public class UdpClientDemo01 { public static void main(String[] args) throws Exception { //建立一个Socket DatagramSocket socket = new DatagramSocket(); //建立一个包 String msg = "你好,服务器"; //发送给谁 InetAddress localhost = InetAddress.getByName("localhost"); int port = 9090; //数据,数据的起始长度,要发送给谁 DatagramPacket packet = new DatagramPacket(msg.getBytes(StandardCharsets.UTF_8), 0, msg.getBytes(StandardCharsets.UTF_8).length, localhost, port); //发送包 socket.send(packet); //关闭流 socket.close(); }}
接收端
package com.sgl.lession03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);//阻塞式接收
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭连接
socket.close();
}
}
循环发送消息
package com.sgl.lession03;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;public class UdpSenderDemo01 { public static void main(String[] args) throws Exception{ DatagramSocket socket = new DatagramSocket(8888); //准备数据 控制台读取 System.in BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true){ String data = reader.readLine(); byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666)); socket.send(packet); if (data.equals("bye")){ break; } } socket.close(); }}
循环接收消息
package com.sgl.lession03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//准备接收包裹
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(receiveData);
if (receiveData.equals("bye")){
break;
}
}
socket.close();
}
}