Bao Cao Mang Nang Cao

  • View
    115

  • Download
    3

Embed Size (px)

Transcript

i Hc Bch Khoa H Ni Vin in t vin thng ---------------o0o---------------

BO CO BI TP LNK thut mng nng cao ti : Lp trnh UDP socket giao tip Client ServerGing vin hng dn: Sinh vin thc hin : Nguyn Th Hng Phm Thu Hng Nguyn Ngc Kiu

T8 - K52

20071475

H Ni, thng 10 nm 2011

Mc lc

ContentsContents..................................................................................................................... 2

Yu cu ti: To chng trnh Client-Server trao i thng tin qua UDP socket thc hin cc chc nng sau : - Client sau khi kt ni s gi ti server thng tin v a ch Mac ca client - Server sau khi nhn thng tin s kim tra thng tin v client c ghi vo file hay cha, nu cha s lu vo file. Sau gi li client bn tin xc nhn nhn, ghi, c thng tin. - Server lu gi thng tin cc client hin ang kt ni vo server.Client mun ngt kt ni cn gi bn tin thng bo cho server - Khi c client khc kt ni vo server, server gi cho cc client khc thng tin v client mi kt ni vo.Client nhn c bn tin ny s hin th thng tin mac ca client mi kt ni vo server

I. Cc kin thc c bn v my tnh 1. H giao thc TCP/IP

Hnh 1-3: Cc tng ca giao thc TCP/IP so vi cc tng ca m hnh OSI Application: Xc nhn quyn, nn d liu v cc dch v cho ngi dng Transport: X l d liu gia cc h thng va cung cp vic truy cp mng cho cc ng dng Network: Tm ng cho cc packet Link: Mc OS hoc cc thit b giao tip mng trn mt my tnh

Mt s im khc nhau ca TCP/IP v m hnh OSI + Lp ng dng trong TCP/IP x l chc nng ca lp 5,6,7 trong m hnh OSI + Lp transport trong TCP/IP cung cp c ch UDP truyn khng tin cy, transport trong OSI lun m bo truyn tin cy + TCP/IP l mt tp ca cc protocols (mt b giao thc) + TCP/IP xy dng trc OSI 2. Giao thc UDP a. Khi qut UDP (User Datagram Protocol) l mt trong nhng giao thc ct li ca giao thc TCP/IP. Dng UDP, chng trnh trn mng my tnh c th gi nhng d liu ngn c gi l datagram ti my khc. UDP khng cung cp s tin cy v th t truyn nhn m TCP lm; cc gi d liu c th n khng ng th t hoc b mt m khng c thng bo. Tuy nhin UDP nhanh v hiu qu hn i vi cc mc tiu nh kch thc nh v yu cu kht khe v thi gian. Do bn cht khng trng thi ca n nn n hu dng i vi vic tr li cc truy vn nh vi s lng ln ngi yu cu. Nhng ng dng ph bin s dng UDP nh DNS (Domain Name System), ng dng streaming media, Voice over IP, Trivial File Transfer Protocol (TFTP), v game trc tuyn b. Cng UDP dng cng cho php cc giao tip gia cc ng dng din ra. Cng dng 16 bit nh a ch, v vy s ca cng nm trong khon 0 n 65.535. Cng 0 c dnh v khng nn s dng. Cng t 1 n 1023 c gi l cng "well-known" v trn cc h iu hnh ta Unix, vic gn kt ti mt trong nhng cng ny i hi quyn root.

Cng 1024 n 49.151 l cng ng k. Cng t 49.152 n 65.535 l cc cng tm, c dng ch yu bi client khi lin lc vi server c. Cu trc gi UDP l giao thc hng thng ip nh nht ca tng giao vn hin c m t trong RFC 768 ca IETF. Trong b giao thc TCP/IP, UDP cung cp mt giao din rt n gin gia tng mng bn di (th d, IPv4) v tng phin lm vic hoc tng ng dng pha trn. UDP khng m bo cho cc tng pha trn thng ip c gi i v ngi gi cng khng c trng thi thng ip UDP mt khi c gi (V l do ny i khi UDP cn c gi l Unreliable Datagram Protocol).

