使用.NET访问Internet(5)Paul_Ni(原作)_第1页
使用.NET访问Internet(5)Paul_Ni(原作)_第2页
使用.NET访问Internet(5)Paul_Ni(原作)_第3页
使用.NET访问Internet(5)Paul_Ni(原作)_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、    同步客户端套接字示例下面的示例程序创建一个连接到服务器的客户端。该客户端是用同步套接字生成的,因此挂起客户端应用程序的执行,直到服务器返回响应为止。该应用程序将字符串发送到服务器,然后在控制台显示该服务器返回的字符串。 C#using System;using System.Net;using System.Net.Sockets;using System.Text; public class SynchronousSocketClient public static void StartClient() / Data buffer for in

2、coming data.byte bytes = new byte1024; / Connect to a remote device.try / Establish the remote endpoint for the socket./The name of the/ remote device is "".IPHostEntry ipHostInfo = Dns.Resolve("");IPAddress ipAddress = ipHostInfo.AddressList0;IPEndPoint remoteEP = new IPEnd

3、Point(ipAddress,11000); / Create a TCP/IPsocket.Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); / Connect the socket to the remote endpoint. Catch any errors.try sender.Connect(remoteEP); Console.WriteLine("Socket connected to 0",sender.RemoteE

4、ndPoint.ToString(); / Encode the data string into a byte array.byte msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); / Send the data through bytesSent = sender.Send(msg); / Receive the response from the remote bytesRec = sender.Receive(bytes);Console.Wri

5、teLine("Echoed test = 0",Encoding.ASCII.GetString(bytes,0,bytesRec); / Release the socket.sender.Shutdown(SocketShutdown.Both);sender.Close(); catch (ArgumentNullException ane) Console.WriteLine("ArgumentNullException : 0",ane.ToString(); catch (SocketException se) Console.WriteL

6、ine("SocketException : 0",se.ToString(); catch (Exception e) Console.WriteLine("Unexpected exception : 0", e.ToString(); catch (Exception e) Console.WriteLine( e.ToString();public static int Main(String args) StartClient();return 0;同步服务器套接字示例下面的示例程序创建一个接收来自客户端的连接请求的服务器。该服务器是用同步套接

7、字生成的,因此在等待来自客户端的连接时不挂起服务器应用程序的执行。该应用程序接收来自客户端的字符串,在控制台显示该字符串,然后将该字符串回显到客户端。来自客户端的字符串必须包含字符串“<EOF>”,以发出表示消息结尾的信号。 C#using System;using System.Net;using System.Net.Sockets;using System.Text; public class SynchronousSocketListener / Incoming data from the client.public static string data = null;

8、public static void StartListening() / Data buffer for incoming data.byte bytes = new Byte1024; / Establish the local endpoint for thesocket./Dns.GetHostName returns the name of the / host running the application.IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName();IPAddress ipAddress = ipHostInfo.

9、AddressList0;IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); / Create a TCP/IP socket.Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp ); / Bind the socket to the local endpoint and / listen for incoming connections.try listener.Bind(localEndPo

10、int);listener.Listen(10); / Start listening for connections.while (true) Console.WriteLine("Waiting for a connection.");/ Program is suspended while waiting for an incoming connection.Socket handler = listener.Accept();data = null; / An incoming connection needs to be processed.while (true) bytes = new byte1024;int bytesRec = handler.Receive(bytes);data += Encoding.ASCII.GetString(bytes,0,bytesRec);if (data.IndexOf("<EOF>") > -1) break; / Show the data

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论