You are on page 1of 16

Cu hi lp trnh mng

1) Cho on code chy trn TCP server nh sau:


IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Socket client = newsock.Accept(); IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Da ket noi voi client {0} tai port {1}", clientep.Address, clientep.Port);

Khi client dng chng trnh Telnet telnet ti server vi cu lnh: telnet 127.0.0.1 5000 th kt qu xut ra th no? 2) Cho on code chy trn TCP server nh sau:
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Socket client = newsock.Accept(); IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; int recv; byte[] data = new byte[10]; string welcome = "Chao mung client ket noi vao"; data = Encoding.ASCII.GetBytes(welcome); client.Send(data, data.Length, SocketFlags.None);

T client telnet vo server, cu cho pha client nhn c cu cho nh th no? 3) Cho on code chy trn UDP Server nh sau:
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Dang cho client ket noi den..."); Socket client = newsock.Accept();

Khi chy chng trnh c li khng? Nu c li th li xy ra u?, Ti sao li b li ny, hy sa li 4) Cho on code nhn v gi d liu chy trn TCP Server nh sau:
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); }

Bin recv s tr v 0 v thot khi vng lp khi no? 5) Cho on code nhn d liu chy trn TCP Server nh sau:

while (true) { data = new byte[10]; recv = client.Receive(data); if (recv == 0) break; Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); }

Di client gi ln thng ip: Khoa cong nghe thong tin th trn server kt qu xut ra th no? 6) Cho on code nhn d liu chy trn TCP Server nh sau:
while (true) { data = new byte[1024]; recv = client.Receive(data); if (recv == 0) break; Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); }

Di client ln lt gi ln server 2 thng ip: Thng ip 1: Khoa cong nghe thong tin Thng ip 2: Truong dai hoc Da Lat Sau khi client gi ht 2 thng ip trn ln server, th trn server kt qu xut ra th no? 7) Cho on code nhn d liu chy trn TCP Server nh sau:
data = new byte[10]; recv = client.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

Di client ln lt gi ln server 2 thng ip: Thng ip 1: Khoa cong nghe thong tin Thng ip 2: Truong dai hoc Da Lat Sau khi client gi ht 2 thng ip trn ln server, th trn server kt qu xut ra th no? 8) Cho on code nhn v gi d liu chy trn TCP Server nh sau:
while (true) { data = new byte[10]; recv = client.Receive(data); if (recv == 0) break; Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

client.Send(data, recv, SocketFlags.None);


}

Di client ln lt gi ln 2 thng ip: Thng ip 1: Khoa cong nghe thong tin Thng ip 2: Truong dai hoc Da Lat

th trn server kt qu xut ra th no? 9) Cho on code nhn v gi d liu chy trn TCP Server nh sau:
data = new byte[10]; while (true) { recv = client.Receive(data); if (recv == 0) break; Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); client.Send(data, recv, SocketFlags.None); }

Di client ln lt gi ln 2 thng ip: Thng ip 1: Khoa cong nghe thong tin Thng ip 2: Truong dai hoc Da Lat th di client kt qu xut ra th no? Bit hm gi v nhn d liu client nh sau:
while (true) { input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input)); data = new byte[1024]; recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); }

10) Cho on code nhn v gi d liu chy trn TCP Server nh sau:
data = new byte[25]; while (true) { client.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); client.Send(data, data.Length, SocketFlags.None); }

Di client ln lt gi ln 2 thng ip: Thng ip 1: Truong dai hoc Da Lat Thng ip 2: Khoa cntt th di client kt qu xut ra th no? Bit hm gi v nhn d liu client nh sau:
while (true) { input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input)); data = new byte[1024]; recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); }

11) Cho on code nhn v gi d liu chy trn TCP Server nh sau:

data = new byte[10]; while (true) { client.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); client.Send(data, data.Length, SocketFlags.None); }

