UDP编程
客户端发送自已机器的ip和端口到服务器(完成)
客户端
package com.gec.work;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UClient {
public static void main(String[] args) throws IOException {
//DatagramSocket
DatagramSocket ds = new DatagramSocket();
//准备ip和端口
InetAddress ia = InetAddress.getByName("localhost");
String[] split = ia.toString().split("/");
String line = "ip:" + split[1] + ",端口:" + 8888;
byte[] buf = line.getBytes();
//数据报包
DatagramPacket dp = new DatagramPacket(buf, buf.length, ia, 8888);
//发送信息到服务器
ds.send(dp);
//ds关闭
ds.close();
}
}
服务器
package com.gec.work;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UServer {
public static void main(String[] args) throws IOException {
//服务端socket
DatagramSocket ds = new DatagramSocket(8888);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//接收报文数据
ds.receive(dp);
//接收好的数据用字符串组织
String str = new String(buf,0,dp.getLength());
System.out.println("客户端的ip和端口:" + str);
//关闭
ds.close();
}
}
效果图
用网络编程完成学生信息的增,删,改,查 ,所有的信息保存在服务器
要求:使用对象流 ObjectInputStream/ObjectOutputStream
客户端:
1向服务器请求数据(查询和添加要完成)
2完成crud 功能
3完成后向服务器发送信息
客户端
package com.gec.work2;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建服务端套接字
ServerSocket ss = new ServerSocket(8888);
while(true) {
//获得客户端socket
Socket s = ss.accept();
//获取输入流
InputStream is = s.getInputStream();
//序列化
ObjectInputStream ois = new ObjectInputStream(is);
List<Student> stus = (List<Student>) ois.readObject();
for (Student stu : stus) {
System.out.println(stu);
}
//写到txt
saveData(stus);
}
}
private static void saveData(List<Student> stus) {
try(OutputStream os = new FileOutputStream("D:\\stus.txt");
ObjectOutputStream oos = new ObjectOutputStream(os)
){
oos.writeObject(stus);
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务器
package com.gec.work2;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建服务端套接字
ServerSocket ss = new ServerSocket(8888);
while(true) {
//获得客户端socket
Socket s = ss.accept();
//获取输入流
InputStream is = s.getInputStream();
//序列化
ObjectInputStream ois = new ObjectInputStream(is);
List<Student> stus = (List<Student>) ois.readObject();
for (Student stu : stus) {
System.out.println(stu);
}
//写到txt
saveData(stus);
}
}
private static void saveData(List<Student> stus) {
try(OutputStream os = new FileOutputStream("D:\\stus.txt");
ObjectOutputStream oos = new ObjectOutputStream(os)
){
oos.writeObject(stus);
} catch (Exception e) {
e.printStackTrace();
}
}
}
先运行生成stus.txt文件
package com.gec.work2;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/*
* 第一次运行程序前要先运行此类,以初始化数据!生成stus.txt
*/
public class InitData {
public static void main(String[] args) {
List<Student> stus = new ArrayList<>();
Student s = new Student(1,"admin");
stus.add(s);
//对象序列化
try(
OutputStream os = new FileOutputStream("D:\\stus.txt");
ObjectOutputStream oos = new ObjectOutputStream(os)
){
//序列化
oos.writeObject(stus);
} catch (Exception e) {
e.printStackTrace();
}
}
}
学生类
package com.gec.work2;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
效果图
文件上传案例
文件上传分析图解
【客户端】输入流,从硬盘读取文件数据到程序中。
【客户端】输出流,写出文件数据到服务端。
【服务端】输入流,读取文件数据到服务端程序。
【服务端】输出流,写出文件数据到服务器硬盘中。
基本实现
服务端实现:
package com.gec.upload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("127.0.0.1",6666);
try(
InputStream is = new FileInputStream("a.wma");
OutputStream os = s.getOutputStream();
){
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b)) >0) {
os.write(b, 0, len);
}
//写个-1到服务端
s.shutdownOutput();
InputStream is2 = s.getInputStream();
int len2 = 0;
byte[] b2 = new byte[1024];
while((len2 = is2.read(b2)) >0) {
System.out.println(new String(b2,0,len2));
}
} catch (Exception e) {
e.printStackTrace();
}
s.close();
}
}
客户端实现:
package com.gec.upload;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
public class Server {
public static void main(String[] args) throws IOException {
//创建服务端套接字
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
//写一个类封装到线程类里面
//try这块代码块写到run方法里面
// new 这个线程类,启动线程
/*
try(
InputStream is = s.getInputStream();
//下面这行代码把文件名写死了
//OutputStream os = new FileOutputStream("b.wma");
OutputStream os = new FileOutputStream(UUID.randomUUID().toString().substring(0, 8)+".wma");
OutputStream os2 = s.getOutputStream();
){
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b)) >0) {
os.write(b, 0, len);
}
//告诉客户端
os2.write("上传成功!".getBytes());
os2.flush();
} catch (Exception e) {
e.printStackTrace();
}
*/
}
}
注意:项目文件夹里面要有a.wma音频文件。
用Java写一个小型服务器页面
package com.gec.netserver;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
//循环响应
while(true) {
System.out.println("与客户端建立连接,准备响应....");
Socket s = ss.accept();
try(OutputStream os = s.getOutputStream();
) {
String str ="hello,我是你的哥哥";
//转成字节数组
byte[] b = str.getBytes();
//设置响应头 \r\n\r\n 最后结束一定要写 不写浏览器不认为是结束 的标志
String head = "HTTP/1.1 200 OK\r\n Content-Type text/html; charset=utf-8\r\n"
+ " Content-Length:" + b.length +"\r\n\r\n";
//写内容出去
os.write(head.getBytes());
os.write(str.getBytes());
System.out.println("响应到页面...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
效果图
注意:可以用localhost或者127.0.0.1加端口号登录浏览页面
文件上传优化分析
文件名称写死的问题
服务端,保存文件的名称如果写死,那么最终导致服务器硬盘,只会保留一个文件,建议使用系统时间优化,保证文件名称唯一,代码如下:
FileOutputStream fis = new FileOutputStream(System.currentTimeMillis()+".jpg") // 文件名称
BufferedOutputStream bos = new BufferedOutputStream(fis);
循环接收的问题
服务端,指保存一个文件就关闭了,之后的用户无法再上传,这是不符合实际的,使用循环改进,可以不断的接收不同用户的文件,代码如下:
// 每次接收新的连接,创建一个Socket
while(true){
Socket accept = serverSocket.accept();
…
}
案例
客户端
package com.gec.upload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("127.0.0.1",6666);
try(
InputStream is = new FileInputStream("a.wma");
OutputStream os = s.getOutputStream();
){
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b)) >0) {
os.write(b, 0, len);
}
//写个-1到服务端
s.shutdownOutput();
InputStream is2 = s.getInputStream();
int len2 = 0;
byte[] b2 = new byte[1024];
while((len2 = is2.read(b2)) >0) {
System.out.println(new String(b2,0,len2));
}
} catch (Exception e) {
e.printStackTrace();
}
s.close();
}
}
服务端
package com.gec.upload;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
public class Server {
public static void main(String[] args) throws IOException {
//创建服务端套接字
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
//写一个类封装到线程类里面
//try这块代码块写到run方法里面
// new 这个线程类,启动线程
try(
InputStream is = s.getInputStream();
//下面这行代码把文件名写死了
//OutputStream os = new FileOutputStream("b.wma");
OutputStream os = new FileOutputStream(UUID.randomUUID().toString().substring(0, 8)+".wma");
OutputStream os2 = s.getOutputStream();
){
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b)) >0) {
os.write(b, 0, len);
}
//告诉客户端
os2.write("上传成功!".getBytes());
os2.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
还有好多不了解,就不一一介绍,加油哇!老铁们