資源簡介
TCP通訊協議,實現C#下邊的客戶端向服務端發送數據,服務端接收,并且服務端無限次數的接收數據,可以把接收的數據放到數據庫中,一個服務端對應多個客戶端

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Net.Sockets;
using?System.Net;
using?System.Data.SqlClient;
using?System.Data;
namespace?TcpClientExample
{
????class?TcpTimeClient?
????{
????????
????????static?void?Main(string[]?args)
????????{
????????????string?sendString?=?null;//要發送的字符串?
????????????byte[]?sendData?=?null;//要發送的字節數組?
????????????TcpClient?client?=?null;//TcpClient實例?
????????????NetworkStream?stream?=?null;//網絡流?
????????????IPAddress?remoteIP?=?IPAddress.Parse(“127.0.0.1“);//遠程主機IP?
????????????int?remotePort?=?1000;//遠程主機端口?1
????????????while?(true)//死循環?
????????????{
????????????????sendString?=?Console.ReadLine();//獲取要發送的字符串?
????????????????sendData?=?Encoding.Default.GetBytes(sendString);//獲取要發送的字節數組?
????????????????client?=?new?TcpClient();//實例化TcpClient?
????????????????try
????????????????{
????????????????????client.Connect(remoteIP?remotePort);//連接遠程主機?
????????????????????Console.WriteLine(“發送成功!“);//連接失敗?
????????????????}
????????????????catch?(Exception?e)
????????????????{
????????????????????Console.WriteLine(“連接超時,服務器沒有響應!“);//連接失敗?
????????????????????Console.ReadKey();
????????????????????return;
????????????????}
????????????????stream?=?client.GetStream();//獲取網絡流?
????????????????stream.Write(sendData?0?sendData.Length);//將數據寫入網絡流
????????????????stream.Close();//關閉網絡流?
????????????????client.Close();//關閉客戶端?
????????????}?
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-02-01?14:29??TCP通訊協議案例\
?????文件????????1739??2018-02-01?14:25??TCP通訊協議案例\TcpTimeClient客戶端.cs
?????文件????????1268??2018-02-01?14:26??TCP通訊協議案例\TimeServer服務端.cs
- 上一篇:C#完整精美時間計時和倒計時軟件(帶進度條)
- 下一篇:c#做的飛鴿傳輸,能發送表情
評論
共有 條評論