#UDP
* UDP是一种不可靠传输,它原理是将信息打包路由向指定IP地址,如果包被拦截也不会报异常,但是其省资源,大多数游戏都使用UDP
```java
//发送端
DatagramSocket ds = new DatagramSocket(); //创建一个发送端
String s = "你好"; //创建发送信息
byte[] b = s.getBytes(); //将字符串转换成字节
DatagramPacket dp = new DatagramPacket(b,b.length,InetAddress.getByName("127.0.0.1"),8888); //将发送信息打包,需要四个参,信息字节数组、字节长度、主机地址、设置端口号
ds.send(dp); //开始发送
ds.close(); //关流
//接收端
DatagramSocket ds = new DatagramSocket(8888); //创建一个接收端,接收指定端口号包,接收端要比发送端先打开等待包
byte[] b = new byte[1024]; //创建一个字节数组,预设大小
DatagramPacket dp = new DatagramPacket(b, b.length); //创建一个包
ds.receive(dp); //接收数据
String s = new String(dp.getData(),0,dp.getLength()); //将字节信息转换成字符串,dp.getLength()表示字符长度,不是字节长度
System.out.println(s);
ds.close(); //关流
```
#不停的发送与接收
```java
//不停的发送
DatagramSocket ds = new DatagramSocket(); //创建一个发送端
while (true){ //不停的发送
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//创建一个标准输入流
String s = br.readLine(); //接收输入信息
if ("quit".equals(s)){ //判断,若输入quit就退出
break;
}
byte[] b = s.getBytes();
DatagramPacket dp = new
DatagramPacket(b,
b.length,InetAddress.getByName("127.0.0.1"),7777); //打包
ds.send(dp);
}
ds.close();
//不停的接收
DatagramSocket ds = new DatagramSocket(7777); //创建一个接收端
while(true){ //不停的接收
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
System.out.println(s);
}
```
#TCP:可靠传输,它会在传输之前,让服务端与客户端建立连接,因此不会存在丢包,但是每个客户端都会创建一个连接会很耗资源
```java
public class NetDemo5 {
public static void main(String[] args) {
new Thread(new Send()).start();
new Thread(new Receive()).start();
}
}
class Send implements Runnable{
public void run(){
try {
System.out.println("客户端:");
Socket s = new Socket("127.0.0.1",8888); //创建客户端
OutputStream os = s.getOutputStream(); //创建输出流
os.write("我是客户端".getBytes()); //向服务端发送消息
InputStream is = s.getInputStream();
byte[] b = new byte[1024];
int i = is.read(b);
System.out.println(new String(b,0,i));
System.out.println("客户端结束");
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Receive implements Runnable{
public void run(){
try {
System.out.println("服务端:");
ServerSocket ss = new ServerSocket(8888);//创建服务端
Socket s = ss.accept(); //获取客户端对象
InputStream is = s.getInputStream();
byte[] b = new byte[1024];
int i = is.read(b);
System.out.println(new String(b,0,i));
OutputStream os = s.getOutputStream();
os.write("我是服务端".getBytes());
System.out.println("服务端结束");
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
#上传
* 上传与下载原理都相同,只是输入输出相反了
```java
//创建一个客户端类
class Client{
public void client() throws UnknownHostException, IOException{
Socket s = null;
OutputStream os = null;
InputStream is = null;
s = new Socket("127.0.0.1", 9999); //创建一个客户端,并指定服务端地址和端口
System.out.println("我是客户端:");
is = new FileInputStream("e:"+File.separator+"test"+File.separator+"金牛.jpg"); //创建一个输入流,读取指定文件数据
os = s.getOutputStream(); //向服务端传输数据
byte[] b = new byte[1024*1024];
int len = 0;
while ((len=is.read(b))!=-1){ //开始读取数据
os.write(b,0,len); //开始传输
}
System.out.println("上传完成");
s.close(); //关流
is.close(); //关流
}
}
//创建一个服务端类
class Sever{
public void sever() throws IOException{
System.out.println("我是服务端:");
ServerSocket ss = null;
OutputStream os = null;
InputStream is = null;
Socket s = null;
ss = new ServerSocket(9999); //服务端接收端口
s = ss.accept(); //等待客户端的请求
os = new FileOutputStream("d:"+File.separator+"test"+File.separator+
"金牛.jpg"
);
//创建一个输出流,将接收文件输出到指定文件
is = s.getInputStream(); //接收数据
byte[] b = new byte[1024*1024]; //缓冲区
int len = 0; //保存字节长度
while ((len=is.read(b))!=-1){ //开始读取数据
os.write(b,0,len); //开始输出到指定文件
}
System.out.println("上传完成");
ss.close(); //关流
os.close(); //关流
}
}
```
在输出到指定文件时,一定要给保存数据的文件(不能只给个文件夹路径)
#多用户上传
* 重点:将服务端写进线程,每个用户上传信息时就开辟一个线程
```java
//客户端
public class NetClient {
public static void main(String[] args) {
new Thread(new NetCli()).start();
}
}
class NetCli implements Runnable{
public void run() {
Socket s = null;
OutputStream os = null;
InputStream is = null;
try {
s = new Socket("127.0.0.1",10086);
is = new FileInputStream("e:"+File.separator+"test"+File.separator+"金牛.jpg");
byte[] b = new byte[1024*1024];
os = s.getOutputStream();
int len = 0;
while ((len=is.read(b)) != -1){
os.write(b,0,len);
}
System.out.println("开始上传");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
is.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//服务端
public class NetServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(10086);
while(true){
Socket s = ss.accept();
new Thread(new NetSer(s)).start(); //每个客户都给创建一个线程
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class NetSer implements Runnable{
private Socket s;
private static int i = 0;
public NetSer() {
}
public NetSer(Socket s) throws IOException {
this.s = s;
}
public void run(){
InputStream is = null;
OutputStream os = null;
try {
is = s.getInputStream(); //接收客户端数据
byte[] b = new byte[1024*1024];
int len = 0;
os = new FileOutputStream("e:"+File.separator+"test"+File.separator+(++i)+".jpg");
while ((len=is.read(b)) != -1)
os.write(b,0,len);
System.out.println("上传完成!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
os.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
```
网络编程的核心就在于明白其上传与下载原理