學習視頻:https://www.bilibili.com/video/BV1FJ411v7hv?p=15 socket通信傳輸的位元組是什麼內容,客戶端和服務端要知道這個位元組是文件,是字元串還是其他?應該設計一個協定。例如在內容位元組前面插入一個位元組表示內容形式,0代表字元串,1代表文件...... ...
學習視頻:https://www.bilibili.com/video/BV1FJ411v7hv?p=15
socket通信傳輸的位元組是什麼內容,客戶端和服務端要知道這個位元組是文件,是字元串還是其他?應該設計一個協定。例如在內容位元組前面插入一個位元組表示內容形式,0代表字元串,1代表文件......
private void button1_Click(object sender, EventArgs e) { Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse("192.169.11.87"); IPEndPoint endPoint = new IPEndPoint(ip, 5000); socket.Connect(endPoint); string msg = "消息來了"; byte[] buffer = Encoding.UTF8.GetBytes(msg); //如何在buffer前面加一個位元組呢,可以使用集合 List<byte> bufferList = new List<byte>(); bufferList.Add(0); bufferList.AddRange(buffer); byte[] newBuffer = bufferList.ToArray(); socket.Send(newBuffer); }View Code
private void button1_Click(object sender, EventArgs e) { Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse("192.169.11.87"); IPEndPoint endPoint = new IPEndPoint(ip, 5000); socket.Connect(endPoint); string path = string.Empty; using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 5]; int r = fsRead.Read(buffer, 0, buffer.Length); List<byte> newBuffer = new List<byte>(); newBuffer.Add(1); newBuffer.AddRange(buffer); // 是r+1 socket.Send(newBuffer.ToArray(), 0, r+1, SocketFlags.None); } }