Both: soketga ma'lumotlarni yuborishni ham, qabul qilishni ham to'xtatish
Receive: ma'lumot olishni to'xtatish
Send: ma'lumotlar yuborishni to'xtatish
Mijoz.
Endi mijoz uchun loyihani qo'shaylik. Mijozning soketlarda ishlashining umumiy sxemasi biroz boshqacha bo'ladi:
To'liq mijoz kodi:
using System; using System.Text; using System.Net; using System.Net.Sockets; namespace SocketTcpClient { class Program { // biz ulanadigan serverning manzili va porti static int port = 8005; // server porti static string address = "127.0.0.1"; // server manzili static void Main(string[] args) { try { IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(address), port); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // masofaviy xostga ulanish socket.Connect(ipPoint); Console.Write("Habarni kiriting:"); string message = Console.ReadLine(); byte[] data = Encoding.Unicode.GetBytes(message); socket.Send(data); // Habarni qabul qilish data = new byte[256]; // habar uchun bufer StringBuilder builder = new StringBuilder(); int bytes = 0; // olingan baytlar soni do { bytes = socket.Receive(data, data.Length, 0); builder.Append(Encoding.Unicode.GetString(data, 0, bytes)); } while (socket.Available > 0); Console.WriteLine("server javobi: " + builder.ToString()); // soketni yopish socket.Shutdown(SocketShutdown.Both); socket.Close(); } catch(Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } } } Mijoz uchun hamma narsa bir xil, faqat endi, soket yaratilgandan so'ng, Connect() usuli chaqirilib, unga server manzili uzatiladi:
IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(address), port); socket.Connect(ipPoint); Endi server va mijoz dasturlarini boshlaymiz.
Mijoz konsol: