介绍
a.网络通信协议(基于TCP协议的通信) b.IP地址: 网上计算机的唯一标识(相当于身份证号) c.端口号: 计算机上软件的标识 端口号一共有:0-65535这个多个,我们写的软件建议使用1024以上的,因为1024一下的被牛逼知名软件服务占用的
使用的类
ServerSocket 服务器端
构造方法:ServerSocket(端口号); 接收端使用
方法:accept(); 返回一个 Socket对象
getInetAddress();返回发送端的地址 InetAddress对象
Socket 客户端 发送端
构造方法:
Socket(IP地址,端口号);
Socket(主机名,端口号);
方法:
public OutputStream getOutputStream();//获取连接通道中的输出流
public InputStream getInputStream();//获取连接通道中的输入流
public void close();//关闭客户端,释放资源,断开连接
public void shutdownOutput();//关闭连接通道中的输出流
public void shutdownInput();//关闭连接通道中的输入流
服务器端创建:
public class SockteTcp { public static void main(String[] args) throws Exception { Socket socket = new Socket("127.0.0.1", 60); OutputStream outputStream = socket.getOutputStream(); outputStream.write("服务器你好,我给你发了个1.txt".getBytes()); FileInputStream fileInputStream = new FileInputStream("111.txt"); int len = 0; while ((len = fileInputStream.read()) != -1) { outputStream.write(len); } socket.shutdownOutput(); InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; len = inputStream.read(bytes); System.out.println(new String(bytes, 0, len)); } }
创建客户端:
public class ServerSocketTcp { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(60); Socket accept = serverSocket.accept(); InputStream inputStream = accept.getInputStream(); byte[] bytes = new byte[1024]; int len = inputStream.read(bytes); System.out.println(new String(bytes, 0, len)); FileOutputStream fileOutputStream = new FileOutputStream("server.txt"); while ((len = inputStream.read()) != -1) { fileOutputStream.write(len); } //返回给客户端消息 OutputStream outputStream = accept.getOutputStream(); outputStream.write("恭喜拷贝成功".getBytes()); } }
其他案例: 服务器使用多线程实现多人传送文件 将服务器实现代码实现Runnable接口 重写run方法,封装到run方法中 //服务器
public class ServerSocketTcp { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(60); while (true) { Socket accept = serverSocket.accept(); InputStream inputStream = accept.getInputStream(); new Thread(() -> { try { byte[] bytes = new byte[1024]; int len = inputStream.read(bytes); System.out.println(new String(bytes, 0, len)); FileOutputStream fileOutputStream = new FileOutputStream("server" + System.currentTimeMillis() + ".txt"); while ((len = inputStream.read()) != -1) { fileOutputStream.write(len); } OutputStream outputStream = accept.getOutputStream(); outputStream.write("恭喜拷贝成功".getBytes()); outputStream.close(); fileOutputStream.close(); inputStream.close(); accept.close(); } catch (Exception e) { e.printStackTrace(); } }).start(); } } }
//客户端
public class SockteTcp { public static void main(String[] args) throws Exception { Socket socket = new Socket("127.0.0.1", 60); OutputStream outputStream = socket.getOutputStream(); outputStream.write("服务器你好,我给你发了个1.txt".getBytes()); FileInputStream fileInputStream = new FileInputStream("111.txt"); int len = 0; while ((len = fileInputStream.read()) != -1) { outputStream.write(len); } socket.shutdownOutput(); InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; len = inputStream.read(bytes); System.out.println(new String(bytes, 0, len)); inputStream.close(); fileInputStream.close(); outputStream.close(); socket.close(); } }