UDP ch thm cc thng tin multiplexing v giao dch. Cc loi thng tin tin cy cho vic truyn d liu nu cn phi c xy dng cc tng cao hn. Phn header ca UDP ch cha 4 trng d liu, trong c 2 trng l ty chn ( nn trong bng).Source port Trng ny xc nh cng ca ngi gi thng tin v c ngha nu mun nhn thng tin phn hi t ngi nhn. Nu khng dng n th t n bng 0. Destination port Trng xc nh cng nhn thng tin, v trng ny l cn thit. Length Trng c di 16 bit xc nh chiu di ca ton b datagram: phn header v d liu. Chiu di ti thiu l 8 byte khi gi tin khng c d liu, ch c header. 2 Trng checksum 16 bit dng cho vic kim tra li ca phn header v d liu. Phng php tnh checksum c nh ngha trong RFC 768.

Do thiu tnh tin cy, cc ng dng UDP ni chung phi chp nhn mt mt, li hoc trng d liu. Mt s ng dng nh TFTP c nhu cu phi thm nhng k thut lm tin cy c bn vo tng ng dng. Hu ht cc ng dng UDP khng cn nhng k thut lm tin cy ny v i khi n b b i. Streaming media, game trc tuyn v voice over IP (VoIP) l nhng th d cho cc ng

dng thng dng UDP. Nu mt ng dng i hi mc cao hn v tnh tin cy, nhng giao thc nh TCP hoc m erasure c th dng thay. Thiu nhng c ch kim sot tc nghn v kim sot lung, cc k thut da trn mng l cn thit gim nguy hiu ng c tc nghn dy chuyn do khng kim sot, t l ti UDP cao. Ni cch khc, v ngi gi gi UDP khng th pht hin tc nghn, cc thnh phn da trn mng nh router dng hng i gi (packet queueing) hoc k thut b gi nh l nhng cng c gim ti ca UDP. Giao thc Datagram Congestion Control Protocol (DCCP) c thit k nh mt gii php cho vn bng cch thm hnh vi kim sot tc nghn cho thit b u cui cho cc dng d liu UDP nh streaming media. Mc d tng lng lu thng ca UDP trn mng thng ch vi phn trm, nhng c nhiu ng dng quan trng dng UDP, bao gm DNS, SNMP, DHCP v RIP. II. UDP socket a. Socket Socket l mt giao din lp trnh ng dng (API) mng Thng qua giao din ny chng ta c th lp trnh iu khin vic truyn thng gia hai my s dng cc giao thc mc thp l TCP, UDP Socket l s tru tng ho mc cao, c th tng tng n nh l thit b truyn thng hai chiu gi - nhn d liu gia hai my tnh vi nhau. Cc loi Socket Socket hng kt ni (TCP Socket) Socket khng hng kt ni (UDP Socket) Raw Socket

c im ca Socket hng kt ni C 1 ng kt ni o gia 2 tin trnh Mt trong 2 tin trnh phi i tin trnh kia yu cu kt ni. C th s dng lin lc theo m hnh Client/Server Trong m hnh Client/Server th Server lng nghe v chp nhn mt yu cu kt ni Mi thng ip gi u c xc nhn tr v Cc gi tin chuyn i tun t c im ca Socket khng hng kt ni Hai tin trnh lin lc vi nhau khng kt ni trc tip Thng ip gi i phi km theo a ch ca ngi nhn Thng ip c th gi nhiu ln Ngi gi khng chc chn thng ip ti tay ngi nhn Thng ip gi sau c th n ch trc thng ip gi trc . S hiu cng ca Socket

c th thc hin cc cuc giao tip, mt trong hai qu trnh phi cng b s hiu cng ca socket m mnh s dng. Mi cng giao tip th hin mt a ch xc nh trong h thng. Khi qu trnh c gn mt s hiu cng, n c th nhn d liu gi n cng ny t cc qu trnh khc. Qu trnh cn li cng yu cu to ra mt socketb. Gii thiu v NameSpace System.Net v System.Net.Sockets

