UDP协议:将数据源和目的封装成数据包,不需要建立连接;每个数据报的大小限制65k;因无连接,是不可靠协议;不需要建立连接,速度快。
TCP协议:建立连接,形成传输数据的通道;在连接中进行大数据量传输;通过三次握手完成连接,是可靠协议;必须建立连接,效率稍低。
package test5;
import java.net.InetAddress;
import java.net.UnknownHostException;
/* DESKTOP-I880TUO/169.254.49.20
*
*/
public class test5_1 {
public static void main(String[] args) throws UnknownHostException {
//InetAddress address=InetAddress.getByName("DESKTOP-I880TUO");
InetAddress address=InetAddress.getByName("169.254.49.20");
//System.out.println(address);
System.out.println(address.getHostAddress());
System.out.println(address.getHostName());
}
}
使用UDP协议发送数据
创建发送端Socket对象
创建数据并打包
发送数据
释放资源
DatagramSocket:此类表示用来发送和接受数据,基于UDP协议。
DatagramSocket():创建Socket对象并随机分配端口号。
DatagramSocket(int port):创建Socket对象并指定端口号。
发送端
package test5;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class test5_2 {
public static void main(String[] args) throws SocketException, IOException {
//创建发送端socket对象
DatagramSocket ds=new DatagramSocket();
//创建数据打包 DatagramPacket此类表示数据包
//数据byte[] 设备地址ip 进程的地址 端口号
String s="hello udp,i am comming";
byte[] bys=s.getBytes();
int length=bys.length;
InetAddress address=InetAddress.getByName("DESKTOP-I880TUO");
int port=8888;
DatagramPacket dp=new DatagramPacket(bys, length,address,port);
//发送数据
ds.send(dp);
//释放资源
ds.close();
}
}
使用UDP协议接收数据
创建接收端SOCKET对象
接收数据
解析数据
输出数据
释放资源
接收端
package test5;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class test5_3 {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(8888);
byte[] bys=new byte[1024];
DatagramPacket dp=new DatagramPacket(bys, bys.length);
ds.receive(dp);
InetAddress address=dp.getAddress();
byte[] data=dp.getData();
int length=dp.getLength();
System.out.println("sender--->"+address.getHostAddress());
System.out.println(new String(data,0,length));
ds.close();
}
}
UDP协议收发数据的注意事项:
端口号数据可以正常发出,不会异常,但是收不到。
端口号不能重复。
使用TCP发送数据
创建发送端Socket对象(创建连接)
获取输出流对象
发送数据
释放资源
发送端
package test5;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class test5_4 {
public static void main(String[] args) throws IOException {
Socket s=new Socket(InetAddress.getByName("DESKTOP-I880TUO"), 10086);
OutputStream os= s.getOutputStream();
String str="hello,i'm tcp";
os.write(str.getBytes());
}
}
使用TCP接受数据
创建发送端Socket对象(明确端口号)
监听(阻塞)
接受输入流对象
获取数据
输出
释放资源
接收端:
package test5;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class test5_5 {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10086);
Socket s=ss.accept();
InputStream is=s.getInputStream();
byte[] bys=new byte[1024];
int len=is.read(bys);
InetAddress address=s.getInetAddress();
System.out.println("client->>>>>"+address.getHostAddress());
System.out.println(new String(bys,0,len));
s.close();
}
}
模拟用户登录:
客户端:
package test6;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class test1 {
public static void main(String[] args) throws IOException, Exception {
Socket s=new Socket("DESKTOP-I880TUO", 10086);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("please input username!");
String username=br.readLine();
System.out.println("please input password!");
String password=br.readLine();
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println(username);
out.println(password);
BufferedReader serverbr=new BufferedReader(new InputStreamReader(s.getInputStream()));
String result=serverbr.readLine();
System.out.println(result);
s.close();
}
}
服务端:
package test6;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class test2 {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10086);
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String username=br.readLine();
String password=br.readLine();
boolean flag=false;
if("111".equals(username)&&"123".equals(password)){
flag=true;
}
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
if(flag){
out.println("login successful");
}else{
out.println("login fail");
}
s.close();
}
}