IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 纸绘武身(网络编程) -> 正文阅读

[网络协议]纸绘武身(网络编程)

package sunday;

import java.io.;
import java.net.
;
import java.util.Scanner;

/*
UDP
TCP
留言板(UDP)
服务器反馈
客户端数据来自键盘录入
多线程上传文件
*/
public class

Demo

{
public static void main(String[] args) throws IOException {
//web();
//send();
//client();
//client1();
client2();
}//main
public static void

idea()

{
/*

*/
}
//网络编程
public static void

udp()

throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();
DatagramSocket datagramSocket1 = new DatagramSocket(1234);
//内容,内容长度,IP地址,端口号
byte[] bytes = “字符串”.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(“192.168.0.101”), 1234);
byte[] bytes1 = new byte[1024];
DatagramPacket datagramPacket1 = new DatagramPacket(bytes1,bytes1.length);
//发送,接收,解析,关闭
datagramSocket.send(datagramPacket);
datagramSocket1.receive(datagramPacket1);

byte[] data = datagramPacket1.getData();
String s = new String(data);
System.out.println(s);
datagramSocket.close();

}

public static void

tcp()

throws IOException {
Socket socket = new Socket(“192.168.0.101”, 12345);
ServerSocket serverSocket = new ServerSocket(12345);
//获取输出流,写数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write(“shuju”.getBytes());

//监听客户端连接,获取输入流,读数据,禁用此套接字的输出流
Socket socket1 = serverSocket.accept();
InputStream inputStream = socket1.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read();
String data = new String(bytes, 0, read);
System.out.println(“数据是”+data);

socket.shutdownOutput();
//解放
socket.close();
serverSocket.close();
socket1.close();

}
//留言板(UDP)
public static void

send()

throws IOException {
//创建对象
DatagramSocket datagramSocket = new DatagramSocket();
//录入数据
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line=bufferedReader.readLine())!=null) {
if (“886”.equals(line)) {
break;
}
//打包
byte[] bytes = line.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(“192.168.0.101”),12345);
//发送
datagramSocket.send(datagramPacket);
}
//关闭
datagramSocket.close();
}

//服务器反馈
public static void

client()

throws IOException {
//客户端
//找对象
Socket socket = new Socket(“192.168.0.100”, 12345);
//获取输出流,写数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write(“鸨,想你的液”.getBytes());

//接受服务器反馈
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
String data = new String(bytes, 0, read);
System.out.println(“服务器端发来:”+data);

//解放
socket.close();
}
//客户端数据来自键盘录入
public static void

client1()

throws IOException {
Socket socket = new Socket(“192.168.0.100”, 1234);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line=bufferedReader.readLine())!=null){
if(“886”.equals(line)){
break;
}
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
socket.close();
}
//多线程上传文件
public static void

client2()

throws IOException {
//找对象
Socket socket = new Socket(“192.168.0.100”, 1234);

//先读后写
BufferedReader bufferedReader = new BufferedReader(new FileReader(“E:\idea\July\12.txt”));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line=bufferedReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
socket.shutdownOutput();
//接收反馈
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = bufferedReader1.readLine();//等待读取数据
System.out.println(“服务器反馈:”+s);
//解放
bufferedReader.close();
socket.close();

}
}//end
package sunday;

import java.io.;
import java.net.
;

public class

Demo1

{
public static void main(String[] args) throws IOException {
//receive();
//server();
//server1();
server2();
}//main
public static void

receive()

throws IOException {
//建对象
DatagramSocket datagramSocket = new DatagramSocket(1234);
while (true) {
//数据包
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
datagramSocket.receive(datagramPacket);
//解析
System.out.println(“有网友留言:”+new String(datagramPacket.getData(),0,datagramPacket.getLength()));
}
}

//服务器反馈
public static void

server()

throws IOException {
//服务器端
//找对象
ServerSocket serverSocket = new ServerSocket(12345);

//监听客户端连接,返回一个socket对象
Socket socket = serverSocket.accept();
//获取输入流,读数据,并把数据显示在控制台
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
String data = new String(bytes, 0, read);
System.out.println(“客户端发来:”+data);
//给出反馈
OutputStream outputStream = socket.getOutputStream();
outputStream.write(“呦呦呦,gui”.getBytes());

//解放
serverSocket.close();
}
public static void

server1()

throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);

//监听客户端连接,返回一个socket对象
Socket socket = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line=bufferedReader.readLine())!=null){
System.out.println(line);
}
serverSocket.close();
}
//多线程上传文件
public static void

server2()

throws IOException {
//找对象
ServerSocket serverSocket = new ServerSocket(1234);
//开线程
while (true){
Socket socket = serverSocket.accept();
new Thread(new ServerThread(socket)).start();
}

}
}//end
package sunday;

import java.io.*;
import java.net.Socket;

public class

ServerThread

implements Runnable {
private Socket socket;
public ServerThread(Socket socket) {
this.socket=socket;
}

@Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(“E:\idea\July\mark.txt”));
int count=0;
File file = new File(“E:\idea\July\mark(” + count + “).txt”);
while (file.exists()){
count++;
file = new File(“E:\idea\July\mark(” + count + “).txt”);
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));

String line;
while ((line=bufferedReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();

//反馈
BufferedWriter bufferedWriter1 = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bufferedWriter1.write(“文件上传成功”);
bufferedWriter1.newLine();
bufferedWriter1.flush();
}
}catch (IOException e){
e.printStackTrace();
}

}
}

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 13:30:27  更:2021-07-28 13:30:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/25 18:42:47-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码