ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:47.39KB ,
资源ID:20593502      下载积分:2 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/20593502.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(是服务器端套接字并不定位具体的客户端套接字而是处文档格式.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

是服务器端套接字并不定位具体的客户端套接字而是处文档格式.docx

1、 server accepts the connection, a dialog can begin with between the client and the server. Once the client is done with whatever it needs to do it can close the connection with the server. Connections are expensive in the sense that servers allow finite connections to occur. During the time client h

2、as an active connection it can send the data to the server and/or receive the data. The complexity begins here. When either side (client or server) sends data the other side is supposed to read the data. But how will the other side know when data has arrived. There are two options - either the appli

3、cation needs to poll for the data at regular intervals or there needs to be some sort of mechanism that would enable application to get notifications and application can read the data at that time. Well , after all Windows is an event driven system and the notification system seems an obvious and be

4、st choice and it in fact is.As I said the two applications that need to communicate with each other need to make a connection first. In order for the two application to make connections the two applications need to identify each other ( or each others computer ). Computers on network have a unique i

5、dentifier called I.P. address which is represented in dot-notation like 10.20.120.127 etc. Lets see how all this works in .NET.System.Net.Sockets NamespaceBefore we go any further, download the source code attached with this article. Extract the zip file to a folder say c:Temp you will see following

6、 two folders : Server ClientIn the Server folder there will be one EXE . And in the client there will be the source code in C# that is our client. There will be one file called SocketClient.sln which the solution file . If you double click that your VS.NET will be launched and you will see the proje

7、ct SocketClientProj in the solution. Under this project you will have SocketClientForm.cs file. Now build the code ( by pressing Ctrl-Shift-B) and run the code you will see the following dialog box:As you can see the dialog box has a field for Host IP address ( which is the IP address of the machine

8、 on which you will run the Server Application ( located under Server folder) ). Also there is a field where you can specify port number at which the Server is listening. The server app I have provided here listens at port 8221. So I have specified port to be 8221.After specifying these parameters we

9、 need to connect to the server. So pressing Connect will connect to the server and to close the connection press Close. To send some data to the server type some data in the field near the button name Tx and if you press Rx the application will block unless there is some data to read.With this info

10、lets now try to check the code behind this:Socket programming in .NET is made possible by Socket class present inside the System.Net.Sockets namespace.Socket class has several method and properties and a constructor.The first step is to create an object of this class.Since there is only one construc

11、tor we have no choice but to use it.Here is how to create the socket:m_socListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.IP);The first parameter is the address family which we will use interNetwork - other options include Banyan NetBios etc.AddressFamily is an enum d

12、efined in Sockets namespace.Next we need to specify socket type: and we would use reliable two way connection-based sockets (stream) instead of un-reliable Connectionless sockets ( datagrams) . So we obviously specify stream as the socket type and finally we are using TCP/IP so we would specify prot

13、ocol type as Tcp.Once we have created a Socket we need to make a connection to the server since we are using connection-based communication.To connect to the remote computer we need to know the IP Address and port at which to connect.In .NET there is a class under System.Net namespace called IPEndPo

14、int which represents a network computer as an IP address and a port number.The IPEndPoint has two constructors - one that takes a IP Address and Port number and one that takes long and port number. Since we have computer IP address we would use the formerpublic IPEndPoint(System.Net.IPAddress addres

15、s, int port);As you can see the first parameter takes a IPAddress object. If you examine the IPAddress class you will see that it has a static method called Parse that returns IPAddress given a string ( of dot notation ) and second parameter will be the port number. Once we have endpoint ready we ca

16、n use Connect method of Socket class to connect to the end point ( remote server computer ).Here is the code:System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(10.10.101.200);System.Net.IPEndPoint remoteEP = new IPEndPoint (iAdd,8221);m_socClient.Connect (remoteEP);These three lines of code wil

17、l make a connection to the remote host running on computer with IP 10.10.101.200 and listening at port 8221. If the Server is running and started ( listening ), the connection will succeed. If however the server is not running an exception called SocketException will be thrown. If you catch the exce

18、ption and check the Message property of the exception in this case you see following text:No connection could be made because the target machine actively refused it.Similarly if you already have made a connection and the server somehow dies , you will get following exception if you try to send data.

19、An existing connection was forcibly closed by the remote hostAssuming that the connection is made, you can send data to other side using the Send method of the Socket class.Send method has several overloads. All of them take a byte array . For example if you want to send Hello There to host you can

20、use following call:tryString szData = ;byte byData = System.Text.Encoding.ASCII.GetBytes(szData);m_socClient.Send(byData);catch (SocketException se)MessageBox.Show ( se.Message );Note that the Send method is blocking. What it means the call will block till the data has been sent or an exception has

21、been thrown. There is an non-blocking version of the send which we will discuss in the next part of this article.Similar to Send there is a Receive method on the Socket class. You can receive data using following call:byte buffer = new byte1024;int iRx = m_socClient.Receive (buffer);The Receive meth

22、od again is blocking. It means that if there is no data available the call will block until some data arrives or an exception is thrown.Non-blocking version of Receive method is more useful than the non-blocking version of Send because if we opt for block Receive , we are effectively doing polling.

23、There is no events about data arrival. This model does not work well for serious applications. But all that is the subject of our next part of this article. For now we will settle with the blocking version.In order to use the source code and application here you would need to run the Server first:He

24、re is the way Server looks like:When you launch the Server, click Start to start listening. The Server listens at port 8221. So make sure you specify the port number 8221 in the port field of our client application. And in the IPAddress field of Client App enter the IP Address of the machine on whic

25、h the Server is running. If you send some data to server from the client by pressing Tx button, you will see that data in the grayed out edit box.DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment

26、 and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from i

27、mplementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. Although you can argue that one can overcome these shortcomings by multithreading meaning that one can spawn a new thread and let that thread do

28、the polling and notifies the main thread of the data. Well this concept will work well. But even if you create a new thread it would require your main thread to share the CPU time with this new thread. Windows operating system (Windows NT /2000 /XP) provide what is called Completion Port IO model fo

29、r doing overlapped ( asynchronous) IO. The details of IO Completion port are beyond the scope of the current discussion, but to make it simple you can think of IO Completion Ports as the most efficient mechanism for doing asynchronous IO in Windows that is provided by the Operating system. Completion Port model can be applied to any kind of IO including the file read /writ

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1