重点:面试题
TCP和UDP的区别?
TCP 面向连接的 、可靠的、安全的传输协议
UDP 非面向连接 不安全 不可靠的传输协议
TCP协议三次握手 四次挥手
TCP 协议三次握手 四次挥手:请参考牛客网面试宝典中的面试题–>牛客网面试宝典
1 IP地址的组成
IP:192.168.10.2,网络地址就是:192.1.168.10.0,为C类
DNS: Domain Name System 域名解析系统
2 网络通信协议
3. Socket(套节字)
4 .基于TCP协议的Socket编程
TCP协议:传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议
代码示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
*客户端
*/
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("客户端启动了。。。");
// 发送
Socket socket = new Socket("127.0.0.1",8899);
OutputStream os = socket.getOutputStream();
String str = "你好,服务器,吃饭了吗?";
byte[] bytes = str.getBytes();
os.write(bytes);
socket.shutdownOutput();
// 接收
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = br.readLine();
System.out.println("客户端接收到的信息:" + readLine);
socket.shutdownInput();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException, ClassNotFoundException {
System.out.println("服务器启动了。。。。。。。。。。。。");
// 接收
ServerSocket server = new ServerSocket(8899);
Socket socket = server.accept();
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = br.readLine();
System.out.println("服务器端接收到的信息:" + readLine);
socket.shutdownInput();
// 发送
OutputStream os = socket.getOutputStream();
os.write("吃过了".getBytes());
socket.shutdownOutput();
}
}
5、UDP 用户数据报(报文)协议
> UDP 非面向连接 不安全 不可靠的传输协议
代码示例:
/**
* UDP 用户数据报(报文)协议
* UDP 非面向连接 不安全 不可靠的传输协议
* @author WHD
*客户端
*/
public class Client {
public static void main(String[] args) {
System.out.println("客户端开始连接………………");
String message = "hello,服务器";
byte [] data = message.getBytes();
InetAddress localHost;
try {
// 发送
localHost = InetAddress.getLocalHost();
DatagramPacket sendDP = new DatagramPacket(data, data.length, localHost, 8899);
DatagramSocket socket = new DatagramSocket();
socket.send(sendDP);
// 接收
byte [] bag = new byte[1024];
DatagramPacket receiveDP = new DatagramPacket(bag, bag.length);
socket.receive(receiveDP); // 接收服务器发送的报文 此时数据已经存在receiveDP对象中
System.out.println(new String(receiveDP.getData(),0,receiveDP.getData().length));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 服务器
* @author WHD
*
*/
public class Server {
public static void main(String[] args) {
System.out.println("服务器启动………………");
try {
// 接收
DatagramSocket socket = new DatagramSocket(8899);
byte [] bag = new byte[1024];
DatagramPacket receiveDP = new DatagramPacket(bag, bag.length);
socket.receive(receiveDP);
System.out.println(new String(receiveDP.getData(),0,receiveDP.getData().length));
// 发送
String message = "你也hello啊";
byte [] sendBag = message.getBytes();
SocketAddress address = receiveDP.getSocketAddress();
DatagramPacket sendDP = new DatagramPacket(sendBag, sendBag.length, address);
socket.send(sendDP);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}