public class Basic_001_Client {
public static void main(String[] args) throws Exception {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(inetAddress, port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你好".getBytes());
outputStream.close();
socket.close();
}
}
服务端
java复制代码
@Slf4j
public class Basic_002_Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9999);
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
log.info(byteArrayOutputStream.toString());
byteArrayOutputStream.close();
inputStream.close();
socket.close();
}
}
}
3.文件上传下载
客户端
java复制代码
public class File_001_client {
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.文件流
final FileInputStream fileIs = new FileInputStream("/Users/qinyingjie/Documents/idea-workspace/ant/ant-netty/src/main/java/Electron.png");
//4.写出文件
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = fileIs.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
//5.关闭资源
fileIs.close();
os.close();
socket.close();
}
}
服务端
java复制代码
public class File_002_server {
public static void main(String[] args) throws Exception {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端连接
Socket socket = serverSocket.accept();
//3.获取输入流
InputStream inputStream = socket.getInputStream();
//4.文件输出
FileOutputStream fileOut = new FileOutputStream("/Users/qinyingjie/Documents/idea-workspace/ant/ant-netty/src/main/java/receive.png");
final byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOut.write(buffer, 0, len);
}
//5.关闭资源
fileOut.close();
inputStream.close();
socket.close();
}
}
4.UDP
udp 没有客户端服务端的概念,主要是为了演示
客户端
java复制代码
public class UDP_001_client {
public static void main(String[] args) throws Exception {
//1.创建一个socket
DatagramSocket socket = new DatagramSocket();
//2.建立一个包
String msg = "你好";
final InetAddress inetAddress = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, inetAddress, port);
//3.发送
socket.send(packet);
//4.关闭资源
socket.close();
}
}
服务端
java复制代码
@Slf4j
public class UDP_002_server {
public static void main(String[] args) throws Exception {
//1.创建socket
DatagramSocket socket = new DatagramSocket(9090);
//2.接收数据包
final byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
log.info(packet.getAddress().getHostAddress());
log.info(new String(packet.getData(), 0, packet.getLength()));
//3.关闭资源
socket.close();
}
}
5.chat
send 方
java复制代码
public class Chat_001_send {
public static void main(String[] args) throws Exception {
//1.创建一个socket
DatagramSocket socket = new DatagramSocket(8888);
//2.控制台输入
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
final String data = reader.readLine();
final byte[] datas = data.getBytes();
final DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
//3.发送
socket.send(packet);
if (StringUtils.equalsIgnoreCase(data, "bye")) {
break;
}
}
//4.关闭资源
socket.close();
}
}
receive 方
java复制代码
@Slf4j
public class Chat_002_receive {
public static void main(String[] args) throws Exception {
//1.创建socket
DatagramSocket socket = new DatagramSocket(6666);
while (true) { //2.接收数据包
final byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//断开连接 bye
final byte[] data = packet.getData();
final String receive = new String(data, 0, packet.getLength());
log.info(receive);
if (StringUtils.equalsIgnoreCase(receive, "bye")) {
break;
}
}
//3.关闭资源
socket.close();
}
}
6.双人交流
java复制代码
@Slf4j
public class TalkReceive implements Runnable {
private DatagramSocket socket;
private final int port;
private final String msgFrom;
@SneakyThrows
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
this.socket = new DatagramSocket(port);
}
@SneakyThrows
@Override
public void run() {
//1.创建socket
while (true) { //2.接收数据包
final byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//断开连接 bye
final byte[] data = packet.getData();
final String receive = new String(data, 0, packet.getLength());
log.info(msgFrom + ": " + receive);
if (StringUtils.equalsIgnoreCase(receive, "bye")) {
break;
}
}
//3.关闭资源
socket.close();
}
}
java复制代码
public class TalkSend implements Runnable {
DatagramSocket socket;
BufferedReader reader;
private final int fromPort;
private final String toIp;
private final int toPort;
public TalkSend(int fromPort, String toIp, int toPort) throws Exception {
this.fromPort = fromPort;
this.toIp = toIp;
this.toPort = toPort;
//1.创建一个socket
this.socket = new DatagramSocket(fromPort);
//2.控制台输入
this.reader = new BufferedReader(new InputStreamReader(System.in));
}
@SneakyThrows
@Override
public void run() {
while (true) {
final String data = reader.readLine();
final byte[] datas = data.getBytes();
final DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIp, this.toPort));
//3.发送
socket.send(packet);
if (StringUtils.equalsIgnoreCase(data, "bye")) {
break;
}
}
//4.关闭资源
socket.close();
}
}
java复制代码
public class TalkStuden {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(7777, "localhost", 9999)).start();
new Thread(new TalkReceive(8888, "老师")).start();
}
}
public class TalkTeacher {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(5555, "localhost", 8888)).start();
new Thread(new TalkReceive(9999, "学生")).start();
}
}
7.URL
java复制代码
@Slf4j
public class UrlDemo {
public static void main(String[] args) throws Exception {
final URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123");
log.info(url.getAuthority());//localhost:8080
log.info(url.getPath());///helloworld/index.jsp
log.info(url.getProtocol());//http
log.info(url.getHost());//localhost
log.info(url.getFile());///helloworld/index.jsp?username=kuangshen&password=123
log.info(url.getUserInfo());//null
log.info(url.getQuery());//username=kuangshen&password=123
log.info(url.getRef());//null
log.info(String.valueOf(url.getDefaultPort()));//80
log.info((String) url.getContent());
}
}