Di client ln lt gi ln 2 thng ip: Thng ip 1: Dai hoc Da Lat Thng ip 2: Khoa cntt th di client kt qu xut ra th no? Bit hm gi v nhn d liu client nh sau:
while (true) { input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input)); data = new byte[1024]; recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); }

12) Cho on code nhn v gi d liu chy trn TCP Server nh sau:
data = new byte[10]; while (true) { client.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); client.Send(data, data.Length, SocketFlags.None); }

Di client gi ln server thng ip: hello th di client kt qu xut ra th no? Bit hm gi v nhn d liu client nh sau:
int recv; byte[] data = new byte[10]; while (true) { input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input), 0, 10, SocketFlags.None); data = new byte[1024]; recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); }

13) Cho on code nhn v gi d liu chy trn TCP Server nh sau:
data = new byte[10]; while (true) { client.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); client.Send(data, data.Length, SocketFlags.None); }

Di client gi ln server thng ip: Truong Dai hoc Da Lat th di client kt qu xut ra th no? Bit hm gi v nhn d liu client nh sau:
int recv; byte[] data = new byte[10]; while (true) { input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input), 0, 10, SocketFlags.None); data = new byte[1024]; recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); }

14) Cho on code nhn v gi d liu chy trn TCP Server nh sau:
data = new byte[1024]; while (true) { client.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); client.Send(data, data.Length, SocketFlags.None); }

Di client ln lt gi ln 2 thng ip: Thng ip 1: Dai hoc Da Lat Thng ip 2: Khoa cntt th di client kt qu xut ra th no? Bit hm gi v nhn d liu client nh sau:
byte[] data = new byte[1024]; while (true) { input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input)); data = new byte[1024]; recv = server.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); }

15) C chng trnh TCP Server nh sau:


using System; using System.Net; using System.Net.Sockets; using System.Text; class SimpleTcpSrvr { public static void Main() { IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Dang cho client ket noi den..."); Socket client = newsock.Accept(); IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Da ket noi voi client {0} tai port {1}", clientep.Address, clientep.Port); int recv;

byte[] data = new byte[10]; string welcome = "Chao mung client ket noi vao"; 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("Ngat ket noi voi client: {0}", clientep.Address); client.Close(); newsock.Close(); } }

Chng trnh TCP Server trn cho php ti a my client kt ni vo 16) C chng trnh TCP Server nh sau:
using System; using System.Net; using System.Net.Sockets; using System.Text; class SimpleTcpSrvr { public static void Main() { IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Dang cho client ket noi den..."); Socket client = newsock.Accept(); IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Da ket noi voi client {0} tai port {1}", clientep.Address, clientep.Port); int recv; byte[] data = new byte[10]; string welcome = "Chao mung client ket noi vao"; 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("Ngat ket noi voi client: {0}", clientep.Address); client.Close(); newsock.Close(); } }

Chng trnh TCP Server trn s b block ti nhng ch no, khi no th nhng ch b block thot khi tnh trng block 17) Cho on code nhn d liu chy trn TCP Server nh sau:
int recv; byte[] data = new byte[1024]; for (int i = 0; i < 5; i++) { recv = client.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

V on code gi d liu chy trn TCP Client nh sau:


server.Send(Encoding.ASCII.GetBytes("message server.Send(Encoding.ASCII.GetBytes("message server.Send(Encoding.ASCII.GetBytes("message server.Send(Encoding.ASCII.GetBytes("message server.Send(Encoding.ASCII.GetBytes("message 1")); 2")); 3")); 4")); 5"));

Khi client thc hin vic gi d liu ln server th kt qu trn server s hin th nh th no? 18) Cho chng trnh TCP Server nh sau:
using System; using System.Net; using System.Net.Sockets; using System.Text; class FixedTcpSrvr { private static int SendData(Socket s, byte[] data) {

//Sinh vin phi vit li hm ny


} private static byte[] ReceiveData(Socket s, int size) { } public static void Main() { byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Dang cho client ket noi den..."); Socket client = newsock.Accept(); IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Da ket noi voi {0} tai port {1}", newclient.Address, newclient.Port); string welcome = "Chao mung den voi server"; data = Encoding.ASCII.GetBytes(welcome); int sent = SendData(client, data); for (int i = 0; i < 5; i++) { data = ReceiveData(client, 9); Console.WriteLine(Encoding.ASCII.GetString(data)); } Console.WriteLine("Da dong ket noi voi {0}", newclient.Address); client.Close(); newsock.Close(); } }

