using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace 網絡客戶端TCPClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("客戶端已經啟動");
//1 . 創建一個Socket連接對象
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//這個IPEndPoint里面保存了要連接到的服務器的 IP地址和端口號
IPEndPoint point = new IPEndPoint(new IPAddress(new byte[]{192,168,1,85}), 12358);
//2. 連接到服務器端的端口
tcpClient.Connect(point);
byte[] reciveData = new byte[1024];
//3. 如果連接上服務器,那么就接收服務器發送的連接消息
// 參數的意思是 接收到的數據存放在哪里。 傳遞一個byte類型的數組
// 返回值的意思是接收到的數據的長度
int dataLength = tcpClient.Receive(reciveData);
//把byte數組中的數據轉換成字符串
string receiveString = Encoding.UTF8.GetString(reciveData, 0, dataLength);
Console.WriteLine("接收到服務器端的消息" + receiveString);
//4. 發送一條消息,發送給連接到的服務器
string sendMesssage = "1111";
tcpClient.Send(Encoding.UTF8.GetBytes(sendMesssage));
Console.WriteLine("程序執行完畢");
Console.ReadKey();
}
}
}