Cung cp mt giao din lp trnh n gin cho rt nhiu cc giao thc mng. C rt nhiu lp lp trnh Ta quan tm lp IPAdress, IPEndPoint, DNS, Lp IPAdress Mt s Field cn ch : Any: Cung cp mt a ch IP ch ra rng Server phi lng nghe trn tt c cc Card mng Broadcast: Cung cp mt a ch IP qung b Loopback: Tr v mt a ch IP lp AdressFamily: Tr v h a ch ca IP hin hnh Lp IPAddress Mt s phng thc cn ch : Phng thc khi to IPAddress(Byte[]) IPAddress(Int64) IsLoopback: Cho bit a ch c phi a ch lp khng Parse: Chuyn IP dng xu v IP chun ToString: Tr a ch IP v dng xu TryParse: Kim tra IP dng xu c hp l khng? Lp IPEndPoint Cng, v d: 192.168.1.1:8080 Lp DNS Mt s thnh phn ca lp: HostName: Cho bit tn ca my c phn gii GetHostAddress: Tr v tt c IP ca mt

trm GetHostEntry: Gii p tn hoc a ch truyn vo v tr v i tng IPHostEntry GetHostName: Ly v tn ca my tnh cc b NameSpace System.Net.Sockets Mt s lp hay dng: TcpClient, UdpClient, TcpListener, Socket, NetworkStream, to ra Socket Socket(AddressFamily af, SocketType st, ProtocolType pt) III. Lp trnh 1. Giao din a. Server Cha c client kt ni

C Client kt ni

b. Client Login

Form chnh

2. Chng trnh a. Server using System;

using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation; using System.IO; using System.Text.RegularExpressions; //using System.Data;

namespace Server { // lenh dung trao doi giua Server va Client enum Command { Login, Logout, //Dang nhap toi Server //Dang xuat khoi Server

ACK, //Thong bao da nhan, ghi, da co thong tin MAC ve Client NewClient, Null, Stop } public partial class Form1 : Form //Gui cho Client thong bao dia chi MAC Client moi ket noi

//No command //Server will stop after 5s

{ // // Cau truc ClientInfo giu thong tin va moi Client ket noi vao server struct ClientInfo { public EndPoint endpoint; //Socket cua Client public string strMac; public string strIP; } //Dia chi Mac ma Client gui toi ket noi //dia chi IP client

// Cac Client ket noi vao Server duoc luu vao Mang ArrayList clientList,clientOld;

// Socket chinh ma Server dung de lang nghe Client Socket serverSocket;

// dinh nghia mot byteData byte[] byteData = new byte[1024];

// flag quiting dung cho viec Stop server bool _quiting; // bool _changeView; // port number cho server int _Port;

//flag dung de xac dinh da startserver chua bool fstartSer;

// public Form1() { InitializeComponent();

// khoi tao mang clientlist clientList = new ArrayList();

// lay dia chi card mang dang su dung

//UdpClient u = new UdpClient("8.8.8.8", 1); //localAddr = ((IPEndPoint)u.Client.LocalEndPoint).Address; //txtIPserver.Text = localAddr.ToString();

// Get host name String strHostName = Dns.GetHostName();

// Find host by name IPHostEntry iphostentry = Dns.GetHostByName(strHostName); // Dns.GetHostByName(strHostName);

// Enumerate IP addresses foreach (IPAddress ipaddress in iphostentry.AddressList) { comboBox1.Items.Add(ipaddress.ToString()); }

// Mac dinh Cong lang nghe la 8888 txtPort.Text = "8888"; _Port = 8888;

// Mac dinh chua chay Server fstartSer = false; button1.Text = "Start Server"; stopToolStripMenuItem.Enabled = false; toolTip1.SetToolTip(this.button1, "Click to Start Server...");

// tooltip toolTip1.SetToolTip(comboBox1, "IP address Server"); toolTip1.SetToolTip(txtPort, "Port number in which Server is listening");

// _changeView = false;

}

private void button1_Click(object sender, EventArgs e) { // if (fstartSer == false) { try { CheckForIllegalCrossThreadCalls = false;

// load cac