一、TCP/IP协议
1、TCP/IP协议包括TCP、IP和UDP等
2、域名通过dns服务器转换为IP地址
3、局域网可以通过IP或者主机地址寻找到相应的主机
4、TCP是可靠的连接,效率低,且连接是一对一,TCP;而UDP是不可靠连接,容易丢失数据,但是效率高,且连接是一对多。
二、端口号
1、端口号唯一确定服务器中的某个应用
2、一台服务器不允许存在两个相同端口号的应用
3、IP:port唯一确定服务器应用程序
4、https的默认端口为443,http为80,https是http的加密和解密的升级版
三、Socket
1、客户端应用程序与Port间使用Socket连接,打个比方,客户端应用程序比作家用电器,Port的IO流比作电流,那么Socket就是插座,家用电器只有将电线插到插座上才能通电
2、服务端与Port间使用ServerSocket连接
3、只要获得了Socket实例,就有了IO流
四、InetAddress类
public class TestTcp {
public static void main(String[] args) {
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost.getHostName());//获取主机名
System.out.println(localHost.getHostAddress());//获取主机地址
localHost = InetAddress.getByName("baidu.com");
System.out.println(localHost.getHostName());
System.out.println(localHost.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
五、简易聊天室
本聊天室包括一个服务端和n个客户端,客户端向服务端发送消息,服务端再将该消息群发给除了它们本身的其它客户端。
想要发消息,必须要拥有IO流,想要IO流就必须拥有Socket,另外有谁的Socket就可以向谁发消息。
聊天室代码如下:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TalkTool {
}
//服务端
class ServerT{
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9014);//建立服务端套接字与端口关联
System.out.println("服务端启动成功,等待客户端连接");
List<Socket> socketList = new ArrayList<>();
while (true) {
//主线程只负责客户端连接
Socket socket = serverSocket.accept();//等待客户端连接(阻塞状态)
System.out.println("有客户端连入");
socketList.add(socket);
new Thread(new ForwardT(socket,socketList)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端
class ClientT{
public static void main(String[] args) {
try {
Socket socket = new Socket("192.168.226.1",9014);//建立连接,IP地址根据实际情况设置
System.out.println("客户端与服务端建立了连接");
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream),true);
Scanner scanner = new Scanner(System.in);
//独立线程负责接收消息
new Thread(new ReceveT(socket)).start();
while (true) {
printWriter.println(scanner.next());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务端转发线程
class ForwardT implements Runnable{
private Socket socket;
private List<Socket> socketList;
public ForwardT(Socket socket,List<Socket>socketList){
this.socket=socket;
this.socketList=socketList;
}
@Override
public void run() {
try {
while (true) {
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String msg = bufferedReader.readLine();
System.out.println(msg);
for (Socket sk:socketList
) {
if (sk!=socket){//把自己排除掉
//发送给其它客户端,必须要有其它客户端的Socket
OutputStream outputStream = sk.getOutputStream();
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream),true);
printWriter.println(socket+"说:"+msg);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端接收线程
class ReceveT implements Runnable{
private Socket socket;
public ReceveT(Socket socket){
this.socket=socket;
}
@Override
public void run() {
try {
while (true) {
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println("服务端返回"+bufferedReader.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}