You are on page 1of 19

1 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t

t r o n g M n g L A N
Mc Lc
Tng Quan V Lp Trnh Socket................................................................3
M Hnh Client/Server..........................................................................................................3
Kt Ni Server - Client Vi TCP/P......................................................................................4
Gii Thiu V Sockets.........................................................................................................5
Giao Thc .....................................................................................................................6
Gii Thiu S Lc V Tr Chi C Caro:...............................................................................................8
Thit K Chng Trnh...............................................................................................................................!
2 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
Tng quan v Lp trnh Socket
M hnh client/server
M hnh duoc ph bin nht v duoc chp nhn rng ri trong cc h thng phn tn l m hnh
client/server. Trong m hnh ny s c mt tp cc tin trnh m mi tin trnh dng vai tr nhu l mt trnh qun l ti
nguyn cho mt tp hop cc ti nguyn cho truc v mt tp hop cc tin trnh client trong d mi tin trnh thuc hin
mt tc v no d cn truy xut ti ti nguyn phn cng hoc phn mm dng chung. Bn thn cc trnh qun l ti
nguyn cn phi truy xut ti cc ti nguyn dng chung duoc qun l bi mt tin trnh khc, v vy mt s tin trnh
va l tin trnh client va l tin trnh server. Cc tin trnh pht ra cc yu cu ti cc server bt ky khi no chng
cn truy xut ti mt trong cc ti nguyn ca cc server. Nu yu cu l dng dn th server s thuc hin hnh dng
duoc yu cu v gi mt dp ng tr li ti tin trnh client.
M hnh client/server cung cp mt cch tip cn tng qut d chia s ti nguyn trong cc h thng phn
tn. M hnh ny c th duoc ci dt bng rt nhiu mi trung phn cng v phn mm khc nhau. Cc my tnh
duoc s dng d chy cc tin trnh client/server c nhiu kiu khc nhau v khng cn thit phi phn bit gia
chng; c tin trnh client v tin trnh server du c th chy trn cng mt my tnh. Mt tin trnh server c th s
dng djch v ca mt server khc.
M hnh truyn tin client/server hung ti vic cung cp djch v. Qu trnh trao di d liu bao gm:
1. Truyn mt yu cu t tin trnh client ti tin trnh server
2. Yu cu duoc server x l
3. Truyn dp ng cho client
M hnh truyn tin ny lin quan dn vic truyn hai thng dip v mt dng dng b ha c th gia client
v server. Tin trnh server phi nhn thc duoc thng dip duoc yu cu buc mt ngay khi n dn v hnh dng
pht ra yu cu trong client phi duoc tm dng (bj phong ta) v buc tin trnh client trng thi ch cho t khi n
nhn duoc dp ng do server gi v buc ba.
M hnh client/server thung duoc ci dt dua trn cc thao tc co bn l gi (send) v nhn (receive)
Qu trnh giao tip client v server c th din ra theo mt t rong hai ch d: bj phong ta (blocked) v khng
bj phong ta (non-blocked).
Ch "# $% &hong t'a ($)oc*+,-:
Trong ch d bj phong ta, khi tin trnh client hoc server pht ra lnh gi d liu (send), vic thuc thi ca
tin trnh s bj tm ngng cho ti khi tin trnh nhn pht ra lnh nhn d liu (receive).
Tuong tu di vi tin trnh nhn d liu, nu tin trnh no d (client hoc server) pht ra lnh nhn d liu, m ti
thi dim d chua c d liu gi ti th vic thuc thi ca tin trnh cng s bj tm ngng cho ti khi c d liu gi ti.
Ch "# *h.ng $% &hong t'a (non/$)oc*+,-
Trong ch d ny, khi tin trnh client hay server pht ra lnh gi d liu thuc su, vic thuc thi ca tin trnh
vn duoc tin hnh m khng quan tm dn vic c tin trnh no pht ra lnh nhn d liu d hay khng.
Tuong tu cho trung hop nhn d liu, khi tin trnh pht ra lnh nhn d liu, n s nhn d liu hin c, vic thuc
thi ca tin trnh vn duoc tin hnh m khng quan tm dn vic c tin trnh no pht ra lnh gi d liu tip theo
hay khng.
3 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
Kt ni Server - Client vi TCP/IP
Khi duoc chy, server cn duoc xc djnh r dja chi P v s "lng nghe trn mt port c th. Server s nm
trong trng thi ny cho dn khi client gi dn mt yu cu kt ni. Sau khi duoc server chp nhn, mt connection
s hnh thnh cho php server v client giao tip vi nhau.
C th hon, cc buc tin hnh trn server v client m ta cn thuc hin s dng giao thc TCP/P trong C#
(c th chy server v client trn cng mt my):
S+r0+r:
1. To mt di tuong S12t+3.4+t.Soc*+t2.Tc&Li2t+n+r d bt du "lng nghe trn mt cng cc
b.
2. oi v chp nhn kt ni t client vi phuong thc AccepSocket(). Phuong thc ny tr v mt
di tuong S12t+3.4+t.Soc*+t2.Soc*+t dng d gi v nhn d liu.
3. Thuc hin giao tip vi client.
4. ng Socket.
Thng thung quy trnh ny s duoc dt trong mt vng lp (lp li buc 2) d chp nhn nhiu kt ni cng
lc (s dng Thread) hoc cc kt ni ln luot.
C)i+nt:
1. To mt di tuong S12t+3.4+t.Soc*+t2.Tc&C)i+nt
2. Kt ni dn server vi dja chi v port xc djnh vi phuong thc TcpClient.Connect()
3. Ly lung (stream) giao tip bng phuong thc TcpClient.GetStream().
4. Thuc hin giao tip vi server.
5. ng lung v socket.
Quy trnh ny c th duoc minh hoa theo m hnh sau:
4 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
Gii thiu v Sockets
Trong nm 60 cc nh nghin cu thuc vin nghin cu nhng du n cao cp (ARPA) M dc yu cu t co
quan b quc phng M (DoD nm 72 ARPA dc st nhp vo DoD) pht trin mt h thng luu tr thng tin qun su
quan trong d phng trung hop sy ra chin tranh. V kt qu lm vic ca ho l mng din t ARPAnet ra di.
trong d tt c nhng thng tin qun su dc luu trn cc my tnh v chng lm mt phn ca mng. cc my tnh dc
dt nhng noi khc nhau v vic chao di thng tin gia chng thng qua kh nhiu cch khc nhau. Khi cp nhp
li d liu hay thm mi su dng b cc my chi din ra trong thi gian ngn. n dm bo phng chnh cho trung
hop mt my tnh bj ph hy nhung thng tin trn d khng bj mt.
T nhng nm 70 DoD d cho php nhng vin nghin cu phi qun su dc tip cn vo mng ARPAnet. V nhng
nh nghin cu quan tm dn vic kt ni my tnh hon l vic dng b d liu. ho s dng n d chuyn d liu v
nhng sinh vin trong vin d s dng vo nhng phn ca mng v n ch thnh Usenet mng s dng.
Bt du t nm 80 mng qun su v mng dn dng ca ARPAnet dc chia ra hnh thnh nhng khu vuc
mng usenet v nhng khu vuc mng ny dc ni li vi nhau th l NTERNET ra di
Mt diu thuc su cn thit l tiu chun ha nhng cch thc lin kt gia cc mng khc nhau v cc kiu my tnh
khc nhau bi th TCP/nternet Protocol (TCP/P) dc pht trin bi ARPA ch thnh mt tiu chun rng ri trn ton
mng nternet.
TCP/P l mt giao thc thn thin n cho php kt ni nhng my tnh d chuyn thng v chia s ti
nguyn suyn qua mng TCP v P chi l 2 giao thc trong b giao thc ny cn c UDP v tt c nhung giao thc
ny dc h tro trong .NET bn c th xen lp System.NET.sockets.socket
truy nhp vo P co bn ca netorks t tng ng dng chng ta phi s dng socket knh. Mt socket l
mt giao lp trnh v truyn thng dim cui n cho php thuc hin kt ni dn mt my tnh khc gi v nhn thng
tin vi my tnh d kin trc truyn thng gia 2 my tnh
Th.ng thng t5n t6i 7 )o6i 2oc*+t2
Ra socket : l loi thuc thi dng trn tng mng v d nhu giao thc P
Datagram sockets : Datagram l mt gi d liu du l kiu sockets dc thuc thi trn tng transposts
giao vn. tuy vy su chuyn giao dn mt tng l khng cht ch
Stream sockets : N di nghjch li datagram socket l nhng sockets dc cung cp cho dong d liu
Kin trc truyn thng hin di s dng stack cho nhng tng giao thc khc nhau noi d liu duoc
dua ti l tng cao nht. mi mt tng cng thm nhng thng tin dc trung giao thc vo d liu v
dua n cho tng k tip. tng thp nht thuc hin vic gi d liu. noi tip nhn d liu, thng qua
mi tng s b di nhng thng tin dc thm vo trong cc tng ca bn gi cho dn khi n ln dn
tng ng dng
Tng ng dng c th bj chia lm nhiu cc tng con. Bn c th ngh nhu th ny tng ng dng s
dng nn tng ML co bn giao thc try nhp di tung don gin (SAP) dng giao thc HTTP d
gi nhng cu lnh diu khin SAP trong ML n dc goi l HTTP tunneling dc s dng mt cch
dc bit di vi tung la khi m tung la dc cu hnh khng cho php SAP suyn qua n
8#t 2oc*+t c9 th: th;c hin $<1 thao t=c c $<n:
1. Kt ni vi mt my xa (v d, chun bj d gi v nhn d liu)
2. Gi d liu
3. Nhn d liu
4. Ngt lin kt
5. Gn cng
6. Nghe d liu dn
7. Chp nhn lin kt t cc my xa trn cng d duoc gn
>%a ch? 2oc*+t
ja chi ca mt socket trn mng TCP/P gm c hai phn:
ja chi P: mt s nguyn 32 bits mt card mng trn my tnh (host)
Cng djch v: 16 bits xc djnh dim kt ni vi mt ng dng trn mt host.
5 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
Gio Th!c
Giao th@c TCA (Trns"ission Control Protocol / BGiao th@c "iu *hi:n tru1n 0CnB- l mt trong cc
giao thc ct li ca b giao thc TCP/P. S dng TCP, cc ng dng trn cc my ch duoc ni mng c th to
cc kt ni vi nhau, m qua d chng c th trao di d liu hoc cc gi tin. Giao thc ny dm bo chuyn giao
d liu ti noi nhn mt cch dng tin cy v dng th tu. TCP cn phn bit gia d liu ca nhiu ng dng
(chng hn, djch v eb v djch v thu din t) dng thi chy trn cng mt my ch.
TCP h tro nhiu giao thc ng dng ph bin nht trn nternet v cc ng dng kt qu, trong d
c , thu din t v Secure Shell.
Trong b giao thc TCP/P, TCP l tng trung gian gia giao thc P bn dui v mt ng dng bn trn.
Cc ng dng thung cn cc kt ni dng tin cy kiudung ng d lin lc vi nhau, trong khi d, giao thc P
khng cung cp nhng dng kiu d, m chi cung cp djch v chuyn gi tin khng dng tin cy. TCP lm nhim v
ca tng giao vn trong m hnh S don gin ca cc mng my tnh.
Cc ng dng gi cc dng gm cc byte 8-bit ti TCP d chuyn qua mng. TCP phn chia dng byte ny
thnh cc don (segment) c kch thuc thch hop (thung duoc quyt djnh dua theo kch thuc ca don vj truyn
dn ti da (MTU) ca tng lin kt d liu ca mng m my tnh dang nm trong d). Sau d, TCP chuyn cc gi
tin thu duoc ti giao thc P d gi n qua mt lin mng ti m dun TCP ti my tnh dch. TCP kim tra d dm
bo khng c gi tin no bj tht lc bng cch gn cho mi gi tin mt s th tu (sequence number). S th tu ny
cn duoc s dng d dm bo d liu duoc trao cho ng dng dch theo dng th tu. M dun TCP ti du kia gi li
tin bo nhn (acknowledgement) cho cc gi tin d nhn duoc thnh cng; mt dng h (timer) ti noi gi s
bo time-out nu khng nhn duoc tin bo nhn trong khong thi gian bng mt round-trip time (RTT), v d liu
(duoc coi l bj tht lc) s duoc gi li. TCP s dng checksum (gi tr kim tra) d xem c byte no bj hng trong
qu trnh truyn hay khng; gi trj ny duoc tnh ton cho mi khi d liu ti noi gi truc khi n duoc gi, v duoc
kim tra ti noi nhn.
TCA cung cD& nhEng ch@c nFng chGnh 2au:
Thit lp, duy tr, gii phng lin kt gia 2 thuc th TCP
Phn pht gi tin dng tin cy
To s th tu (Squencing) cc gi d liu
iu khin li
Cung cp kh nng da kt ni cho cc qu trnh khc nhau gia thuc th ngun v thuc th
dch thng qua vic s dng s hiu cng
Truyn d liu theo ch d song cng (ull_Duplex)
TCA c9 nhEng "Hc tGnh 2au:
Hung kt ni
m bo d tin cy trong qu trnh truyn d liu
Phn don d liu truyn thnh cc gi tin don l
D liu truyn duoc dnh s th tu
Cung cp co ch phn hi sau khi nhn duoc d liu
S dng co ch pht hin li checksums
Ch@c nFng chGnh c)i+nt *t nIi ti 2+r0+r (tc&-
c djnh dja chi P v cng djch v server
To mt socket vi cng djch v cc b bt ky do TCP chi djnh
Kt ni socket dn server
Gi yu cu v nhn thng tin phn hi t server
ng kt ni
6 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
Ch@c nFng chGnh 2+r0+r chD& nhCn *t nIi c)i+nt (tc&-
To mt socket v gn cho socket mt dja chi c djnh (P v port) d cung cp djch v
t socket ch d passive
Chp nhn yu cu thit lp kt ni t client v nhn socket mi t h thng
Thuc hin qu trnh lp nhn yu cu v gi thng tin phn hi
Khi client kt thc, dng kt ni v tr v trng thi ch chp nhn yu cu thit lp kt ni t
cc client
Giao Th@c JKA (#ser $t%r" Protocol- l mt trong nhng giao thc ct li ca giao thc TCP/P.
Dng UDP, chuong trnh trn mng my tnh c th gi nhng d liu ngn duoc goi l datagram ti my khc.
UDP khng cung cp su tin cy v th tu truyn nhn m TCP lm; cc gi d liu c th dn khng dng th tu
hoc bj mt m khng c thng bo. Tuy nhin UDP nhanh v hiu qu hon di vi cc mc tiu nhu kch thuc nh
v yu cu kht khe v thi gian. Do bn cht khng trng thi ca n nn n hu dng di vi vic tr li cc truy
vn nh vi s luong ln ngui yu cu.
Nhng ng dng ph bin s dng UDP nhu DNS (Domain Name System), ng dng streaming
media, Voice over P, Trivial ile Transer Protocol (TTP), v game truc tuyn.
JKA c9 nhEng "Hc tGnh 2au:
Phi kt ni
Khng phn don d liu truyn
Nu c li xy ra, ng dng bt buc phi thuc hin truyn li d liu
Khng c co ch phn hi sau khi nhn d liu
JKA C)i+nt:
c djnh dja chi P v cng djch v server
To mt socket vi cng djch v cc b bt ky do UDP chi djnh
Chi djnh server cn giao tip
Gi yu cu v nhn thng tin phn hi t server
ng socket
JKA S+r0+r:
To mt socket v gn cho socket mt dja chi c djnh (P v port) d cung cp djch v
Thuc hin qu trnh lp nhn yu cu v gi thng tin phn hi
7 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N

Gii thiu so luoc v tr choi c caro:


C caro l mt loi tr choi kh ph bin v lut choi don gin v khng di hi nhiu v cng c choi,
bn chi cn mt t giy c k v bt l d.Chnh v vy
tr choi ny li cng ph bin trong gii hoc sinh sinh vin.
C caro l tr choi di khng 2 ngui: Tr choi di khng (to-agent, conlicting game ) : Gm 2 ngui choi, di th
ny s tm cch dnh chin thng truc di th kia trong mt s hu hn nuc di, mi nuc di duoc to ra dua t 1
trng thi bt ky ca trn du. Nu sau 1 s gii hn nuc di, nu chua ai dnh chin thng th xem nhu ho. Ngoi
ra, thng tin v trn du l hon ton bit duoc (perect inormation) di vi c 2 di th.
Tr choi c lut choi ht sc don gin nhu sau: mi di th trong mi luot di ca mnh s chon 1 trng cn li
trn bn c (k sn cc lui ) sao cho to thnh n con lin tip d chin thng ... Nu n 3 th n c 1 tn khc
l Tic Tac Toe, nhung dy tr choi kt thc khi c mt ngui dt duoc 5 con lin nhau cng
trn mt hng ngang, hng doc hoc dung cho. Nu khi d choi kn bn c nhung khng c ai dt duoc 5
con lin nhau th tr choi kt thc v ho nhau. Tr choi c caro c ngun gc t rt lu, v l tr choi mang tnh
quc t. cc nuc khc nhau tr choi ny mang nhng ci tn khc nhau nhung du gn vi
lut choi don gin ca n. V d nhu: Nht tr choi c tn Gomoku, Gobang (Go trong ting Nht c ngha l 5,
Gomoku c ngha l 5 dim), Anh tr choi c tn l Connect 5.
Sau dy l mt hnh nh ca tr choi Gomoku trn bn c 15x15
Ta thy r, trn bn c, qun den l qun di truc, cc nuc di theo th tu duoc dnh du nhu trn
Do thng thung qun den duoc di truc nn ngui choi thy r qun den c loi th hon hn qun trng.
Victor Allis d chng minh duoc rng qun den hon ton c th chin thng duoc d dng. V v vy c mt s
phin bn khc nhau ca tr choi cng vi su thay di nho nh trong lut choi d nhm gim loi th ca qun den
di. Trong d Vit Nam, tr choi c caro l mt phin bn nhu vy vi su tng thm trong lut choi l ngui thng
phi c 5 qun lin nhau v khng bj
chn 2 du. Lut choi lm cho tr choi Gomoku tr nn phc tp hon v tng thm sc mnh cho qun trng
c th dnh chin thng.
8 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
Gii thiu 2 )c 0 " tLi:
ti l xy dung mt chuong trnh phc v choi c caro vi cc chc nng co bn nhu sau:
Chc nng Multi Player: Choi da ngui dng, choi online: Chuong trnh h tro cho 2 ngui trn
cng mng LAN d c th choi v chat truc tip vi nhau trong lc choi.
LM ,o chNn " tLi:
Nhu d gii thiu trn, C caro l mt tr choi c t lu di nay, hin nay n rt ph bin trong
gii hoc dung. T nhu cu gii tr d, d c rt nhiu hng d sn xut tr choi caro trn my tnh, gip
cho vic choi c caro duoc d dng hon, v bn c th dnh c vi my m khng cn mt ngui bn
thuc su.
Trn cng mc dch mun dem tr choi c caro dn vi moi ngui, lm cho vic choi c tr nn
d dng hon, c th h tro cho bn choi vi bn b, bon em d chon chuong trnh choi c caro lm d
ti ca mnh.
4#i ,ung chng trnh:
y dung chuong trnh choi c caro (d 2 ngui choi vi nhau) gm cc ni dung sau:
V bn c .
Hin thj 2 k hiu (v d v ) d ngui di truc chon. Nhp ho tn ca tng ngui choi.
Cho php ngui choi dng cc phm mi tn hoc dng chut d di chuyn con tr dn cc
trn bn c.
Cho php dng phm Enter d chon rng dang cha con tr. nh du duoc chon bng 1
trong 2 k tu chon trong mc 2.
Cho php hon sau mt nuc.
Sau mi nuc chuong trnh kim tra xem d ai thng cuc hay chua. Nu c ngui thng,
chuong trnh thng bo ho tn ngui thng v kt thc mt vn, tri li tr choi duoc tip
tc.
Sau khi kt thc mt vn, chuong trnh s cho ngui choi tip hay kt thc, ty theo su lua
chon ca ngui choi
OLi To=n.
Vit chuong trnh d thuc hin tr choi dnh c caro (TicTacToe) trn mng:
Cho php nhiu ngui choi nhng my khc nhau moi noi trn internet. My server to ra mt
Socket phc v kt ni tng cp ngui tham gia choi c caro. Theo tng vn c. Mi phin l mt lung dm
bo su trao di gia 2 ngui choi v xc djnh trng thi ( thng/ thua) ca vn c. Trong mi vn c ngui
du kt ni vi server duoc goi l ngui th nht duoc gn vi k hiu cn ngui th 2 duoc gn vi k
hiu l . Ho thay phin nhau dnh vo nhng du kin ( nhn vo m mnh djnh dnh) d nhanh chng
dt duoc s (v d 4, hay 5) thng hang theo hng ngang, hng doc v dung cho. Ngui no dnh duoc
s d truc, ngui d thng c. S vn c duoc to trn server l khng hn ch.
AhPn TGch OLi To=n.
Dua vo bi ton ta phi thuc hin nhng cng vic sau:
- To ra 1 Server cho game caro.
- To ra 1 Client cho game caro
- My Server to ra 1 Socket phc v kt ni tng cp Client choi.
Lut choi ca game duoc thit lp nhu sau:
- Client kt ni truc th duoc goi l ngui choi 1 v duoc dnh truc.
- Client kt ni sau th duoc goi l ngui choi 2 v dnh sau.
- Client no dnh duoc s trn hng doc, hng ngang v dung cho ln hon hoc bng 5
th thng cuc.
9 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
THT K CHNG TRNH
Gio $in
Giao ,in chGnh
Co&e'
()" *+ ,)n C-
public void DraTable(Graphics dc)

