TCP客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace TCP_client_communication
{
class Program
{
static void Main(string[] args)
{
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipaddress = IPAddress.Parse("192.168.123.1");
EndPoint point = new IPEndPoint(ipaddress, 7788);
tcpClient.Connect(point);
byte[] data = new byte[1024];
int length = tcpClient.Receive(data);
string message = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine("Server:"+message);
Console.Write("Client:");
string message2 = Console.ReadLine();
tcpClient.Send(Encoding.UTF8.GetBytes(message2));
Console.ReadKey();
}
}
}