m bo d liu khng b mt khi gi v nhn trong chng trnh TCP Server trn cc hm Send() v Receive() c vit li thnh cc SendData() v ReceiveData(). Sinh vin hy vit li hm SendData() gi khng b mt d liu. Bit lc client gi d liu ln n ch gi d liu khng gi km kch thc d liu. 19) Cho chng trnh TCP Server nh sau:
using System; using System.Net; using System.Net.Sockets; using System.Text; class FixedTcpSrvr { private static int SendData(Socket s, byte[] data) {

} private static byte[] ReceiveData(Socket s, int size) {

//Sinh vin phi vit li hm ny


} public static void Main() { byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Dang cho client ket noi den..."); Socket client = newsock.Accept(); IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Da ket noi voi {0} tai port {1}", newclient.Address, newclient.Port); string welcome = "Chao mung den voi server"; data = Encoding.ASCII.GetBytes(welcome); int sent = SendData(client, data); for (int i = 0; i < 5; i++) { data = ReceiveData(client, 9); Console.WriteLine(Encoding.ASCII.GetString(data)); } Console.WriteLine("Da dong ket noi voi {0}", newclient.Address); client.Close(); newsock.Close(); } }

m bo d liu khng b mt khi gi v nhn trong chng trnh TCP Server trn cc hm Send() v Receive() c vit li thnh cc SendData() v ReceiveData(). Sinh vin hy vit li hm ReceiveData() nhn khng b mt d liu. Bit lc client gi d liu ln n ch gi d liu khng gi km kch thc d liu 20) Cho chng trnh TCP Server nh sau:
using System; using System.Net; using System.Net.Sockets; using System.Text; class VarTcpSrvr { private static int SendData(Socket s, byte[] data) {

//Sinh vin phi vit li hm ny


} private static byte[] ReceiveData(Socket s) { } public static void Main() { 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("Dang cho client ket noi den..."); Socket client = newsock.Accept(); IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Da ket noi voi client {0} tai port {1}", newclient.Address, newclient.Port); string welcome = "Chao mung da ket noi den server"; data = Encoding.ASCII.GetBytes(welcome); int sent = SendData(client, data); for (int i = 0; i < 5; i++) {

data = ReceiveData(client); Console.WriteLine(Encoding.ASCII.GetString(data)); } Console.WriteLine("Da dong ket noi voi client: {0}", newclient.Address); client.Close(); newsock.Close(); } }