cellC 0;
cellR 0;
try

or (int i 0; i m_Columns 30; i 30)

cellR 0;
or (int 34; m_Ros 30 30; 30)

Rectangle rect ne Rectangle(i, , 30, 30);
arrRectcellR, cellC rect;
dc.DraRectangle(Pens.Blue, rect);
i (CellscellR, cellC 1)

dc.Dramage(imageList.mages0,rect);

else i (CellscellR, cellC 2)

dc.Dramage(imageList.mages1, rect);

cellR;

cellC;


10 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
catch
dc.Dispose();

()" $i Chu./n Chu0t Khi Ch1i C-
void Caro_MouseDon(obect sender, MouseEventArgs e)

Graphics g this.CreateGraphics();
i (e.Button MouseButtons.Let)

Point p ne Point(e., e.Y);
#region or
or (int i 0; i m_Ros; i)

or (int 0; m_Columns; )

sitch (player)

case NCH.Player1:// A di

i (arrRecti, .Contains(p) Cellsi, 0)

loat top arrRecti, .Top;
loat let arrRecti, .Let;
sendPacket(let , top , 1, i , );
g.Dramage(imageList.mages0, arrRecti, );
Cellsi, (int)NCH.Player1;
player NCH.None;
rect arrRecti, ;
i (KEMTRA.Checkorin(Cells, i, , NCH.Player1)
NCH.Player1)

