如何通过Socket TCP发送并接收一个文件 如何通过Socket TCP发送并接收一个文件

\u5982\u4f55\u901a\u8fc7Socket TCP\u53d1\u9001\u5e76\u63a5\u6536\u4e00\u4e2a\u6587\u4ef6

\u4e00.\u5c0f\u7ed3
1.\u5927\u5305\u53d1\u5c0f\u5305\u6536\uff0c\u53ea\u53d1\u4e00\u6b21\u3002
2.\u53d1\u65f6\u628a\u6587\u4ef6\u6269\u5c55\u540d\uff0c\u6587\u4ef6\u957f\u5ea6\u4e5f\u968f\u540c\u4e00\u8d77\u53d1\u9001\uff0c\u65b9\u4fbf\u63a5\u6536\u7aef\u63a5\u6536\u65f6\u53e6\u5b58\u4e3a\u6b63\u786e\u7684\u6587\u4ef6\u7c7b\u578b\uff0c\u5e76\u5224\u65ad\u662f\u5426\u5df2\u7ecf\u63a5\u6536\u5b8c\u6bd5\u3002
\u5982\u679c\u4e0d\u4e00\u8d77\u53d1\u9001\uff0c\u5206\u4e3a\u6587\u4ef6\u6269\u5c55\u540d\uff0c\u6587\u4ef6\u957f\u5ea6\uff0c\u6587\u4ef6\u5185\u5bb9\uff0c\u53d1\u9001\u4e09\u6b21\uff0c\u5728\u63a5\u6536\u7aef\uff0c\u4e5f\u53ef\u80fd\u4f1a\u4e00\u8d77\u6536\u5230\uff0c\u53cd\u800c\u4e0d\u5229\u4e8e\u89e3\u6790\u3002