m bo d liu khng b mt khi gi v nhn trong chng trnh TCP Server trn cc hm Send() v Receive() c vit li thnh cc SendData() v ReceiveData(). Sinh vin hy vit li hm SendData() gi khng b mt d liu. Bit lc client gi d liu ln n c gi km kch thc d liu. 21) Cho chng trnh TCP Server nh sau:
using System; using System.Net; using System.Net.Sockets; using System.Text; class VarTcpSrvr { private static int SendData(Socket s, byte[] data) { } private static byte[] ReceiveData(Socket s) {

//Sinh vin phi vit li hm ny


} public static void Main() { 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("Dang cho client ket noi den..."); Socket client = newsock.Accept(); IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Da ket noi voi client {0} tai port {1}", newclient.Address, newclient.Port); string welcome = "Chao mung da ket noi den server"; data = Encoding.ASCII.GetBytes(welcome); int sent = SendData(client, data); for (int i = 0; i < 5; i++) { data = ReceiveData(client); Console.WriteLine(Encoding.ASCII.GetString(data)); } Console.WriteLine("Da dong ket noi voi client: {0}", newclient.Address); client.Close(); newsock.Close(); } }

m bo d liu khng b mt khi gi v nhn trong chng trnh TCP Server trn cc hm Send() v Receive() c vit li thnh cc SendData() v ReceiveData(). Sinh vin hy vit li hm ReceiveData() nhn khng b mt d liu. Bit lc client gi d liu ln n c gi km kch thc d liu. 22) Cho bit 1 i tng IPEndPoint c khi to nh sau:
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

i s th 2 trong phng thc to lp ca IPEndPoint mang gi tr 0 c ngha l g, trong trng hp no ta dng gi tr 0 cho i s ny? 23) Ti sao giao thc UDP khng cn phi phn bit bin ca cc thng ip? 24) Ti sao giao thc TCP phi phn bit bin ca cc thng ip? 25) Cho chng trnh sau:
using using using using System; System.Collections.Generic; System.Text; System.Threading;

namespace ConsoleApplication2 { class Program { public delegate void OutputDelegate(DelegateHandler handler); static void Main(string[] args) { OutputDelegate output = new OutputDelegate(OutputResult); output.Invoke(new DelegateHandler("Dai hoc", "Da Lat")); Console.Read(); } static void OutputResult(DelegateHandler handler) { Console.WriteLine("This is message 1: " + handler.Message1); Console.WriteLine("This is message 2: " + handler.Message2); } } class DelegateHandler { public string Message1; public string Message2; public DelegateHandler(string Message1, string Message2) { this.Message1 = Message1; this.Message2 = Message2; } } }

Chng trnh trn thc thi ng b, hy vit li chng trnh trn thc thi bt ng b m kt qu xut ra vn khng thay i (ch vit phn thc thi bt ng b, khng cn phi vit li ton b chng trnh) 26) Cho on code nhn v gi d liu chy trn UDP Server nh sau: UDP Server:
while (true) { buff = new byte[1024]; byteReceive = clientSocket.ReceiveFrom( buff, 0, buff.Length,SocketFlags.None, ref remote); Console.WriteLine(Encoding.ASCII.GetString(buff, 0, byteReceive)); clientSocket.SendTo(buff, 0, buff.Length, SocketFlags.None, remote);

UDP Client:
while (true) { string input = Console.ReadLine(); buff = Encoding.ASCII.GetBytes(input); serverSocket.SendTo(buff, 0, buff.Length, SocketFlags.None, remote); byteReceive = serverSocket.ReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref remote); Console.WriteLine(Encoding.ASCII.GetString(buff,0,byteReceive)); }

Khi chng trnh UDP Client gi thng ip: Khoa CNTT ln server th di client kt qu s nh th no? 27) Hai chng trnh UDP Server v TCP Server c th cng m 1 port c khng?, gii thch. 28) Cho on code nhn v gi d liu chy trn UDP Server nh sau: UDP Server:
while (true) { buff = new byte[1024]; byteReceive = clientSocket.ReceiveFrom( buff, 0, buff.Length,SocketFlags.None, ref remote); Console.WriteLine(Encoding.ASCII.GetString(buff, 0, byteReceive)); clientSocket.SendTo(buff, 0, 20, SocketFlags.None, remote); }

UDP Client:
while (true) { string input = Console.ReadLine(); buff = Encoding.ASCII.GetBytes(input); serverSocket.SendTo(buff, 0, buff.Length, SocketFlags.None, remote); try { byteReceive = serverSocket.ReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref remote); Console.WriteLine(Encoding.ASCII.GetString(buff, 0, byteReceive)); } catch { byteReceive = serverSocket.ReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref remote); Console.WriteLine(Encoding.ASCII.GetString(buff, 0, 20)); } }

