网络编程01
//服务端 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace learn { class Program { const int portNo = 500; static void Main(string[] args) { IPAddress localAdd = IPAddress.Parse("127.0.0.1"); TcpListener listener = new TcpListener(localAdd, portNo); listener.Start(); TcpClient tcpClient = listener.AcceptTcpClient(); NetworkStream ms = tcpClient.GetStream(); byte[] data = new byte[tcpClient.ReceiveBufferSize]; int numBytesRead = ms.Read(data, 0,System.Convert.ToInt32(tcpClient.ReceiveBufferSize)); Console.WriteLine("Receiced:" + Encoding.ASCII.GetString(data, 0, numBytesRead)); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; namespace Client_CS { class Program { const int portNo = 500; static void Main(string[] args) { TcpClient tcpclient = new TcpClient(); tcpclient.Connect("127.0.0.1", portNo); NetworkStream ns = tcpclient.GetStream(); byte[] data = Encoding.ASCII.GetBytes("Hello"); ns.Write(data, 0, data.Length); } } }