6–Laboratoriya ishi: Soketlar asosida ma’lumot uzatishni dasturlash.
Ishdan
maqsad:
Telekommunikatsiyalarda
kommutatsiya
va
marshrutizatsiyalash turlari, sokketlar va sokketlar
asosida tarmoq qurilish
prinsiplarini o‘rganish.
Nazariy ma’lumotlar.
TCP soketlar ulanishga yo‘naltirilgan mijoz server
aloqa dasturlarini ishlab
chiqish uchun ishlatiladi. Agar server bir mijoz ulanishi bilan ma'lumotlarni
uzatishdan oldin to'rtta vazifalarni bajaradi:
- Socketni yaratish;
- Mahalliy IPEndPoint;
- Ish joyi socket rejimini tinglash;
- Socket kiruvchi aloqani qabul qiling.
Bu dars bu protseduralar oddiy TCP server yaratish
uchun qanday amalga
oshirilishini ko'rsatadi.
Server yaratish
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
Socket newsock = Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
Socket client = newsock.Accept();
Qabul () usuli bilan yaratilgan Socket ob'ekt endi server va mijoz o'rtasidagi
masofadan ikki tomonga ham ma'lumotlarni uzatish uchun foydalanish mumkin.
Bu barcha asosiy bosqichlar SimpleTcpSrvr.cs dasturida namoyish etiladi.
SimpleTcpSrvr.cs dasturi.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication5
{
class
Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
9050);
Socket newsock = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = newsock.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length,SocketFlags.None);
while(true)
{
data = new byte[1024];
recv = client.Receive(data);
if (recv == 0)
break;
Console.WriteLine( Encoding.ASCII.GetString(data, 0, recv));
client.Send(data, recv, SocketFlags.None);
}
Console.WriteLine("Disconnected from {0}",clientep.Address);
client.Close();
newsock.Close();
}
}
}
Birinchidan, bo'sh baytda kiruvchi va chiquvchi xabarlar uchun bir adres bufer
sifatida belgilangan. Socket orqali uzatiladigan barcha ma'lumotlar
qandaydir bir
baytlar qatori bo‘lishi kerak.
Next, an IPEndPoint object is defined for the local server machine:
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
IP Address.Any
maydonidan foydalanib, server
tizimiga uzatilishi mumkin
bo'lgan har qanday tarmoq interfeysi kelgan ulanish so'rovlarini qabul qiladi. Agar
faqat bitta interfeysidan paketlarini qabul qilishdan manfaatdor bo'lsangiz, alohida-
alohida quyidagi ibora bilan uning IP manzilini ko'rsatish mumkin:
IPEndPoint ipep = new
IPEndPoint(IPAddress.Parse("192.168.1.6"), 9050);
Bu misolda, server, darhol mijozga xabar yuboradi
va keyin mijoz kelgan
xabarlarni kutadi:
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length,