Khi chng trnh UDP Client gi thng ip: Khoa CNTT ln server th di client kt qu s nh th no? 29) Trong trng hp no th ta chn lp trnh mng theo giao thc UDP ? 30) Trong trng hp no th ta chn lp trnh mng theo giao thc TCP ? 31) Cho on code nhn v gi d liu trn UDP Server nh sau:
while (true) { buff = new byte[1024];

byteReceive = serveSocket.ReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref remoteEndPoint); str = Encoding.ASCII.GetString(buff, 0, buff.Length); Console.WriteLine(str); serveSocket.SendTo(buff, 0, byteReceive, SocketFlags.None, remoteEndPoint); }

Di Client gi ln thng ip Khoa cong nghe thong tin th trn server kt qu xut ra nh th no 32) Cho on code nhn v gi d liu trn UDP Server nh sau:
while (true) { buff = new byte[1024]; byteReceive = serveSocket.ReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref remoteEndPoint); str = Encoding.ASCII.GetString(buff, 0, byteReceive); Console.WriteLine(str); serveSocket.SendTo(buff, 0, buff.Length, SocketFlags.None, remoteEndPoint); }

Cho on code nhn v gi d liu trn UDP Client nh sau:


while (true) { str = Console.ReadLine(); buff = Encoding.ASCII.GetBytes(str); serverSocket.SendTo(buff, 0, buff.Length, SocketFlags.None, serverEndPoint); byteReceive = serverSocket.ReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref remoteEndPoint); str = Encoding.ASCII.GetString(buff, 0, buff.Length); Console.WriteLine(str); }

Di Client gi ln 2 thng ip: Truong dai hoc Da Lat Khoa cong nghe thong tin Th kt qu nhn c client s nh th no? 33) Cho on code UDP Client nh sau:
byte[] data = new byte[1024]; string input, stringData; IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); server.Connect(ipep); string welcome = "Hello Server"; data = Encoding.ASCII.GetBytes(welcome); server.Send(data); data = new byte[1024]; int recv = server.Receive(data); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

Nu server cha c bt th chng trnh UDP Client s b li ch no? To sao li b li ny 34) Khi gi cc gi tin ra ngoi mng theo giao thc UDP th n s gi trc tip ra ngoi hay n gi ra b m trc?Gii thch cch gi d liu theo giao thc UDP?

35) Khi ang chy chng trnh UDP Server v UDP Client, ta bt ng tt chng trnh UDP Client th server c bo li khng?V sao? 36) Khi gi cc gi tin ra ngoi mng theo giao thc TCP th n s gi trc tip ra ngoi hay n gi ra b m trc?Gii thch cc gi d liu theo giao thc TCP? 37) Giao thc UDP c cn phi phn bit bin ca cc thng ip hay khng?Hy cho bit cch gi d liu theo giao thc UDP? 38) Giao thc TCP c cn phi phn bit bin ca cc thng ip hay khng?Hy cho bit cch gi d liu theo giao thc TCP 39) Khi dng phng thc ReceiveFrom() ca giao thc UDP c d liu, nu b m d liu truyn vo phng thc nh v d liu khng c c ht bng phng thc ReceiveFrom() th phn d liu cn li s u? 40) Khi dng phng thc Receive() ca giao thc TCP c d liu, nu b m d liu truyn vo phng thc nh v d liu khng c c ht bng phng thc Receive() th phn d liu cn li s u? 41) Phng thc Receive() khc vi phng thc ReceiveFrom() ch no? 42) Phng thc Send() khc vi phng thc SendTo() ch no? 43) Ti sao trc khi gi phng thc Receive() ta phi reset li buffer? 44) Trong khi lp trnh mng Server ta phi dng ti thiu my socket?, cc socket dng nh th no?

45) Gii thch ngha cc i s trong phng thc to lp ca TcpSocket:


Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

