网络编程(TCP协议)
2022/4/10 17:13:37
本文主要是介绍网络编程(TCP协议),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
TCP协议发送数据
A:创建Socket对象---利用Socket对象
B:获取输出流,写数据
C:释放资源
package Day26; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; /* * TCP协议发送数据: * A:创建发送端的Socket对象 * 这一步如果成功,就说明连接已经建立成功了。 * B:获取输出流,写数据 * C:释放资源 * * 连接被拒绝。TCP协议一定要先看服务器。 * java.net.ConnectException: Connection refused: connect */ public class TcpDemo { public static void main(String[] args) throws IOException { //创建Socket对象 // Socket(InetAddress address, int port) //Socket s = new Socket(InetAddress.getByName("192.168.12.92"), 8888); //public Socket(String host,int port) // ----创建一个流套接字并将其连接到指定主机上的指定端口号。 //指定IP地址和连接端口 Socket sk = new Socket("192.168.1.8",12345); //获取输出流,写数据 //OutputStream getOutputStream()------返回此套接字的输出流。 //此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。 //获取输出流 OutputStream os = sk.getOutputStream(); //写数据-----将字符数据转换为字节数组,写入 os.write("你好中国".getBytes()); //释放资源 sk.close(); } }
TCP协议接受数据
A:创建Socket接受对象
B:监听客户端连接--返回一个对应的Socket对象
C:获取输入流,读取数据并将其显示在控制台
D:释放资源
package Day26; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; // * TCP协议接收数据: // * A:创建接收端的Socket对象 // * B:监听客户端连接。返回一个对应的Socket对象 // * C:获取输入流,读取数据显示在控制台 // * D:释放资源 // */ public class ServersDemo { public static void main(String[] args) throws IOException { //创建Socket接收对象---并给出连接的端口 //public ServerSocket(int port)-------创建绑定到特定端口的服务器套接字 ServerSocket ss = new ServerSocket(12345); //监听客户端连接,返回一个对应的Socket对象 //public Socket accept()----侦听并接受到此套接字的连接---返回新的套接字 Socket st = ss.accept(); //获取输入流和读出数据显示到控制台 //获取输入流对象 //public InputStream getInputStream()---从此套接字读取字节的输入流。 InputStream is = st.getInputStream(); //读出数据 byte [] bt = new byte[1024]; //public int read(byte[] b) //从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中 int len = is.read(bt); //public String(byte[] bytes,int offset,int length)---将字节数组转换为字符串 String aa = new String(bt,0,len); //获取发送端的ip地址 String ip = st.getInetAddress().getHostAddress(); //输出到控制台 System.out.println(ip+"--"+aa); //释放资源 st.close(); //ss.close();---服务器不应该关闭 } }
服务器给客户端一个反馈的案例
Tcp客户端
package cn.itcast_07; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class ClientDemo { public static void main(String[] args) throws IOException { // 创建客户端Socket对象 Socket s = new Socket("192.168.12.92", 9999); // 获取输出流 OutputStream os = s.getOutputStream(); os.write("今天天气很好,适合睡觉".getBytes()); // 获取输入流 InputStream is = s.getInputStream(); byte[] bys = new byte[1024]; int len = is.read(bys);// 阻塞 String client = new String(bys, 0, len); System.out.println("client:" + client); // 释放资源 s.close(); } }
TCP协议服务器
package cn.itcast_07; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 创建服务器Socket对象 ServerSocket ss = new ServerSocket(9999); // 监听客户端的连接 Socket s = ss.accept(); // 阻塞 // 获取输入流 InputStream is = s.getInputStream(); byte[] bys = new byte[1024]; int len = is.read(bys); // 阻塞 String server = new String(bys, 0, len); System.out.println("server:" + server); // 获取输出流 OutputStream os = s.getOutputStream(); os.write("数据已经收到".getBytes()); // 释放资源 s.close(); // ss.close(); } }
* 客户端键盘录入,服务器输出到控制台
客户端:
package cn.itcast_08; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /* * 客户端键盘录入,服务器输出到控制台 */ public class ClientDemo { public static void main(String[] args) throws IOException { // 创建客户端Socket对象 Socket s = new Socket("192.168.12.92", 22222); // 键盘录入数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 把通道内的流给包装一下 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { // 键盘录入数据要自定义结束标记 if ("886".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } // 释放资源 // bw.close(); // br.close(); s.close(); } }
服务器:
package cn.itcast_08; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 创建服务器Socket对象 ServerSocket ss = new ServerSocket(22222); // 监听客户端连接 Socket s = ss.accept(); // 包装通道内容的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } // br.close(); s.close(); // ss.close(); } }
客户端键盘录入,服务器输出文本文件
package cn.itcast_09; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /* * 客户端键盘录入,服务器输出文本文件 */ public class ClientDemo { public static void main(String[] args) throws IOException { // 创建客户端Socket对象 Socket s = new Socket("192.168.12.92", 23456); // 封装键盘录入 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 封装通道内的数据 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { if ("over".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } // bw.close(); // br.close(); s.close(); } }
接受数据
package cn.itcast_09; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 创建服务器Socket对象 ServerSocket ss = new ServerSocket(23456); // 监听客户端连接 Socket s = ss.accept(); // 封装通道内的数据 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 封装文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); // br.close(); s.close(); // ss.close(); } }
客户端文本文件,服务器输出到控制台
客户端
package cn.itcast_10; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; /* * 客户端文本文件,服务器输出到控制台 */ public class ClientDemo { public static void main(String[] args) throws IOException { // 创建Socket对象 Socket s = new Socket("192.168.12.92", 34567); // 封装文本文件 BufferedReader br = new BufferedReader(new FileReader( "InetAddressDemo.java")); // 封装通道内的流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } br.close(); s.close(); } }
服务器
package cn.itcast_10; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException { // 创建服务器Socket对象 ServerSocket ss = new ServerSocket(34567); // 监听客户端连接 Socket s = ss.accept(); // 封装通道内的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } s.close(); } }
TCP协议上传文本文件----字符流
客户端
package cn.itcast_11; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; public class UploadClient { public static void main(String[] args) throws IOException { // 创建客户端Socket对象 Socket s = new Socket("192.168.12.92", 11111); // 封装文本文件 BufferedReader br = new BufferedReader(new FileReader( "InetAddressDemo.java")); // 封装通道内流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } // 释放资源 br.close(); s.close(); } }
服务器端
package cn.itcast_11; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { // 创建服务器端的Socket对象 ServerSocket ss = new ServerSocket(11111); // 监听客户端连接 Socket s = ss.accept(); // 封装通道内的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 封装文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); s.close(); } }
TCP协议上传文本文件并给出反馈信息----字符流
客户端
package cn.itcast_12; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /* * 按照我们正常的思路加入反馈信息,结果却没反应。为什么呢? * 读取文本文件是可以以null作为结束信息的,但是呢,通道内是不能这样结束信息的。 * 所以,服务器根本就不知道你结束了。而你还想服务器给你反馈。所以,就相互等待了。 * * 如何解决呢? * A:在多写一条数据,告诉服务器,读取到这条数据说明我就结束,你也结束吧。 * 这样做可以解决问题,但是不好。 * B:Socket对象提供了一种解决方案 * public void shutdownOutput() */ public class UploadClient { public static void main(String[] args) throws IOException { // 创建客户端Socket对象 Socket s = new Socket("192.168.12.92", 11111); // 封装文本文件 BufferedReader br = new BufferedReader(new FileReader( "InetAddressDemo.java")); // 封装通道内流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { // 阻塞 bw.write(line); bw.newLine(); bw.flush(); } //自定义一个结束标记 // bw.write("over"); // bw.newLine(); // bw.flush(); //Socket提供了一个终止,它会通知服务器你别等了,我没有数据过来了 s.shutdownOutput(); // 接收反馈 BufferedReader brClient = new BufferedReader(new InputStreamReader( s.getInputStream())); String client = brClient.readLine(); // 阻塞 System.out.println(client); // 释放资源 br.close(); s.close(); } }
服务器
package cn.itcast_12; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { // 创建服务器端的Socket对象 ServerSocket ss = new ServerSocket(11111); // 监听客户端连接 Socket s = ss.accept();// 阻塞 // 封装通道内的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 封装文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java")); String line = null; while ((line = br.readLine()) != null) { // 阻塞 // if("over".equals(line)){ // break; // } bw.write(line); bw.newLine(); bw.flush(); } // 给出反馈 BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); bwServer.write("文件上传成功"); bwServer.newLine(); bwServer.flush(); // 释放资源 bw.close(); s.close(); } }
TCP协议上传图片 ----采用字节流
客户端
A: OutputStream------
public void flush() throws IOException
- 刷新此输出流并强制写出所有缓冲的输出字节
package cn.itcast_13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class UploadClient { public static void main(String[] args) throws IOException { // 创建客户端Socket对象 Socket s = new Socket("192.168.12.92", 19191); // 封装图片文件 BufferedInputStream bis = new BufferedInputStream(new FileInputStream( "林青霞.jpg")); // 封装通道内的流 BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream()); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); bos.flush(); } s.shutdownOutput(); // 读取反馈 InputStream is = s.getInputStream(); byte[] bys2 = new byte[1024]; int len2 = is.read(bys2); String client = new String(bys2, 0, len2); System.out.println(client); // 释放资源 bis.close(); s.close(); } }
服务器端
A: OutputStream------
public void flush() throws IOException
- 刷新此输出流并强制写出所有缓冲的输出字节
package cn.itcast_13; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { // 创建服务器Socket对象 ServerSocket ss = new ServerSocket(19191); // 监听客户端连接 Socket s = ss.accept(); // 封装通道内流 BufferedInputStream bis = new BufferedInputStream(s.getInputStream()); // 封装图片文件 BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("mn.jpg")); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); bos.flush(); } // 给一个反馈 OutputStream os = s.getOutputStream(); os.write("图片上传成功".getBytes()); bos.close(); s.close(); } }
TCP多线程改进多个客户端上传文件
客户端
package cn.itcast_15; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class UploadClient { public static void main(String[] args) throws IOException { // 创建客户端Socket对象 Socket s = new Socket("192.168.12.92", 11111); // 封装文本文件 // BufferedReader br = new BufferedReader(new FileReader( // "InetAddressDemo.java")); BufferedReader br = new BufferedReader(new FileReader( "ReceiveDemo.java")); // 封装通道内流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { // 阻塞 bw.write(line); bw.newLine(); bw.flush(); } // Socket提供了一个终止,它会通知服务器你别等了,我没有数据过来了 s.shutdownOutput(); // 接收反馈 BufferedReader brClient = new BufferedReader(new InputStreamReader( s.getInputStream())); String client = brClient.readLine(); // 阻塞 System.out.println(client); // 释放资源 br.close(); s.close(); } }
多线程
package cn.itcast_15; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class UserThread implements Runnable { private Socket s; public UserThread(Socket s) { this.s = s; } @Override public void run() { try { // 封装通道内的流 BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); // 封装文本文件 // BufferedWriter bw = new BufferedWriter(new // FileWriter("Copy.java")); // 为了防止名称冲突 String newName = System.currentTimeMillis() + ".java"; BufferedWriter bw = new BufferedWriter(new FileWriter(newName)); String line = null; while ((line = br.readLine()) != null) { // 阻塞 bw.write(line); bw.newLine(); bw.flush(); } // 给出反馈 BufferedWriter bwServer = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())); bwServer.write("文件上传成功"); bwServer.newLine(); bwServer.flush(); // 释放资源 bw.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } }
服务器
package cn.itcast_15; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { // 创建服务器Socket对象 ServerSocket ss = new ServerSocket(11111); while (true) { Socket s = ss.accept(); new Thread(new UserThread(s)).start(); } } }
这篇关于网络编程(TCP协议)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-27Rocket消息队列资料:新手入门指南
- 2024-11-27rocket消息队资料详解与入门指南
- 2024-11-27RocketMQ底层原理资料详解入门教程
- 2024-11-27RocketMQ项目开发资料:新手入门教程
- 2024-11-27RocketMQ项目开发资料详解
- 2024-11-27RocketMQ消息中间件资料入门教程
- 2024-11-27初学者指南:深入了解RocketMQ源码资料
- 2024-11-27Rocket消息队列学习入门指南
- 2024-11-26Rocket消息中间件教程:新手入门详解
- 2024-11-26RocketMQ项目开发教程:新手入门指南