tailieunhanh - Foundations of F#.Net phần 9
Tham khảo tài liệu 'foundations of f#.net phần 9', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 246 CHAPTER 10 DISTRIBUTED APPLICATIONS open open open let form let temp new Form - F Talk Client fun e - 0 let output new TextBox Dock ReadOnly true Multiline true output let input new TextBox Dock Multiline true input let tc new TcpClient localhost 4242 let load let run let sr new StreamReader while true do let text if text null text then new MethodInvoker fun - text - ignore let t new Thread new ThreadStart run fun _ - load let sw new StreamWriter let keyUp _ if 1 then let text if text null text then CHAPTER 10 DISTRIBUTED APPLICATIONS 247 begin try text with err - sprintf Server error n n O err ignore end - fun _ - keyUp e temp STAThread do form Figure 10-1 shows the resulting client-server application. Figure 10-1. The chat client-server application Now you ll look at how the client in Listing 10-2 works. The first portion of code in the client is taken up initializing various aspects of the form this is not of interest to you at the moment though you can find details of how WinForms applications work in Chapter 8. The first part of Listing 10-2 that is relevant to TCP IP sockets programming is when you connect to the server. You do this by creating a new instance of the TcpClient class and calling its Connect method 248 CHAPTER 10 DISTRIBUTED APPLICATIONS let tc new TcpClient localhost 4242 In this example you specify localhost which is the local computer and port 4242 which is the same port on which the server is listening. In a more realistic example you d probably give the DNS name of the server
đang nạp các trang xem trước