資源簡介
TCP通訊協(xié)議,實現(xiàn)C#下邊的客戶端向服務(wù)端發(fā)送數(shù)據(jù),服務(wù)端接收,并且服務(wù)端無限次數(shù)的接收數(shù)據(jù),可以把接收的數(shù)據(jù)放到數(shù)據(jù)庫中,一個服務(wù)端對應(yīng)多個客戶端

代碼片段和文件信息
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;//要發(fā)送的字符串?
????????????byte[]?sendData?=?null;//要發(fā)送的字節(jié)數(shù)組?
????????????TcpClient?client?=?null;//TcpClient實例?
????????????NetworkStream?stream?=?null;//網(wǎng)絡(luò)流?
????????????IPAddress?remoteIP?=?IPAddress.Parse(“127.0.0.1“);//遠程主機IP?
????????????int?remotePort?=?1000;//遠程主機端口?1
????????????while?(true)//死循環(huán)?
????????????{
????????????????sendString?=?Console.ReadLine();//獲取要發(fā)送的字符串?
????????????????sendData?=?Encoding.Default.GetBytes(sendString);//獲取要發(fā)送的字節(jié)數(shù)組?
????????????????client?=?new?TcpClient();//實例化TcpClient?
????????????????try
????????????????{
????????????????????client.Connect(remoteIP?remotePort);//連接遠程主機?
????????????????????Console.WriteLine(“發(fā)送成功!“);//連接失敗?
????????????????}
????????????????catch?(Exception?e)
????????????????{
????????????????????Console.WriteLine(“連接超時,服務(wù)器沒有響應(yīng)!“);//連接失敗?
????????????????????Console.ReadKey();
????????????????????return;
????????????????}
????????????????stream?=?client.GetStream();//獲取網(wǎng)絡(luò)流?
????????????????stream.Write(sendData?0?sendData.Length);//將數(shù)據(jù)寫入網(wǎng)絡(luò)流
????????????????stream.Close();//關(guān)閉網(wǎng)絡(luò)流?
????????????????client.Close();//關(guān)閉客戶端?
????????????}?
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-02-01?14:29??TCP通訊協(xié)議案例\
?????文件????????1739??2018-02-01?14:25??TCP通訊協(xié)議案例\TcpTimeClient客戶端.cs
?????文件????????1268??2018-02-01?14:26??TCP通訊協(xié)議案例\TimeServer服務(wù)端.cs
評論
共有 條評論