网络编程部分案例总结

网络编程部分案例总结


1.网络编程的三要素
A:IP地址
B:端口
是应用程序的标示。范围:0-65535,其中0-1024被系统占用或保留,不建议使用
C:协议
UDP:数据打包传输,包大小有限制,不连接,效率高,传输中可能会丢包,不可靠
TCP:建立数据传输通道,数据大小无限制,效率低,可靠

2.UDP
A:最基本的UDP协议发送和接收数据
发送端:
public class SendDemo{
public static void main(String[] args) throws IOException{
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//创建IP地址对象
InetAddress address = InetAddress.getByName("192.168.1.124");
//创建数据并打包
byte[] bys = "hello,我来了!".getBytes();
int length = bys.length;
int port = 10088;
DatagramPacket dp = new DatagramPacket(bys,length,address,port);
//调用Socket对象的发送端发送数据
ds.send(dp);
//释放资源
ds.close();
}
}
发送端简化版:
public class SendDemo{
public static void main(String[] args) throws IOException {
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//创建数据并打包
byte[] bys = "hello,大家好!".getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),12345);
//发送数据
ds.send(dp);
//释放资源
ds.close();

}
}

接收端:
public class ReceiveDemo{
public static void main(String[] args) throws IOException{
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(10088);
//创建一个接收数据包(接收容器)
byte[] bys = new byte[1024];
int length = bys.length;
DatagramPacket dp = new DatagramPacket(bys,length);
//调用Socket对象的接收方法接收数据
ds.receive(dp);
//解析数据包并显示在控制台上
InetAddress address = dp.getAddress();
String IP = address.getHostAddress();
byte[] bys2 = dp.getData();
int len = dp.getLength();
String str = new String(bys2,o,len);
System.out.println(IP +"传递的数据是:"+ str);
//释放资源
ds.close();
}
}
接收端简化版:
public class ReceiveDemo{
public static void main(String[] args) throws IOException {
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(12345);
//创建一个接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据并打印
String IP = dp.getAddress().getHostAddress();
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(IP + "传递的数据是:" + str);
//释放资源
ds.close();
}
}

B:键盘录入数据发送和接收
发送端:
public calss SendDemo{
public static void main(String[] args) throws IOException {
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
//创建数据并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),10011);
//发送数据
ds.send(dp);
}
//关闭资源
}
}

接收端:
public calss ReceiveDemo{
public static void main(String[] args) throws IOException{
//创建接收端Socket对象
DatagramSocket ds = new DatagramSocket(10011);

while(true){
//创建接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据
String IP = dp.getAdress().getHostAdress();
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(IP + "传输的数据是:" + str)
}
}
}

C:使用多线程实现同一个窗口下数据的接收和发送(模拟聊天窗口)
数据发送线程:
public class SendThread implements Runnable{
private DatagramSocket ds;
public SendThread(DatagramSocket ds){
this.ds = ds;
}
public void run(){
try{
//封装键盘录入数据
BufferedReader  br = new BufferedReader(new InputStreamReader(System.in));

String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
//创建数据并打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,
InetAddress.getByName("192.168.1.124"),10068);
//发送数据
ds.send(dp);
}
//释放资源
ds.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
数据接收线程:
public class ReceiveThread implements Runnable {
private DatagramSocket ds;
pubic ReceiveThread(DatagramSocket ds){
this.ds = ds;
}

public void run(){
try{
while(true){
//创建一个接收包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//接收数据
ds.receive(dp);
//解析数据
String IP = dp.getAddress().getHostAddress();
String str = new String(dp.getDate(),0,dp.getLength());
System.out.println(IP + " : " + str);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
聊天室:
public class ChatRoom{
public static void main(String[] args) throws IOException{
//创建发送端和接收端的Socket对象
DatagramSocket dsSend = new DatagramSocket();
DategramSocket dsReceive = new DatagramSocket(12345);
//创建发送端和接收端的资源对象
SendThread st = new SendThread(dsSend);
ReceiveThread rt = new ReceiveThread(dsReceive);
//创建线程
Thread t1 = new Thread(st);
Thread t2 = new Thread(rt);
//开启线程
t1.start();
t2.start();
}
}

3.TCP
A:最基本的TCP协议发送和接收数据
客户端:
public static ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",8888);
//创建输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,TCP,我来了!".getBytes());
//释放资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端Socket对象
ServerSocket ss = new ServerSocket(8888);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//创建输入流,读取数据并显示在控制台
InputStream is = s.getInputStream();

byte[] bys = new byte[1024];
int len = is.read(bys);
String str = new String(bys,0,len);
String IP = s.getInetAddress().getHostAddress();
System.out.println(IP + " : " + str);
//释放资源
s.close();
}
}

B:服务器端给出反馈信息
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10089);
//获取输出流对象,写出数据
OutputStream os = s.getOutputStream();
os.write("天气不错,适合睡觉!".getBytes());

//获取输入流对象,读入数据并显示
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String client = new String(bys,0,len);
System.out.println("client: "+client);
//关闭资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(10089);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//获取输入流对象,读入数据并显示
InputStream is = s.InputStream();
byte[] bys = new byte[1024];
len = is.read(bys);
String server = new String(bys,0,len);
System.out.println("server: " + server);

//获取输出流对象,写出反馈信息
OutputStream os = s.getOutputStream();
os.write("数据已收到!".getBytes());
//关闭资源
s.close();
}
}

