五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

.Net core 使用socket 傳輸數(shù)據(jù)

2022-11-10 11:30 作者:限量版范兒  | 我要投稿

模擬服務(wù)端

private string TcpServiceControl(string value) ? ? ? ?{ ? ? ? ? ? ?// 1.按照模擬工具上看 先new一個tcp服務(wù) ? ? ? ? ? ?Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ? ? ? ? ? ?//申請端口,綁定ip ? ? ? ? ? ?IPAddress iPAddress = IPAddress.Parse(_appConfig.ApplictionSettings.ControlTcpIP); ? ? ? ? ? ?IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, Convert.ToInt32(_appConfig.ApplictionSettings.ControlTcpPort)); ? ? ? ? ? ?//綁定ip和端口 ? ? ? ? ? ?serverSocket.Bind(iPEndPoint); ? ? ? ? ? ?//開始監(jiān)聽端口 設(shè)置50防止服務(wù)器崩潰,設(shè)置0表示不限制數(shù)量 ? ? ? ? ? ?serverSocket.Listen(0);//傳遞掛起的連接隊(duì)列的最大長度 ? ? ? ? ? ?//接收客戶端信息 程序會暫停 直到由一個客戶端連接過來才會繼續(xù)向下運(yùn)行 ? ? ? ? ? ?Socket clientSocket = serverSocket.Accept();//接收一個客戶端連接,返回一個socket用來和客戶端通訊 ? ? ? ? ? ?//向客戶端發(fā)送一條數(shù)據(jù) ? ? ? ? ? ?byte[] data = System.Text.Encoding.UTF8.GetBytes(value); ? ? ? ? ? ?clientSocket.Send(data); ? ? ? ? ? ?//接收客戶端一條消息 ? ? ? ? ? ?byte[] databuffer = new byte[1024];//接收時先定義一個數(shù)組 ? ? ? ? ? ?int count = clientSocket.Receive(databuffer);//知道數(shù)組中前count個是接收到的數(shù)據(jù) ? ? ? ? ? ?String hex = Encoding.ASCII.GetString(databuffer, 0, count); ? ? ? ? ? ?//string msgREceive = System.Text.Encoding.UTF8.GetString(databuffer, 0, count); ? ? ? ? ? ?//Console.WriteLine(msgREceive); ? ? ? ? ? ?//Console.ReadKey();//程序終止的太快,方便觀察輸出 ? ? ? ? ? ?//關(guān)閉服務(wù)器端 ? ? ? ? ? ?clientSocket.Close();//關(guān)閉與客戶端的連接 ? ? ? ? ? ?serverSocket.Close();//關(guān)閉服務(wù)器自身的連接 ? ? ? ? ? ?return hex; ? ? ? ?}

模擬客戶端

private static void NewMethod() ? ? ? ?{ ? ? ? ? ? ?// 1.按照模擬工具上看 先new一個tcp服務(wù) ? ? ? ? ? ?//Parse將字符串轉(zhuǎn)換為IP地址類型 ? ? ? ? ? ?IPAddress myIP = IPAddress.Parse("192.168.88.1"); ? ? ? ? ? ?//構(gòu)造一個TcpClient類對象,TCP客戶端 ? ? ? ? ? ?TcpClient client = new TcpClient(); ? ? ? ? ? ?//與TCP服務(wù)器連接 ? ? ? ? ? ?client.Connect(myIP, 10067); ? ? ? ? ? ?Console.WriteLine("服務(wù)器已經(jīng)連接...請輸入對話內(nèi)容..."); ? ? ? ? ? ?//創(chuàng)建網(wǎng)絡(luò)流,獲取數(shù)據(jù)流 ? ? ? ? ? ?NetworkStream stream = client.GetStream(); ? ? ? ? ? ?//讀數(shù)據(jù)流對象 ? ? ? ? ? ?StreamReader sr = new StreamReader(stream); ? ? ? ? ? ?//寫數(shù)據(jù)流對象 ? ? ? ? ? ?StreamWriter sw = new StreamWriter(stream); ? ? ? ? ? ?while (true) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?string msg = Console.ReadLine(); ? ? ? ? ? ? ? ?sw.WriteLine(msg); ? ? ? ? ? ? ? ?sw.Flush(); ? ? ? ? ? ? //刷新流 ? ? ? ? ? ? ? ?Console.WriteLine("服務(wù)器:" + sr.ReadLine()); ? ? ? ? ? ?} ? ? ? ? ? ?client.Close(); ? ? ? ? ? ?Console.Read(); ? ? ? ? ? ?// 2.輸入的數(shù)據(jù)轉(zhuǎn)化為 16 進(jìn)制傳輸 ? ? ? ?}

?

鏈接:https://www.dianjilingqu.com/607194.html

.Net core 使用socket 傳輸數(shù)據(jù)的評論 (共 條)

分享到微博請遵守國家法律
阿合奇县| 广宗县| 西乌| 衡阳市| 榆树市| 南丰县| 宁明县| 金阳县| 华宁县| 富宁县| 靖江市| 苏州市| 黑山县| 榆树市| 广安市| 鄂州市| 池州市| 宁城县| 白城市| 新闻| 留坝县| 当雄县| 万州区| 竹溪县| 界首市| 古交市| 静宁县| 长沙县| 太仓市| 株洲市| 拉孜县| 济南市| 寻甸| 中山市| 清水河县| 铜川市| 樟树市| 尉犁县| 靖安县| 汕头市| 文安县|