g.DraString(YU N, ne ont(Arial, 25, ontStyle.talic), Brushes.Red, ne
Point(10, 30));
_THANG t ne _THANG();
t.ShoDialog();
SetStatusMessage( in.);
sendPacket();

return;

break;
case NCH.Player2: //B di

i (arrRecti, .Contains(p) Cellsi, 0)

loat top arrRecti, .Top;
loat let arrRecti, .Let;
sendPacket(let , top , 2, i , );
g.Dramage(imageList.mages1, arrRecti, );
Cellsi, (int)NCH.Player2;
player NCH.None;
rect arrRecti, ;
i (KEMTRA.Checkorin(Cells, i, , NCH.Player2) NCH.Player2)

g.DraString(YU N, ne ont(Arial, 25, ontStyle.talic), Brushes.Red, ne
Point(10, 30));
11 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
_THANG t ne _THANG();
t.ShoDialog();
SetStatusMessage( in.);
sendPacket();

return;


break;


#endregion

g.Dispose();

23 Ki/" Tr
namespace CaroGame
{
public class KIEMTRA
{
static NCHOI curPlayer = NCHOINone!
static int curRo" = #$ curColumn = #!
static int Ro"s = CaroRo"s!
static int Columns = CaroColumns!
static int%$& 'oar( = ne" int%Ro"s$ Columns&!
public KIEMTRA)* { +
public static NCHOI C,ec-.or/in)int %$&Cells$int ,$ int c$NCHOI player*
{
curRo" = ,!
curColumn = c!
curPlayer = player!
'oar( = Cells!
00Kiem tra Han1 t,u 2,2
i3 )KiemTraHan1T,uI)* 44 KiemTraCotT,uI)* 44 Top5e3t6To6'uttomRi1,t)* 44
TopRi1,t6To6'uttom5e3t)**
return player!
return NCHOINone!
+
7re1ion Kiem tra Han1 8a cot t,u I
pri8ate static bool KiemTraHan1T,uI)*
{
int count = #!
3or )int i = #! i 9 Columns! i::*
{
i3 )'oar(%curRo"$ i& == 'oar(%curRo"$ curColumn&*
{
count::!
+
else
{
count = #!
+
i3 )count ; <*
return true!
+
return 3alse!
12 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
+
pri8ate static bool KiemTraCotT,uI)*
{
int count = #!
3or )int i = #! i 9 Ro"s! i::*
{
i3 )'oar(%i$ curColumn& == 'oar(%curRo"$ curColumn&*
{
count::!
+
else
{
count = #!
+
i3 )count ; <*
return true!
+
return 3alse!
+
7en(re1ion
7re1ion Kiem tra = (uon1 c,eo
7re1ion Kiem tra (uon1 c,eo >)top?le3t ??; buttom?ri1,t*
pri8ate static bool Top5e3t6To6'uttomRi1,t)*
{
i3 ))curColumn ? curRo"* ;= #*
return KiemTraTren)true*!
else
return KiemTra@uoi)true*!
+
pri8ate static bool KiemTraTren)bool b*
{
int c = curColumn ? curRo"!
int r = #!
int count = #!
3or )int i = c! i 9 Columns! i::*
{
i3 )'oar(%r$ i& == 'oar(%curRo"$ curColumn&*
{
count::!
i3 )count ; <*
return true!
+
else
{
count = #!
+
r::!
+
return 3alse!
+
pri8ate static bool KiemTra@uoi)bool b*
{
int r = curRo" ? curColumn!
int count = #!
int c = #!
13 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
3or )int i = r! i 9= Ro"s ? A! i::*
{
i3 )'oar(%i$ c& == 'oar(%curRo"$ curColumn&*
{
count::!
i3 )count ; <*
return true!
+
else
{
count = #!
+
c::!
+
return 3alse!
+
7en(re1ion
7re1ion Kiem tra (uon1 c,eo >)top?ri1,t ??; buttom?le3t*
pri8ate static bool TopRi1,t6To6'uttom5e3t)*
{
i3 )curRo" : curColumn 9= Ro"s ? A*
return KiemTraTren)*!
else
return KiemTra@uoi)*!
+
pri8ate static bool KiemTraTren)*
{
int ne"Ro" = curColumn : curRo"!
int c = #!
int count = #!
3or )int i = ne"Ro"! i ;= #! i??*
{
i3 )'oar(%i$ c& == 'oar(%curRo"$ curColumn&*
{
count::!
i3 )count ; <*
return true!
+
else
{
count = #!
+
c::!
+
return 3alse!
+
pri8ate static bool KiemTra@uoi)*
{
int ne"Column = )curRo" : curColumn* ? Ro"s : A!
int count = #!
int r = Ro"s ? A!
3or )int i = ne"Column! i 9= Ro"s! i::*
{
i3 )'oar(%r$ i& == 'oar(%curRo"$ curColumn&*
{
count::!
i3 )count ; <*
14 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
return true!
+
else
{
count = #!
+
r??!
+
return 3alse!
+
7en(re1ion
7en(re1ion
+
+
Kt ni Sockets
pri8ate static Boc-et client!
pri8ate static byte%& (ata = ne" byte%A#=<&!
pri8ate T,rea( recei8er!
pri8ate 8oi( ConnectTo)strin1 ipBer8er*
{
BetBtatusMessa1e)CConnectin1C*!
client = ne" Boc-et)A((ress.amilyInterNet"or-$ Boc-etTypeBtream$
ProtocolTypeTcp*!
try
{
IPEn(Point iep = ne" IPEn(Point)IPA((ressParse)ipBer8er*$ D#E#*!
client'e1inConnect)iep$ ne" AsyncCallbac-)Connecte(*$ client*!
+
catc,
{
BetBtatusMessa1e)CIP In8ali(C*!
+
+
pri8ate 8oi( 5isten)*
{
BetBtatusMessa1e)C/aitin1C*!
Boc-et ne"soc- = ne" Boc-et)A((ress.amilyInterNet"or-$ Boc-etTypeBtream$
ProtocolTypeTcp*!
IPEn(Point iep = ne" IPEn(Point)IPA((ressAny$ D#E#*!
ne"soc-'in()iep*!
ne"soc-5isten)=*!
ne"soc-'e1inAccept)ne" AsyncCallbac-)AcceptConn*$ ne"soc-*!
+
8oi( AcceptConn)IAsyncResult iar*
{
Boc-et ol(ser8er = )Boc-et*iarAsyncBtate!
client = ol(ser8erEn(Accept)iar*!
BetBtatusMessa1e)CConnecte(C*!
recei8er = ne" T,rea()ne" T,rea(Btart)Recei8e@ata**!
recei8erBtart)*!
+

8oi( Connecte()IAsyncResult iar*
{
15 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
try
{
clientEn(Connect)iar*!

BetBtatusMessa1e)CConnecte(C*!
T,rea( recei8er = ne" T,rea()ne" T,rea(Btart)Recei8e@ata**!
recei8erBtart)*!
+
catc, )Boc-etEFception*
{
Messa1e'oFB,o")CK,Gn1 tHm t, y mIy c, T, - t n i lJiC$CT,Gn1
'IoC$Messa1e'oF'uttonsOK$Messa1e'oFIconError*!
+
+

8oi( Ben(@ata)IAsyncResult iar*
{
Boc-et remote = )Boc-et*iarAsyncBtate!
int sent = remoteEn(Ben()iar*!
+
8oi( Recei8e@ata)*
{
int rec8!
strin1 pac-@ata!
",ile )true*
{
rec8 = clientRecei8e)(ata*!
pac-@ata = Enco(in1ABCIIGetBtrin1)(ata$ #$ rec8*!
i3 )pac-@ata == CEFitC*
{ clientClose)*! +
Grap,ics (c = t,isCreateGrap,ics)*!
(cBmoot,in1Mo(e = Bmoot,in1Mo(eAntiAlias!
s"itc, )pac-@ataToBtrin1)**
{
case CRCK00Restart Game
Ne"Game)*! brea-!
case COCK00'ao O /in
t,isBetBtatusMessa1e)CO /inC*!
.6THLA 3t,A = ne" .6THLA)*!
3t,AB,o"@ialo1)*!
player = NCHOINone!
brea-!
case CMCK00'ao M /in
t,isBetBtatusMessa1e)CM /inC*!
.6THLA 3t,= = ne" .6THLA)*!
3t,=B,o"@ialo1)*!
player = NCHOINone!
brea-!
(e3aultK
{
strin1%& point = pac-@ataBplit)2$2*!
int F = Con8ertToIntAN)point%#&*!
int y = Con8ertToIntAN)point%A&*!
int user = Con8ertToIntAN)point%=&*!
int i = Con8ertToIntAN)point%O&*!
int P = Con8ertToIntAN)point%<&*!
i3 )user == A*
{
16 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
(c@ra"Ima1e)ima1e5istIma1es%#&$ ne" Rectan1le.)F$ y$ O#$
O#**!
player = NCHOIPlayer=!
t,isCells%i$ P& = user!
+
else
{
(c@ra"Ima1e)ima1e5istIma1es%A&$ ne" Rectan1le.)F$ y$ O#$
O#**!
player = NCHOIPlayerA!
t,isCells%i$ P& = user!
+
t,is@ra"Table)(c*!
+ brea-!
+
(c@ispose)*!
+
clientClose)*!
return!
+
8oi( sen(Pac-et)strin1 pac-et*
{
byte%& Bent = Enco(in1ABCIIGet'ytes)pac-et*!
client'e1inBen()Bent$ #$ Bent5en1t,$ #$ ne" AsyncCallbac-)Ben(@ata*$ client*!
+
7en(re1ion
Ph41n% th!c t5o ch1i "i
public 8oi( Ne"Game)*
{
In8ali(ate)*!
00player = PlayersPlayerA!
Cells = ne" int%m6Ro"s Q =$ m6Columns Q =&!
arrRect = ne" Rectan1le.%m6Ro"s$ m6Columns&!
3or )int i = #! i 9 m6Ro"s! i::*
{
3or )int P = #! P 9 m6Columns! P::*
{
Cells%i$ P& = )int*NCHOINone!
+
+
Ran(om r(m = ne" Ran(om)*!
int p = r(mNeFt)A$ O*!
i3 )p == A*
{
player = NCHOIPlayerA!
BetBtatusMessa1e)C)O* Ri trSTcC*!
+
else
{
player = NCHOIPlayer=!
BetBtatusMessa1e)C)M* Ri trSTcC*!
+
messT,is = CC!
+
17 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
18 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m c h a t
t r o n g M n g L A N
19 | L p T r n h M n g _ n g d n g l p t r n h s o c k e t l p t r n h p h n m m
c h a t t r o n g M n g L A N