今天我们继续开始学习网络部分的知识。今天的部分是分为两个部分的一个数据传送,一个是MySQL的开头。废话不多说我们开始今天的内容。
我们其实知道在vs中是有粘包与分包的机制的,是为了内部的优化机制。那么我们应该如何去进行数据的传出与发送呢,我们用到的是这个类BitConverter.GetBytes这个类将所有的字符串都转换成四个字节,我们在tcp客户端创建一个类message,在其中写下如下的动心:
public class Message { public static byte[] GetBytes(string data) { //首先得到的是传来的字符串转换成字节数组 byte[] dataBytes = Encoding.UTF8.GetBytes(data); //接着计算长度 int dataLength = data.Length; //创建新数组 byte[] lengthByte = BitConverter.GetBytes(dataLength);//取到字节数组的长度 byte[] newBytes = lengthByte.Concat(dataBytes).ToArray(); return newBytes; } }
然后在在program中写
for (int i = 0; i < 100; i++) { clientSocket.Send(Message.GetBytes(i.ToString())); }
接着在tcp服务器端写message,如下
private byte[] data = new byte[1024]; private int startIndex = 0;//开始索引 public byte[] Data { get { return data; } } public int StartIndex { get { return startIndex; } } //还剩余什么 public int RemainSize { get { return data.Length - startIndex; } } public void AddCount(int count) { startIndex += count; } //读数据 public void ReadMessage() { while (true) { //如果数据长度不足4的话,会返回 if (startIndex <= 4) return; //得到传来数据的长度,因为toint32一次只会解析前四个字节,这样就知道了数组中还有几个数据 int count = BitConverter.ToInt32(data, 0); if ((startIndex - 4) >= count) { //索引减4就是真正的数据长度,就接受数据 string s = Encoding.UTF8.GetString(data, 4, count); Console.WriteLine("解析出来的数据为:" + s); //将数据进行更新 Array.Copy(data, count + 4, data, 0, startIndex - 4 - count); startIndex -= (count + 4); } else { break; } } }
在tcp的program中写下
static Message msg = new Message();
static void AcceptCallBack(IAsyncResult ar) { Socket serverSocket = ar.AsyncState as Socket; Socket clientSocket = serverSocket.EndAccept(ar); string msgStr = "Hello Client 您好..."; byte[] data = Encoding.UTF8.GetBytes(msgStr); clientSocket.Send(data); clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.RemainSize, SocketFlags.None, ReceiveCallBack, clientSocket); serverSocket.BeginAccept(AcceptCallBack, serverSocket); } static void ReceiveCallBack(IAsyncResult ar) { Socket clientSocket = null; try { clientSocket = ar.AsyncState as Socket; int count = clientSocket.EndReceive(ar); if (count == 0) { clientSocket.Close(); return; } msg.AddCount(count); msg.ReadMessage(); clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.RemainSize, SocketFlags.None, ReceiveCallBack, clientSocket); } catch (Exception e) { Console.WriteLine(e); if (clientSocket != null) { clientSocket.Close(); } } }
这样就写了。
接下来是数据部分MySQL的使用就不说了,
//创建连接 string connStr = "database=text002;data source=localhost;port=3306;user Id=root;password=root;"; MySqlConnection conn = new MySqlConnection(connStr); conn.Open(); #region 查询 //MySqlCommand cmd = new MySqlCommand("select *from user", conn); //MySqlDataReader reader = cmd.ExecuteReader(); //while (reader.Read()) //{ // string username = reader.GetString("username"); // string password = reader.GetString("password"); // Console.WriteLine(username + ":" + password); //} //reader.Close(); #endregion #region 插入 //string username = "你好"; string password = "世界"; //MySqlCommand cmd = new MySqlCommand("insert into user set username=@un,password=@pwd", conn); //cmd.Parameters.AddWithValue("un", username); //cmd.Parameters.AddWithValue("pwd", password); //cmd.ExecuteNonQuery(); #endregion conn.Close();//断开连接 Console.ReadKey();
就是这么多了。