\u4e8c.\u5ba2\u6237\u53d1\u9001\u7aef\u4ee3\u7801
private void btnSend_Click(object sender, EventArgs e) { //\u7ec4\u5408\u51fa\u8fdc\u7a0b\u7ec8\u7ed3\u70b9 IPAddress ipAddress = IPAddress.Parse(this.txtIP3.Text); IPEndPoint hostEP = new IPEndPoint(ipAddress, Convert.ToInt32(this.txtPort3.Text)); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(hostEP); //1.\u53d1\u9001\u7528\u6237\u534f\u8bae string path1 = Environment.CurrentDirectory; //\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002 string doc = "YourSendFile.pdf"; string path = Path.Combine(path1, doc); FileStream fs = File.Open(path, FileMode.Open); //\u6587\u4ef6\u5185\u5bb9 byte[] bdata = new byte[fs.Length]; fs.Read(bdata, 0, bdata.Length); fs.Close(); //\u6587\u4ef6\u6269\u5c55\u540d\uff0c\u56fa\u5b9a3\u5b57\u8282 byte[] fileExtArray = Encoding.UTF8.GetBytes(string.Format("{0:D3}", currentDocExt)); //\u6587\u4ef6\u957f\u5ea6, \u56fa\u5b9a\u4e3a20\u5b57\u8282\uff0c\u524d\u9762\u4f1a\u81ea\u52a8\u8865\u96f6 byte[] fileLengthArray = Encoding.UTF8.GetBytes(bdata.Length.ToString("D20")); //\u5408\u5e76byte\u6570\u7ec4 byte[] fileArray = CombomBinaryArray(fileExtArray, fileLengthArray); //\u5408\u5e76byte\u6570\u7ec4 byte[] bdata1 = CombomBinaryArray(fileArray, bdata); //\u53d1\u6587\u4ef6\u957f\u5ea6+\u6587\u4ef6\u5185\u5bb9 socket.Send(bdata1, bdata1.Length, 0); //2.\u63a5\u6536 //\u58f0\u660e\u63a5\u6536\u8fd4\u56de\u5185\u5bb9\u7684\u5b57\u7b26\u4e32 string recvStr = ""; //\u58f0\u660e\u5b57\u8282\u6570\u7ec4\uff0c\u4e00\u6b21\u63a5\u6536\u6570\u636e\u7684\u957f\u5ea6\u4e3a 1024 \u5b57\u8282 byte[] recvBytes = new byte[1024]; //\u8fd4\u56de\u5b9e\u9645\u63a5\u6536\u5185\u5bb9\u7684\u5b57\u8282\u6570 int bytes = 0; //\u5faa\u73af\u8bfb\u53d6\uff0c\u76f4\u5230\u63a5\u6536\u5b8c\u6240\u6709\u6570\u636e while (true) { bytes = socket.Receive(recvBytes, recvBytes.Length, 0); //\u8bfb\u53d6\u5b8c\u6210\u540e\u9000\u51fa\u5faa\u73af if (bytes <= 0) break; //\u5c06\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32 recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes); } //\u7981\u7528 Socket socket.Shutdown(SocketShutdown.Both); //\u5173\u95ed Socket socket.Close(); //... do some busness logic ... } catch (Exception e1) { throw e1; } }\u4e09.\u670d\u52a1\u63a5\u6536\u7aef\u4ee3\u7801
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;using System.Xml;using System.Configuration;using System.Windows.Forms;using System.IO;namespace ConsoleAppServer{ class Program { [STAThread] static void Main(string[] args) { bool Done = false; IPAddress ipAddress = IPAddress.Parse(ConfigurationSettings.AppSettings["IP"].ToString()); IPEndPoint hostEP = new IPEndPoint(ipAddress, Convert.ToInt32(ConfigurationSettings.AppSettings["Port"])); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(hostEP); socket.Listen(3); while (!Done) { Console.WriteLine("waiting for client"); Socket client = socket.Accept(); Console.WriteLine("connected client"); //1.\u63a5\u6536 //\u58f0\u660e\u5b57\u8282\u6570\u7ec4\uff0c\u4e00\u6b21\u63a5\u6536\u6570\u636e\u7684\u957f\u5ea6\u4e3a 1024 \u5b57\u8282 byte[] recvBytes = new byte[1024]; //\u8fd4\u56de\u5b9e\u9645\u63a5\u6536\u5185\u5bb9\u7684\u5b57\u8282\u6570 int bytes = 0; int FileLength = 0; // 900866; int ReceivedLength = 0; //1.0 \u63a5\u6536\u6587\u4ef6\u6269\u5c55\u540d bytes = client.Receive(recvBytes, 3, 0); string fileExt = Encoding.UTF8.GetString(recvBytes, 0, bytes); string vFilePath = Environment.CurrentDirectory; string vFileName = vFilePath + "\\Tmp" + Guid.NewGuid().ToString() + "." + fileExt; //\u521b\u5efa\u6587\u4ef6\u6d41\uff0c\u7136\u540e\u8ba9\u6587\u4ef6\u6d41\u6765\u6839\u636e\u8def\u5f84\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6 FileStream fs = new FileStream(vFileName, FileMode.Create); //1.1 \u63a5\u6536\u6587\u4ef6\u957f\u5ea6 bytes = client.Receive(recvBytes, 20, 0); //\u5c06\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32 string fileLength = Encoding.UTF8.GetString(recvBytes, 0, bytes); FileLength = Convert.ToInt32(fileLength); //1.2\u63a5\u6536\u6587\u4ef6\u5185\u5bb9 while (ReceivedLength < FileLength) { bytes = client.Receive(recvBytes, recvBytes.Length, 0); ReceivedLength += bytes; fs.Write(recvBytes, 0, bytes); } fs.Flush(); fs.Close(); //... do some business logic ... string returnData = SomeBusinessLogic(); //2.\u53d1\u9001 byte[] bdata = Encoding.UTF8.GetBytes(returnData); client.Send(bdata, bdata.Length, 0); client.Close(); } socket.Shutdown(SocketShutdown.Both); socket.Close(); } }}

\u4f60\u597d\uff0c
\u4e3b\u8981\u662f\u4f60\u901a\u8fc7socket api\u5c01\u88c5\u8981\u53d1\u9001\u7684\u6570\u636e\uff0c\u5185\u90e8\u4f1a\u81ea\u52a8\u5c01\u88c5\u6210\u6570\u636e\u6d41\u8fdb\u884c\u4f20\u8f93\u3002 1\uff0c\u4ec0\u4e48\u662fSocket \u7f51\u7edc\u4e0a\u7684\u4e24\u4e2a\u7a0b\u5e8f\u901a\u8fc7\u4e00\u4e2a\u53cc\u5411\u7684\u901a\u8baf\u8fde\u63a5\u5b9e\u73b0\u6570\u636e\u7684\u4ea4\u6362\uff0c\u8fd9\u4e2a\u53cc\u5411\u94fe\u8def\u7684\u4e00\u7aef\u79f0\u4e3a\u4e00\u4e2aSocket\u3002Socket\u901a\u5e38\u7528\u6765\u5b9e\u73b0\u5ba2\u6237\u65b9\u548c\u670d\u52a1\u65b9\u7684\u8fde\u63a5\u3002

主要是你通过socket api封装要发送的数据,内部会自动封装成数据流进行传输。
1,什么是Socket
网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个Socket。Socket通常用来实现客户方和服务方的连接。Socket是TCP/IP协议的一个十分流行的编程界面,一个Socket由一个IP地址和一个端口号唯一确定。
但是,Socket所支持的协议种类也不光TCP/IP一种,因此两者之间是没有必然联系的。在Java环境下,Socket编程主要是指基于TCP/IP协议的网络编程。
2,Socket通讯的过程
Server端Listen(监听)某个端口是否有连接请求,Client端向Server 端发出Connect(连接)请求,Server端向Client端发回Accept(接受)消息。一个连接就建立起来了。Server端和Client 端都可以通过Send,Write等方法与对方通信。
对于一个功能齐全的Socket,都要包含以下基本结构,其工作过程包含以下四个基本的步骤:
  (1) 创建Socket;
  (2) 打开连接到Socket的输入/出流;
  (3) 按照一定的协议对Socket进行读/写操作;
  (4) 关闭Socket.(在实际应用中,并未使用到显示的close,虽然很多文章都推荐如此,不过在我的程序中,可能因为程序本身比较简单,要求不高,所以并未造成什么影响。)
3,创建Socket
创建Socket
java在包java.net中提供了两个类Socket和ServerSocket,分别用来表示双向连接的客户端和服务端。这是两个封装得非常好的类,使用很方便。其构造方法如下:
Socket(InetAddress address, int port);
Socket(InetAddress address, int port, boolean stream);
Socket(String host, int prot);
Socket(String host, int prot, boolean stream);
Socket(SocketImpl impl)
Socket(String host, int port, InetAddress localAddr, int localPort)
Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
ServerSocket(int port);
ServerSocket(int port, int backlog);
ServerSocket(int port, int backlog, InetAddress bindAddr)

Socket client = new Socket("127.0.01.", 80);
ServerSocket server = new ServerSocket(80);

  在创建socket时如果发生错误,将产生IOException,在程序中必须对之作出处理。所以在创建Socket或ServerSocket是必须捕获或抛出例外。

扩展阅读:java socket tcp ... modbus tcp ... socketexception ... linux socket ... socket5 tcp ... socket并发测试工具 ... shadowsocket ios ... socket fm2支持的cpu ... tcp socket通信的基本过程 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网