46) Gii thch ngha cc i s trong phng thc to lp ca UdpSocket:


Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

47) Khi client kt ni ti server ti port 8000 th client s dng port no kt ni n? 48) Phng thc Bind() ca lp Socket dng lm g? Kiu i s truyn vo ca hm Bind() 49) Gii thch cch hot ng ca phng thc Accept() ca lp Socket? 50) Phng thc Receive() ca lp Socket s tr v 0 khi no? 51) Khi lp trnh mng ta s lng nghe client kt ni n trn mt port, khi mt client khc kt ni vo server ta c th dng port ny cho php client kt ni vo khng hay cn phi dng port khc? 52) Gii thch qu trnh thnh lp kt ni ca giao thc TCP? 53) Gii thch qu trnh ng kt ni ca giao thc TCP? 54) Client gi 1024 byte d liu ln TCP server, trn TCP server ta dng phng thc Receive() c nhng buffer ch c chiu di ti a l 100 byte. Hi phng thc Receive() c pht sinh ra li hay khng? S byte cn li s c x l ra sao? 55) Client gi 1024 byte d liu ln UDP server, trn UDP server ta dng phng thc ReceiveFrom() c nhng buffer ch c chiu di ti a l 100 byte. Hi phng thc ReceiveFrom() c pht sinh ra li hay khng? S byte cn li c x l ra sao? 56) Client gi 1024 byte d liu ln TCP server, trn TCP server ta dng phng thc Receive() c nhng buffer ch c chiu di ti a l 100 byte. Trong trng hp ny server s x l ra sao? 57) Client gi 1024 byte d liu ln UDP server, trn UDP server ta dng phng thc ReceiveFrom() c nhng buffer ch c chiu di ti a l 100 byte. Trong trng hp ny server s x l ra sao? 58) Cc i s ca cc phng thc bt ng b nh BeginConnect(), BeginAccept(), BeginSend(), khc vi cc i s ca phng thc Begin(), Accept(), Send(), nh th no? 59) IPEndPoint l g? 60) Delegate l g? Hy k mt s trng hp s dng Delegate. 61) Hm Callback x l bt ng b, c 1 i s l IAsynResult i s ny dng lm g? 62) Trong qu trnh lp trnh bt ng b thng tin c truyn t phng thc bt u x l bt ng b (cc phng thc Begin) n hm callback x l khi phng thc bt ng b kt thc c truyn nh th no? 63) Cc i s ca hm kt thc vic x l bt ng b (cc hm End) 64) Cc bc to ra ng dng mt server nhiu client 65) Cho on chng trnh sau:
byte[] buff = new byte[10]; string hello = "Xin chao client"; buff = Encoding.ASCII.GetBytes(hello);

Console.WriteLine(Encoding.ASCII.GetString(buff,0,buff.Length));

Kt qu xut ra l g? 66) Trong qu trnh lp trnh bt ng b, thuc tnh AsyncState ca lp IAsyncResult cha thng tin g ? 67) Hm Start() ca lp TcpListener tng ng vi hm g ca lp Socket ? 68) Cc bc to ra mt ng dng TCP server dng cc lp helper ca C# (cc lp helper nh: TcpListener, TcpClient,. ) 69) Cc bc to ra mt ng dng TCP client dng cc lp helper ca C# (cc lp helper nh: TcpListener, TcpClient,.) 70) Cc bc to ra mt ng dng TCP server dng 2 hng i, 1 hng i gi v 1 hng i nhn d liu 71) Cc bc to ra mt ng dng TCP client dng 2 hng i, 1 hng i gi v 1 hng i nhn d liu 72) Gii thch cch hot ng ca phng thc : ThreadPool.QueueUserWorkItem() 73) Trong lp trnh mng 1 server nhiu client, khi 2 ng dng client cng kt ni vo 1 server th lc gi tr d liu v cho client lm sao ng dng server bit c phi gi tr d liu cho client no ? 74) C on code sau chy client:
byte[] buff = new byte[1024]; string welcome = "Hello server"; buff = Encoding.ASCII.GetBytes(welcome); server.Send(buff, buff.Length, SocketFlags.None); server.Send(buff, buff.Length, SocketFlags.None);

