UDP代码示例
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(12345);
byte[] bys = new byte[1024];
DatagramPacket packet = new DatagramPacket(bys,bys.length);
while(true){
ds.receive(packet);
byte[] data = packet.getData();
String str = new String(data,0,packet.getLength());
System.out.println("UDP服务器接收到数据 "+str);
}
}
}
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
byte[] bys = "hello UDP 22".getBytes();
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(bys,bys.length,ip,12345);
ds.send(packet);
ds.close();
}
}
TCP代码示例
- TCP与UDP不同,TCP在发送数据之前必须要经过三次握手建立连接,只有连接建立成功才发送数据。而UDP不同,UDP程序的客户端发送数据给接收端的时候根本不在乎接收端是否存在,数据是否丢失,它直接把数据丢给目标地址就完事了。
- TCP服务器代码,不断运行等待TCP请求
public class TCPServer {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket ss = new ServerSocket(12345);
while(true){
Socket socket = ss.accept();
System.out.println("检测到一个tcp请求");
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
byte[] by = new byte[3];
int len = in.read(by);
while(len != -1){
String data = new String(by,0,len);
Thread.sleep(3000);
System.out.println("接收到客户端数据:"+data);
len = in.read(by);
}
String[] data = {"111","222","333","444"};
for(String s:data){
Thread.sleep(3000);
out.write(s.getBytes());
}
socket.shutdownOutput();
System.out.println("关闭当前tcp连接");
socket.close();
}
}
}
public class TCPClient {
public static void main(String[] args) throws IOException, InterruptedException {
Socket socket = new Socket("127.0.0.1",12345);
OutputStream out = socket.getOutputStream();
String[] data = {"aaa","bbb","ccc","ddd"};
for(String s:data){
Thread.sleep(3000);
out.write(s.getBytes());
}
socket.shutdownOutput();
InputStream in = socket.getInputStream();
byte[] by = new byte[3];
int len = in.read(by);
while(len!=-1){
String res = new String(by,0,len);
System.out.println("接收到服务器反馈数据:"+res);
len = in.read(by);
}
}
}
- 客户端请求建立连接,然后客户端操作io流向服务器发送数据,服务器读取io流来读取数据,当读取完毕后,服务器操作io流向客户端发送数据,客户端读取io流读取服务器返回的数据,当客户端读取完毕,tcp连接断开。
- 通过上例代码可以发现,tcp连接建立之后,会得到两个IO流,服务器指向客户端的IO流和客户端指向服务器的IO流,通过操作这两个IO流完成对对方的读写操作。
- 而且tcp连接是全双工通信的,也就是客户端和服务器完全可以同时向对方发送数据。
- 对于上例代码,服务器只有一个线程,当客户端1与服务器建立连接后,客户端2只能等待,等客户端1的tcp连接断开,服务器再继续处理客户端2的请求。即服务器只能通过主线程串行处理客户端请求。
- 可以改成多线程的方式,服务器遇到一个请求,单独创建一个新的线程处理请求,建立tcp连接,这样服务器就可以并发处理多个客户端请求。
public class TCPServer {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket ss = new ServerSocket(12345);
while(true){
new Thread(()->{
try {
Socket socket = ss.accept();
System.out.println("检测到一个tcp请求");
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
byte[] by = new byte[3];
int len = in.read(by);
while(len != -1){
String data = new String(by,0,len);
Thread.sleep(3000);
System.out.println("接收到客户端数据:"+data);
len = in.read(by);
}
String[] data = {"111","222","333","444"};
for(String s:data){
Thread.sleep(3000);
out.write(s.getBytes());
}
socket.shutdownOutput();
System.out.println("关闭当前tcp连接");
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
}
总结
- 使用UDP编程,发送端给接收端发送一个数据包后,一次通信就结束了。发送多个数据包表示经过了多次通信,但是两个计算机并不需要建立连接,发生一次通信代价很小。
- 使用TCP编程时,整个输入输出流传输完成,才表示一次通信的结束。再发送一个输入输出流要重新建立通信连接。
- UDP类似a向b简单打个招呼,但是这个招呼b可能听到也可能没听到,至于是否听到a也不管。
- TCP类似a与b之间进行一次长对话,因此在长对话前要先建立连接,双方要互相询问对方有没有空,如果双方都有空才能建立连接,双方才开始交谈。因为是可靠传输,a所说的每一句话b都能听到 而且不会乱序。
基于UDP实现一个聊天软件
- 每个聊天进程都要维护一个udp接收端和udp发送端,启动两个用户线程,发消息线程负责持续接收用户输入,通过udp发送端 发送给对方接收线程的udp服务端。收消息线程负责维护udp接收端持续接收对方聊天进程的消息。
- 代码如下所示
public class MyQQ {
public static void main(String[] args) throws IOException {
int thisport = 12345;
String thatip = "127.0.0.1";
int thatport = 12346;
DatagramSocket server = new DatagramSocket(thisport);
byte[] serverbys = new byte[1024];
DatagramPacket packetserver = new DatagramPacket(serverbys,serverbys.length);
DatagramSocket client = new DatagramSocket();
new Thread(()->{
try{
InetAddress ip = InetAddress.getByName(thatip);
Scanner sc = new Scanner(System.in);
while (true){
String data = sc.nextLine();
byte[] bytes = data.getBytes();
client.send(new DatagramPacket(bytes,bytes.length,ip,thatport));
}
}
catch (Exception e){
System.out.println(e.fillInStackTrace());
}
}).start();
new Thread(()->{
try{
while(true){
server.receive(packetserver);
byte[] res = packetserver.getData();
String str = new String(res,0,packetserver.getLength());
System.out.println(" 对方:"+str);
}
}
catch (Exception e){
System.out.println(e.fillInStackTrace());
}
}).start();
}
}
基于tcp实现一个聊天软件
- 基于udp实现的聊天软件,两者是对等的关系,而基于tcp实现聊天软件,两个聊天用户不对等也可以实现。一个聊天用户作为tcp客户端,另一个聊天用户作为tcp服务端。
- 客户端向服务端发送请求建立连接后,就可以直接基于这个tcp连接实现两个用户的全双工通信了。只要当前tcp连接不断开,从头到尾只需要一个tcp连接即可。
- 同样每个聊天用户需要启动两个线程,发送端线程通过操作IO流向对方发送消息,接收端线程用于读取IO接收对方发过来的消息。
- 客户端程序代码
public class TCPClient {
public static void main(String[] args) throws IOException, InterruptedException {
Socket socket = new Socket("127.0.0.1",12345);
new Thread(()->{
try {
OutputStream out = socket.getOutputStream();
Scanner scanner = new Scanner(System.in);
while (true){
String s = scanner.nextLine();
out.write(s.getBytes());
}
}
catch (Exception e){
System.out.println(e.fillInStackTrace());
}
}).start();
new Thread(()->{
try{
InputStream in = socket.getInputStream();
byte[] by = new byte[1024];
while(true){
int len = in.read(by);
String res = new String(by,0,len);
System.out.println(" 对方:"+res);
}
}
catch (Exception e){
System.out.println(e.fillInStackTrace());
}
}).start();
}
}
public class TCPServer {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket ss = new ServerSocket(12345);
Socket socket = ss.accept();
System.out.println("检测到一个tcp请求");
new Thread(()->{
try {
OutputStream out = socket.getOutputStream();
Scanner scanner = new Scanner(System.in);
while (true){
String s = scanner.nextLine();
out.write(s.getBytes());
}
}
catch (Exception e){
System.out.println(e.fillInStackTrace());
}
}).start();
new Thread(()->{
try{
InputStream in = socket.getInputStream();
byte[] by = new byte[1024];
while(true){
int len = in.read(by);
String res = new String(by,0,len);
System.out.println(" 对方:"+res);
}
}
catch (Exception e){
System.out.println(e.fillInStackTrace());
}
}).start();
}
}