[vnk57]20121404

13
Họ và tên: Phạm Quang Duy MSSV: 20121404 Lớp: VN-7B I.Socket ( viết bằng c# ) 1.Sever: Code: using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; namespace Server { class Program { private const int BUFFER_SIZE=1024 private const int PORT_NUMBER=2404; static ASCIIEncoding encoding=new ASCIIEncoding(); public static void Main() { try { IPAddress address = IPAddress.Parse("192.168.3.101"); TcpListener listener=new TcpListener(address,PORT_NUMBER); // 1. listen listener.Start();

Upload: phamquangduy

Post on 24-Dec-2015

5 views

Category:

Documents


0 download

TRANSCRIPT

Họ và tên: Phạm Quang Duy

MSSV: 20121404 Lớp: VN-7B

I.Socket ( viết bằng c# )

1.Sever:

Code:

using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; namespace Server { class Program { private const int BUFFER_SIZE=1024 private const int PORT_NUMBER=2404; static ASCIIEncoding encoding=new ASCIIEncoding(); public static void Main() { try { IPAddress address = IPAddress.Parse("192.168.3.101"); TcpListener listener=new TcpListener(address,PORT_NUMBER); // 1. listen listener.Start();

Console.WriteLine("Server started on "+listener.LocalEndpoint); Console.WriteLine("Waiting for a connection..."); Socket socket=listener.AcceptSocket(); Console.WriteLine("Connection received from " + socket.RemoteEndPoint); var stream = new NetworkStream(socket); var reader=new StreamReader(stream); var writer=new StreamWriter(stream); writer.AutoFlush=true; while(true) { // 2. receive string str=reader.ReadLine(); // 3. send writer.WriteLine(str.ToUpper()); } // 4. close stream.Close(); socket.Close(); listener.Stop(); } catch (Exception ex) { Console.WriteLine("Error: " + ex); } Console.Read(); } } }

Chạy thực nghiệm:

2.Client:

Code:

using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; namespace Client { class Program { private const int BUFFER_SIZE = 1024; private const int PORT_NUMBER = 2404; static ASCIIEncoding encoding = new ASCIIEncoding(); public static void Main() { try { TcpClient Duy_client = new TcpClient(); // 1. connect Duy_client.Connect("192.168.3.101", PORT_NUMBER); Stream stream = Duy_client.GetStream(); Console.WriteLine("Connected to Server."); Console.Write("Enter your name: ");

string str = Console.ReadLine(); var reader = new StreamReader(stream); var writer = new StreamWriter(stream); writer.AutoFlush = true; // 2. send writer.WriteLine(str); // 3. receive str = reader.ReadLine(); Console.WriteLine(str); Console.Write("MSSV: "); string mssv = Console.ReadLine(); writer.AutoFlush = true; // send writer.WriteLine(mssv); // receive mssv = reader.ReadLine(); Console.WriteLine(mssv); Console.Write("Que quan: "); string que = Console.ReadLine(); writer.AutoFlush = true; // send writer.WriteLine(que); // receive que = reader.ReadLine(); Console.WriteLine(que); // 4. close stream.Close(); Duy_client.Close(); } catch (Exception ex) { Console.WriteLine("Error: " + ex); } Console.Read(); } } }

Chạy thực nghiệm:

3.Kiểm nghiệm bằng wireshark:

No.53 : một gói tin gửi từ địa chỉ 192.168.3.103 (client) đên địa chỉ 192.168.3.101

(sever) có nội dung “ pham quang duy ”

No.54: một gói tin gửi lại từ 192.168.3.101 (sever) đến địa chỉ 192.168.3.103

(client) có nội dung “ PHAM QUANG DUY ”

No.66: một gói tin gửi từ địa chỉ 192.168.3.103 (client) đên địa chỉ 192.168.3.101

(sever) có nội dung “ 20121404 ”

No.67: một gói tin gửi lại từ 192.168.3.101 (sever) đến địa chỉ 192.168.3.103

(client) có nội dung “ 20121404 ”

No.87: một gói tin gửi từ địa chỉ 192.168.3.103 (client) đên địa chỉ 192.168.3.101

(sever) có nội dung “ hai phong ”

No.88: một gói tin gửi lại từ 192.168.3.101 (sever) đến địa chỉ 192.168.3.103

(client) có nội dung “ HAI PHONG ”

Wireshark Lab: Ethernet and ARP 1. Capturing and analyzing Ethernet frames:

1. What is the 48-bit Ethernet address of your computer? My 48-bit Ethernet address of my computer is 44 : 6d :57 :75 :08 : 55

2. What is the 48-bit destination address in the Ethernet frame? Is this the Ethernet address of

gaia.cs.umass.edu? (Hint: the answer is no). What device has this as its Ethernet address?

The 48-bit destination address is Ethernet f8 :1a :67 :eb :5c :0c

This is not the Ethernet address of gaia.cs.umass.edu. This is the MAC address of the router which

contains an ARP module and adapter. It is the IP address of the gateway

3. Give the hexadecimal value for the two-byte Frame type field. What do the bit(s) whose value is 1

mean within the flag field?

Two-byte frame field value is 0x0800

4. How many bytes from the very start of the Ethernet frame does the ASCII “G” in “GET” appear in the Ethernet frame? There are 589 Bytes appear in the Ethernet frame

5. What is the hexadecimal value of the CRC field in this Ethernet frame?

There is no hexadecimal value in the CRC field.

6. What is the value of the Ethernet source address? Is this the address of your computer, or of

gaia.cs.umass.edu (Hint: the answer is no). What device has this as its Ethernet address?

Value of source address is f8 :1a :67 :eb :5c :0c. It is neither mine nor gaia.cs.umass address

.This is the address of the gateway (router/adapter)

7. What is the destination address in the Ethernet frame? Is this the Ethernet address of your

computer?

The destination address is 44 : 6d :57 :75 :08 : 55

This is not the Ethernet of my computer

8. Give the hexadecimal value for the two-byte Frame type field. What do the bit(s) whose value is 1

mean within the flag field?

Two-byte frame field value is 0x0800.

9. How many bytes from the very start of the Ethernet frame does the ASCII “O” in “OK” (i.e., the HTTP

response code) appear in the Ethernet frame

There are 56 bytes from the very beginning of the Ethernet frame

10. What is the hexadecimal value of the CRC field in this Ethernet frame?

There is no hexadecimal value in the CRC field.

2. The Address Resolution Protocol:

11. Write down the contents of your computer’s ARP cache. What is the meaning of each column

value?

The Internet Address column contains the IP address, the Physical Address column contains the MAC

address, and the type indicates the protocol type.

12. What are the hexadecimal values for the source and destination addresses in the Ethernet frame containing the ARP request message?

Hexadecimal value source is 44 : 6d :57 :75 :08 : 55 and destination is ff :ff :ff :ff :ff :ff.

13. Give the hexadecimal value for the two-byte Ethernet Frame type field. What do the bit(s) whose

value is 1 mean within the flag field?

Hexadecimal value for the two-byte Ethernet frame type field is 0x0806.

14. Download the ARP specification from ftp://ftp.rfc-editor.org/innotes/std/std37.txt . A readable, detailed discussion of ARP is also at http://www.erg.abdn.ac.uk/users/gorry/course/inet-pages/arp.html. a) How many bytes from the very beginning of the Ethernet frame does the ARP opcode field begin? There are 20 bytes from the very beginning of the Ethernet frame

b) What is the value of the opcode field within the ARP-payload part of the Ethernet frame in which an ARP request is made? The value of the opcode field is 0x0001. c) Does the ARP message contain the IP address of the sender?

Yes. The senders IP address is 192.168.3.101

d) Where in the ARP request does the “question” appear – the Ethernet address of the machine whose corresponding IP address is being queried? The field “Target MAC address” is set to 00:00:00:00:00:00 to question the machine whose corresponding IP address (192.168.3.101) is being queried. 15. Now find the ARP reply that was sent in response to the ARP request. a) How many bytes from the very beginning of the Ethernet frame does the ARP opcode field begin? There were 60 bytes from the very beginning of the Ethernet frame b) What is the value of the opcode field within the ARP-payload part of the Ethernet frame in which an ARP response is made? The value of the opcode field is 0x0002

c) Where in the ARP message does the “answer” to the earlier ARP request appear – the IP address of the machine having the Ethernet address whose corresponding IP address is being queried? The answer to the earlier ARP request appears in the”Sender MAC address” field, which contains the Ethernet address f8 :1a :67 :eb :5c :0c for the sender with IP address 192.168.1.33. 16. What are the hexadecimal values for the source and destination addresses in the Ethernet frame containing the ARP reply message?

Hexadecimal value source is f8 :1a :67 :eb :5c :0c destination is 44 : 6d :57 :75 :08 : 55

17. Open the ethernet-ethereal-trace-1 trace file in http://gaia.cs.umass.edu/wireshark-

labs/wireshark-traces.zip . The first and second ARP packets in this trace correspond to an ARP

request sent by the computer running Wireshark, and the ARP reply sent to the computer running

Wireshark by the computer with the ARP-requested Ethernet address. But there is yet another

computer on this network, as indiated by packet 6 – another ARP request. Why is there no ARP reply

(sent in response to the ARP request in packet 6) in the packet trace?

Because this host computer is not the router that maintains the ARP table and therefore does not give the sender an answer. Only the router running the network will respond to the ARP request.