C:客户端键盘录入,服务器控制台输出
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",9999);
//创建键盘录入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//包装通道传输流为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
if("886".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(9999);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//包装通道传输流数据为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//读出数据并显示
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
//关闭资源
s.close();
}
}

D:客户端键盘录入,服务器端写到文本文件
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",12345);
//封装键盘录入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//把通道传输流封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
if("over".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端对象
ServerSocket ss = new ServerSocket(12345);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//把通道传输流封装为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//封装文本文件写出数据
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
bw.close();
s.close();
}
}

E:客户端读取文本文件,服务器端控制台输出
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10022);
//封装文本文件
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//把通道传输流封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
br.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args){
//创建服务器端对象
ServerSocket ss = new ServerSocket(10022);
//监听客户端连接,返回Socket对象
Socket s = ss.accept();
//把通道传输流封装为高效字符流
BuferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream));
//读入数据并显示
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
//关闭资源
s.close();
}
}

F:客户端读取文本文件,服务器端写到文本文件,服务器端给出反馈。
客户端:
public calss ClientDemo{
public static void main(String[] args) throws IOException{
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",10088);
//封装文本文件对象
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//把通道传输里封装为高效字符流
BufferedWriter bw = new BufferedWriter(new OutputStream(s.getOutputStream()));
//写出数据
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//结束标记
s.shutdownOutput();
//接收反馈信息并显示
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = brClient.readLine();
System.out.println(str);
//关闭资源
s.close();
br.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args){
//创建服务器端对象
ServerSocket ss = new ServerSocket(10088);
//监听客户端连接,返回Socket对象
Socket s = ss.accept()
//封装文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
//把通道传输流封装为高效字符流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//写出文本
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件传输完毕!");
bwServer.newLine();
bwServer.flush();
//关闭资源
s.close();
bw.close();
}
}

G:上传图片到服务器,服务器端给出反馈信息
客户端:
public class ClientDemo{
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.124",6666);
//封装图片对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aa.jpg"));
//把通道传输流封装为高效字节流
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
//写出数据
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read()) != -1){
bos.write(bys,o.len);
bos.flush();
}
//结束标记
s.shutdownOutput();
//接收反馈并显示
InputStream is = s.getInputStream();
byte[] bys2 = new byte[1024];
int len2 = is.read(bys2);
String client = new String(bys2,0,len2);
System.out.println(client);
//关闭资源
bis.close();
s.close();
}
}

服务器端:
public class ServerDemo{
public static void main(String[] args) throws IOException{
//创建服务器端对象
ServerSocket ss = new ServerSocket(6666);
//监听客户端,返回Socket对象
Socket s = ss.accept();
//封装图片对象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("aa.jpg"));
//把通道传输流封装为高效字节流
BufferedInputStream bis = new BufferedInputStream(s.getInputStream);
//读出数据
byte[] bys = new byte{1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0.len);
bos.flush();
}
//给出反馈
OutputStream os = s.getOutputStream();
os.write("图片上传完毕!".getBytes());
//关闭资源
bos.close();
s.close();
}
}

H:上传图片的多线程改进
客户端:
无变化
服务器端:
public class ServerThread implements Runnable{
private Socket s;
public ServerThread(Socket s){
this.s = s;
}

public void run(){
try{
//封装通道传输流为高效字节流
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
//封装文件对象
String newName = System.currentTimeMillis() + ".jpg";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newName));
//读入数据
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0,len);
bos.flush();
}
//给出反馈
OutputStream os = s.getOutputStream();
os.write("图片上传完毕!".getBytes());
//关闭资源
bos.close();
r.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

public class ServerDemo{
public static void main(String[] args) throws IOException {
//创建服务器端对象
ServerSocket ss = new ServerSocket(12345);

while(true){
Socket s = ss.accept();
new Thread(new ServerThread(s)).start();
}
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值