Làm chương trình phía ClientTrong ứng dụng này, chúng ta se thao tác với .Net Sockets. Chúng ta có 3 lớp chính là: TcpClient, TcpListener và NetworkStream. Trong đó, ứng dụng Listener luôn chạy đầu tiên bằng cách sử dụng phương thức start(): TcpListener.start(). Chúng ta sẽ viết ứng dụng TcpClient đầu tiên.
Đây là hàm có chức năng nhận tin nhắn:
Đây là hàm xử lý sự kiện khi nhấn nút Connect:Code:public void ThreadProcReceive() { for (;;) { received = new byte[1024]; int readBytes = 0; try { readBytes = tcp.GetStream().Read(received,0,received.Length); } catch (ObjectDisposedException) { return; } if (readBytes == 8) { StringBuilder shutMessage = new StringBuilder(8); for (int count = 0; count < 8; count++) { char ch = (char)received[count]; shutMessage = shutMessage.Append(ch); } string shut = "shutdown"; string receivedMessage = shutMessage.ToString(); if (receivedMessage.Equals(shut)) { MessageBox.Show(this,"Shutdown Request has arrived from the \nconnected party.\nYou cannot send message anymore.\nPlease close the window.","Shut down Request",MessageBoxButtons.OK,MessageBoxIcon.Information); buttonSend.Enabled = false; return; } } StringBuilder str = new StringBuilder(1024); for (int count = 0; count < readBytes; count++) { char ch = (char)received[count]; str = str.Append(ch); str = str.Append(" "); } textBoxSendText.Text = str.ToString(); if (formClosing == true) { break; } }
Đây là hàm xử lý sự kiện khi đóng Form1:Code:private void buttonConnect_Click(object sender, System.EventArgs e) { textBoxIPAddress.Enabled = false; IPAddress address = IPAddress.Parse(ipAddress); tcp = new TcpClient((new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0],4002))); LingerOption lingerOption = new LingerOption(false, 1); tcp.LingerState = lingerOption; tcp.Connect(new IPEndPoint(Dns.Resolve(ipAddress).AddressList[0],4001)); buttonSend.Enabled = true; ((Button)sender).Enabled = false; receiveThread = new Thread(new ThreadStart(ThreadProcReceive)); receiveThread.Name = "Client's Receive Thread"; receiveThread.ApartmentState = ApartmentState.MTA; receiveThread.Start(); }
Đây là hàm xử lý sự kiện khi nhấn nút Send:Code:private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { dataToSend = new byte[]{(byte)'s',(byte)'h',(byte)'u',(byte)'t',(byte)'d',(byte)'o',(byte)'w',(byte)'n'}; tcp.GetStream().Write(dataToSend,0,dataToSend.Length); tcp.Close(); } private void textBoxIPAddress_TextChanged(object sender, System.EventArgs e) { ipAddress = textBoxIPAddress.Text; }
Download mã nguồn:Code:private void buttonSend_Click(object sender, System.EventArgs e) { if (textBoxDataToSend.Text.Length != 0) { char[] charArray = textBoxDataToSend.Text.ToCharArray(0,textBoxDataToSend.Text.Length); dataToSend = new byte[textBoxDataToSend.Text.Length]; for (int charCount = 0; charCount < textBoxDataToSend.Text.Length; charCount++) { dataToSend[charCount] = (byte)charArray[charCount]; } } else { dataToSend = new byte[]{(byte)'e',(byte)'m',(byte)'p',(byte)'t',(byte)'y'}; } tcp.GetStream().Write(dataToSend,0,dataToSend.Length); textBoxDataToSend.Text = ""; }
http://www.mediafire.com/?dyymmmizmmm
Bookmarks