TCP
三次握手四次挥手(三次干架,四次分手)
三次握手(三次干架)
A:你瞅啥
B:瞅你咋地
A:干他
四次挥手(四次分手)
A:我要走了
B:你真的要走了吗?
B:你真的真的要走了吗?
A:我真的要走了
package TCP;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//服务器
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(6000);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = null;
while ((str = br.readLine()) != null){
System.out.println("服务器接收数据为:" + str);
}
//接收状态转为发送状态
s.shutdownInput();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("我是小兰");
//发送之后必须清空缓存区
bw.flush();
//关闭资源
br.close();
bw.close();
s.close();
}
}
package TCP;
import java.io.*;
import java.net.Socket;
//客户端
public class TCPClient{
public static void main(String[] args) throws IOException {
Socket s = new Socket("127.0.0.1" ,6000);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("我是小明");
//发送之后必须清空缓存区
bw.flush();
//发送状态转为接收状态
s.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = null;
while ((str = br.readLine()) != null){
System.out.println("客户端接收数据为:" + str);
}
//关闭资源
bw.close();
br.close();
s.close();
}
}
UDP
package UDP;
import java.io.IOException;
import java.net.*;
//服务端
public class UdpClient {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9000);
byte[] buffer = new byte[1024];
DatagramPacket packetReceive = new DatagramPacket(buffer , 0 , buffer.length);
socket.receive(packetReceive);
System.out.println("服务端接收到的数据:" + new String(buffer , 0 , packetReceive.getLength()));
//接收数据
String str = "你好 生活";
InetAddress address = InetAddress.getByName("localhost");
int port = packetReceive.getPort();;
DatagramPacket packetSend = new DatagramPacket(str.getBytes() , 0 ,str.getBytes().length , address , port);
socket.send(packetSend);
//关闭UDP
socket.close();
}
}
package UDP;
import java.io.IOException;
import java.net.*;
//客户端
public class UdpClient2 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9001);
String str = "你好 世界";
InetAddress address = InetAddress.getByName("localhost");
int port = 9000;
DatagramPacket packetSend = new DatagramPacket(str.getBytes() , 0 ,str.getBytes().length , address , port);
socket.send(packetSend);
//发送数据
byte[] buffer = new byte[1024];
DatagramPacket packetReceive = new DatagramPacket(buffer , 0 , buffer.length);
socket.receive(packetReceive);
System.out.println("客户端接收到的数据:" + new String(buffer , 0 , packetReceive.getLength()));
//关闭UDP
socket.close();
}
}
UDP聊天
package UDP聊天;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ThreadReceive extends Thread{
DatagramSocket socket;
private int port;
private String str;
public ThreadReceive(int port, String str) {
this.port = port;
this.str = str;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] buffer = new byte[1024];
DatagramPacket packetReceive = new DatagramPacket(buffer , 0 , buffer.length);
socket.receive(packetReceive);
String receiveData = new String(buffer , 0 , packetReceive.getLength());
System.out.println(str + ":" + receiveData);
if (receiveData.equals("再见"))
{
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package UDP聊天;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class ThreadSend extends Thread{
DatagramSocket socket = null;
BufferedReader reader = null;
private final int fromPort;
private final String toIP;
private final int toPort;
public ThreadSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String data = reader.readLine();
DatagramPacket packetSend = new DatagramPacket(data.getBytes() , 0 ,data.getBytes().length , InetAddress.getByName(toIP) , this.toPort);
socket.send(packetSend);
if (data.equals("再见"))
{
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package UDP聊天;
public class Udp_XiaoMing {
public static void main(String[] args) {
new Thread(new ThreadSend(7777 , "localhost" , 9999)).start();
new Thread(new ThreadReceive(8888 , "小明")).start();
}
}
package UDP聊天;
public class Udp_XiaoHong {
public static void main(String[] args) {
new Thread(new ThreadSend(5555 , "localhost" , 8888)).start();
new Thread(new ThreadReceive(9999 , "小红")).start();
}
}