UDP通信协议
发送数据
package UDPDEmo;import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;public class SendMessageDemo {public static void main(String[] args) throws IOException {//发送数据//1.创建对象//细节://绑定端口号,以后我们通过这个端口往外发送//空参:所有可用的端口随机一个使用//有参:指定端口号进行绑定DatagramSocket ds=new DatagramSocket();
//2.打包数据String str="karry";byte[] bytes=str.getBytes();InetAddress byName = InetAddress.getByName("127.0.0.1");int port=10086;DatagramPacket dp=new DatagramPacket(bytes, bytes.length,byName,port);ds.send(dp);ds.close();}
}
接收数据
package UDPDEmo;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;public class ReceiveMessageDemo {public static void main(String[] args) throws IOException {//接收数据//细节://在接收的时候一定要绑定端口//而且绑定的端口一定要跟发送的端口保持一致DatagramSocket ds=new DatagramSocket(10086);byte[] bytes=new byte[1024];DatagramPacket dp=new DatagramPacket(bytes, bytes.length);ds.receive(dp);byte[] data = dp.getData();int length = dp.getLength();InetAddress address = dp.getAddress();int port = dp.getPort();System.out.println("接收到的数据"+new String(data,0,length));System.out.println("数据是从"+address+"这台电脑中的"+port+"这个端口发出的");ds.close();}
}
聊天室代码
package UDPDEmo.uddemo01;import java.io.IOException;
import java.net.*;
import java.util.Scanner;public class SendMessageDemo {public static void main(String[] args) throws IOException {DatagramSocket ds=new DatagramSocket();Scanner sc=new Scanner(System.in);while (true) {System.out.println("请输入要说的话");String str=sc.nextLine();if("886".equals(str)){break;}byte[] bytes=str.getBytes();InetAddress inetAddress = InetAddress.getByName("127.0.0.1");int port=10086;DatagramPacket dp=new DatagramPacket(bytes, bytes.length,inetAddress,port);ds.send(dp);}ds.close();}
}
package UDPDEmo.uddemo01;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;public class ReceiveMessageDemo {public static void main(String[] args) throws IOException {DatagramSocket ds=new DatagramSocket(10086);byte[] bytes=new byte[1024];DatagramPacket dp=new DatagramPacket(bytes, bytes.length);while (true) {ds.receive(dp);byte[] data = dp.getData();int length = dp.getLength();String hostAddress = dp.getAddress().getHostAddress();String hostName = dp.getAddress().getHostName();System.out.println("ip为"+hostAddress+",主机名为"+hostName+"的人,发送了数据:"+new String(data,0,length));}}
}
UDP的通信方式
单播
组播:
组播地址:224.0.0.0-239.255.255.255,其中224.0.0.0-224.0.0.255为预留的组播地址
广播:255.255.255.255
TCP通信程序
TCP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket对象,通信之前要保证连接已经建立,通过Socket产生IO流进行网络通信。
package UDPDEmo.tcpd1;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;public class Client {public static void main(String[] args) throws IOException {Socket socket=new Socket("127.0.0.1",10000);OutputStream os = socket.getOutputStream();os.write("aaa".getBytes());os.close();socket.close();}
}
package UDPDEmo.tcpd1;import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) throws IOException {ServerSocket serverSocket=new ServerSocket(10000);Socket socket = serverSocket.accept();InputStream is = socket.getInputStream();int b;while((b=is.read())!=-1){System.out.println((char)b);}socket.close();
serverSocket.close();}
}
package UDPDEmo.tcpd1;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) throws IOException {ServerSocket serverSocket=new ServerSocket(10000);Socket socket = serverSocket.accept();InputStream is = socket.getInputStream();InputStreamReader isr=new InputStreamReader(is);BufferedReader br=new BufferedReader(isr);int b;while((b=br.read())!=-1){System.out.println((char)b);}socket.close();
serverSocket.close();}
}
package UDPDEmo.tcpd1;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;public class Client {public static void main(String[] args) throws IOException {Socket socket=new Socket("127.0.0.1",10000);OutputStream os = socket.getOutputStream();os.write("aaa".getBytes());os.close();socket.close();}
}
练习
多发多收
package UDPDEmo.test1;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;public class Client {public static void main(String[] args) throws IOException {Socket socket=new Socket("127.0.0.1",10000);OutputStream os = socket.getOutputStream();String str= null;while (true) {Scanner sc=new Scanner(System.in);System.out.println("请输入您要发送的信息");str = sc.nextLine();if("886".equals(str)){break;}}os.write(str.getBytes());socket.close();}
}
package UDPDEmo.test1;import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;public class Server {public static void main(String[] args) throws IOException {ServerSocket ss=new ServerSocket(10000);Socket socket = ss.accept();InputStream is = socket.getInputStream();InputStreamReader isr=new InputStreamReader(is);int b;while((b=isr.read())!=-1){System.out.println((char)b);}socket.close();ss.close();}
}
接收和反馈
package UDPDEmo.test2;import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;public class Client {public static void main(String[] args) throws IOException {Socket socket=new Socket("127.0.0.1",10000);String str="见到你很高兴";OutputStream os = socket.getOutputStream();os.write(str.getBytes());socket.shutdownOutput();InputStream is = socket.getInputStream();InputStreamReader isr=new InputStreamReader(is);int b;while((b=isr.read())!=-1){System.out.println((char)b);}socket.close();}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) throws IOException {ServerSocket ss=new ServerSocket(10000);Socket socket = ss.accept();InputStream is=socket.getInputStream();InputStreamReader isr=new InputStreamReader(is);int b;while((b=isr.read())!=-1){System.out.println((char)b);}//回写数据String str="到底有多开心";OutputStream os = socket.getOutputStream();os.write(str.getBytes());socket.close();ss.close();}
}
上传文件
package UDPDEmo.test3;import java.io.*;
import java.net.Socket;public class Client {public static void main(String[] args) throws IOException {Socket socket=new Socket("127.0.0.1",10000);BufferedInputStream bis=new BufferedInputStream(new FileInputStream("F:\\javaEE\\clientdir\\a.jpg"));BufferedOutputStream bos=new BufferedOutputStream(socket.getOutputStream());byte[] bytes=new byte[1024];int len;while((len=bis.read())!=-1){bos.write(bytes,0,len);}socket.shutdownOutput();BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));String line = br.readLine();System.out.println(line);socket.close();}
}
package UDPDEmo.test3;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) throws IOException {ServerSocket ss=new ServerSocket(10000);Socket socket = ss.accept();BufferedInputStream bis=new BufferedInputStream(socket.getInputStream());BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("F:\\javaEE\\serverdir"));int len;byte[] bytes=new byte[1024];while((len=bis.read())!=-1){bos.write(bytes,0,len);}BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));bw.write("上传成功");bw.newLine();bw.flush();socket.close();ss.close();}
}