Hoc Tap
Learning
 
HomeHome  FAQFAQ  SearchSearch  RegisterRegister  Log inLog in  
Post new topic   Reply to topic
 

Cach giai bai tap thuc hanh 11/1

View previous topic View next topic Go down 
AuthorMessage
khoajacky




Joined : 12 Jan 2008
Posts : 1

PostSubject: Cach giai bai tap thuc hanh 11/1   12/1/2008, 09:06

Sau 1 thoi gian ngoi ngam nghi nghien cuu hom thuc hanh 11/1 nhom cua tui minh da lam duoc bai tap ma thay giao hom qua va da duoc thay cong nhan la dung.
Trong project sanxuattieuthu2005 cac ban them vao 1 button va dat ten Name : c2

Sau do them vao doan ma nay

private void c2_Click(object sender, EventArgs e)
{
if (thrConsumers[1].ThreadState == ThreadState.Suspended)
thrConsumers[1].Resume();
else
thrConsumers[1].Suspend();
}

trong ReceiveData() may ban them vao doan code ma toi boi den ben duoi
private void ReceiveData()
{
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint;

while (true)
{
try
{
RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.UTF8.GetString(receiveBytes);

switch (returnData)
{
case "0":
btnP1_Click(null, null);
break;
case "1":
btnP2_Click(null, null);
break;
case "2":
btnC1_Click(null, null);
break;
case "3":
c2_Click(null, null);
break;

}
}
catch (Exception err)
{
// Xử lư lỗi truyền thông
}
}

}

Con trong Project udpPeerClient cac ban lan luot tao ra 4 button va dat ten lan luot la p1_r, p2_r, c1_r, c2_r va cho doan ma nay vao :
private void p1_r_Click(object sender, EventArgs e)
{
//txtReceive.Text = "0";

// Chuyển thông điệp cần gửi sang mảng Byte
Byte[] sendBytes = Encoding.UTF8.GetBytes("0");

//udpClient.Connect(txtRemoteHost.Text, PortNumber); // RemotePort
//udpClient.Send(sendBytes, sendBytes.Length);

udpClient.Send(sendBytes, sendBytes.Length, txtRemoteHost.Text, Convert.ToInt16(txtRemotePort.Text));

udpClient.Close();
udpClient = new UdpClient(LocalPortNumber);
}

private void p2_r_Click(object sender, EventArgs e)
{
// Chuyển thông điệp cần gửi sang mảng Byte
Byte[] sendBytes = Encoding.UTF8.GetBytes("1");

//udpClient.Connect(txtRemoteHost.Text, PortNumber); // RemotePort
//udpClient.Send(sendBytes, sendBytes.Length);

udpClient.Send(sendBytes, sendBytes.Length, txtRemoteHost.Text, Convert.ToInt16(txtRemotePort.Text));

udpClient.Close();
udpClient = new UdpClient(LocalPortNumber);
}

private void c1_r_Click(object sender, EventArgs e)
{
// Chuyển thông điệp cần gửi sang mảng Byte
Byte[] sendBytes = Encoding.UTF8.GetBytes("2");

//udpClient.Connect(txtRemoteHost.Text, PortNumber); // RemotePort
//udpClient.Send(sendBytes, sendBytes.Length);

udpClient.Send(sendBytes, sendBytes.Length, txtRemoteHost.Text, Convert.ToInt16(txtRemotePort.Text));

udpClient.Close();
udpClient = new UdpClient(LocalPortNumber);
}

private void c2_r_Click(object sender, EventArgs e)
{
// Chuyển thông điệp cần gửi sang mảng Byte
Byte[] sendBytes = Encoding.UTF8.GetBytes("3");

//udpClient.Connect(txtRemoteHost.Text, PortNumber); // RemotePort
//udpClient.Send(sendBytes, sendBytes.Length);

udpClient.Send(sendBytes, sendBytes.Length, txtRemoteHost.Text, Convert.ToInt16(txtRemotePort.Text));

udpClient.Close();
udpClient = new UdpClient(LocalPortNumber);
}
the la hoan tat ! Chuc cac ban thanh cong
Back to top Go down
To Tuan
Admin



Age : 53
Joined : 08 May 2007
Posts : 190

PostSubject: Re: Cach giai bai tap thuc hanh 11/1   12/1/2008, 16:35

Cách làm của bạn KhoaJacky là đúng.

Bạn nên cho biết Họ tên và Mục quản để Thày nắm được em là ai.

Mục quản chính là Số thứ tự trong Danh sách điểm danh.

Các em tham khảo để chuẩn bị thi thực hành cho tốt.
Back to top Go down
TrongNguyen




Joined : 25 Dec 2007
Posts : 3

PostSubject: Re: Cach giai bai tap thuc hanh 11/1   14/1/2008, 07:56

Phạm Trọng Nguyễn
Mục quản: 27.

Ḿnh đồng ư với cách làm của bạn Khoa trong phần ReceiveData(), nhưng trong Project udpPeerClient bạn lần lượt tạo 4 button tương ứng với 2 nhà sản xuất và 2 nhà tiêu thụ, trong phạm vi bài này cách làm trên có thể chấp nhận được. Nhưng nếu chúng ta có nhiều hơn các nhà sản xuất và tiêu thụ th́ phải làm bao nhiêu button?
Theo cách làm của ḿnh th́ chỉ cần thêm vào phần ReceiveData() các case khác là đủ. Không cần phải thêm ở phần udpPeerClient v́ khi đă truyền được các giá trị là 0 và 1 th́ tại sao lại không truyền được các giá trị 3, 4, ...
Cách làm của bạn là bạn truyền các tham số mặc định ứng với mỗi nhà sản xuất, tiêu thụ bạn có 1 tham số truyền riêng; điều này chỉ có hiệu quả tốt khi số lượng nhà sản xuất, tiêu thụ ít.
Các bạn hăy thử và nếu có ư kiến xin phản hồi.
Back to top Go down
zerox




Joined : 18 Jan 2008
Posts : 2

PostSubject: Re: Cach giai bai tap thuc hanh 11/1   19/1/2008, 21:00

Lê Thanh Tuấn
Mục quản : 29

Ḿnh đồng ư với cách làm của bạn Trọng Nguyễn. Nhưng ḿnh sẽ thêm vào đoạn code sau:

if (returnData == "P1" || returnData == "p1")
returnData = "0";

if(returnData == "P2" || returnData == "p2")
returnData = "1";

if(returnData == "C1" || returnData == "c1")
returnData = "2";


vào trước lệnh switch trong hàm private void ReceiveData() cảu chương tŕnh SanXuatTieuThu C# .NET 2005 (Remote) với mục đích là khi nhập P1, P2 th́ sẽ kích hoạt nút P1, P2 chứ không nhập 0 hay 1 nữa.

Các bạn cho ḿnh hỏi, ḿnh có thể viết như thế này được không? Tại sao?

switch (returnData)
{
case "P1":
btnP1_Click(null, null);
break;
case "P2":
btnP2_Click(null, null);
break;
case "C1":
btnC1_Click(null, null);
break;
}
Back to top Go down

Cach giai bai tap thuc hanh 11/1

View previous topic View next topic Back to top 
Page 1 of 1

Permissions of this forum:You cannot reply to topics in this forum
Hoc Tap :: Môn học HĐH :: Hệ Điều Hành-
Post new topic   Reply to topic