Sau khi client thc hin 2 lnh send() trn server mi thc hin lnh Receive()
byte[] buff = new byte[1024]; byteReceive = client.Receive(buff); Console.WriteLine(Encoding.ASCII.GetString(buff, 0, byteReceive));

Trn server kt qu s xut ra nh th no? Gii thch? 75) Cc qu trnh thc thi mt phng thc bt ng b, cho v d c th. 76) Vic gi d liu t server n client c thc hin nh sau:
streamWriter.WriteLine("Dang goi du lieu den client"); streamWriter.Flush();

Ti sao ta phi dng hm Flush()? 77) Khi no ta la chn phng php lp trnh mng theo giao thc TCP v khi no ta chn phng php lp trnh mng theo giao thc UDP? 78) S khc nhau gia phng php lp trnh mng theo giao thc TCP v phng php lp trnh mng theo giao thc UDP? 79) Delegate AsyncCallback s tham chiu n phng thc c ch k th no (kiu tr v, cc i s truyn vo) ?

80) Cc bc to ra mt Event? 81) Cch to ra v chy 1 Thread. Cho v d 82) c im ca giao thc TCP? 83) c im ca giao thc UDP? 84) Khi lp trnh server, ta c th khng dng bt k port no c khng? Gii thch? 85) Phng thc Connect() ca lp Socket c dng client hay server? Ti sao ta phi dng nh vy 86) Vit hm tnh tng ca 2 s v cho n chy trong ThreadPool 87) Phng thc AcceptTcpClient() ca lp TcpClient s tr v kiu d liu l g v phng thc ny l phng thc blocking hay nonblocking? 88) Ti sao phng thc ReceiveFrom() li c mt i s bt buc l: ref EndPoint?. i s ny cha thng tin g? 89) Lm sao bit c a ch IP v port ca client kt ni n server? 90) Trong khi lp trnh mng 1 server nhiu client th mi khi client kt ni n ta c cn to mt socket ring lin lc vi client khng hay ta s dng chung mt socket duy nht trn server? 91) server, chp nhn kt ni trn bt k a ch IP v port no ta dng thuc tnh no ca lp IPAddress? 92) Hm Listen() ca lp Socket c mt i s, i s l g? 93) Hm Accept() ca lp Socket s tr v mt Socket, Socket ny c dng lm g? 94) Vit li hm int SendData(Socket
s, byte[] buff)

thc hin vic gi d liu (buff) ra ngoi

mng, vic gi d liu ny phi m bo tt c d liu phi c gi ra mng, nu phn d liu no cha c gi ra th phi tin hnh gi li phn d liu cha c gi 95) Vit li hm int byte[]
ReceiveData(Socket s, int size)

thc hin vic nhn d liu, vic

gi d liu ny phi m bo nhn s byte c ch ra tham s size, nu phn d liu no cha c nhn th phi tin hnh nhn li phn d liu cha c nhn. 96) Hy a ra mt s bin php gi d liu t server n client v ngc li vi cc d liu c kch thc khc nhau. 97) Khi ta gi hm BeginInvoke() ca mt i tng c kiu l mt delegate, cc i s ca hm BeginInvoke() l g? Gii thch cc i s ny? 98) Vic gi v nhn d liu bt ng b khc vi ng b ch no? 99) Trong lp trnh mng dng cc lp helper nh TcpListener, TcpClient, UdpClient c thun li g so vi s dng cc lp Socket? 100) C bao nhiu cch thc thi mt phng thc do mt delegate tham chiu n? l nhng cch no, cho v d.

You might also like