亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c#(Socket)同步套接字代碼示例

 更新時間:2007年03月09日 00:00:00   作者:  
同步客戶端套接字示例  

下面的示例程序創(chuàng)建一個連接到服務(wù)器的客戶端。該客戶端是用同步套接字生成的,因此掛起客戶端應(yīng)用程序的執(zhí)行,直到服務(wù)器返回響應(yīng)為止。該應(yīng)用程序?qū)⒆址l(fā)送到服務(wù)器,然后在控制臺顯示該服務(wù)器返回的字符串。

C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SynchronousSocketClient {
public static void StartClient() {
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
// Create a TCP/IP  socket.
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.RemoteEndPoint.ToString());
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
// Send the data through the socket.
int bytesSent = sender.Send(msg);
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("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.WriteLine("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;
}
}
同步服務(wù)器套接字示例 下面的示例程序創(chuàng)建一個接收來自客戶端的連接請求的服務(wù)器。該服務(wù)器是用同步套接字生成的,
因此在等待來自客戶端的連接時掛起服務(wù)器應(yīng)用程序的執(zhí)行。該應(yīng)用程序接收來自客戶端的字符串,
在控制臺顯示該字符串,然后將該字符串回顯到客戶端。來自客戶端的字符串必須包含字符串“<EOF>”,
以發(fā)出表示消息結(jié)尾的信號。



 




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;
public static void StartListening() {
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the 
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
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(localEndPoint);
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 byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf("<EOF>") > -1) {
break;
}
}
// Show the data on the console.
Console.WriteLine( "Text received : {0}", data);
// Echo the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static int Main(String[] args) {
StartListening();
return 0;
}
}

相關(guān)文章

  • C#.NET 圖片水印添加代碼

    C#.NET 圖片水印添加代碼

    這篇文章主要為大家詳細介紹了C#.NET 圖片水印添加代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 解決Unity無限滾動復用列表的問題

    解決Unity無限滾動復用列表的問題

    這篇文章主要介紹了Unity無限滾動復用列表,無限滾動復用ScrollView就是解決這種問題,還可以用來做朋友圈,聊天等,需要的朋友可以參考下
    2022-04-04
  • winform 中顯示異步下載的圖片

    winform 中顯示異步下載的圖片

    本文主要介紹利用WebClient異步下載圖片,顯示在GridView上,需要的朋友可以參考下。
    2016-05-05
  • C# 數(shù)獨求解算法的實現(xiàn)

    C# 數(shù)獨求解算法的實現(xiàn)

    這篇文章主要介紹了C# 數(shù)獨求解算法的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • 使用C#中的Flags特性

    使用C#中的Flags特性

    這篇文章介紹了使用C#中的Flags特性,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • unity學習教程之定制腳本模板示例代碼

    unity學習教程之定制腳本模板示例代碼

    這篇文章主要給大家介紹了關(guān)于unity學習教程之定制腳本模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例

    ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例

    本文主要介紹了ftp服務(wù)器搭建部署與C#實現(xiàn)ftp文件的上傳的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • C# Winform多屏幕多顯示器編程技巧實例

    C# Winform多屏幕多顯示器編程技巧實例

    這篇文章主要介紹了C# Winform多屏幕多顯示器編程技巧實例,本文直接給出代碼實例,需要的朋友可以參考下
    2015-06-06
  • ItemsControl 數(shù)據(jù)綁定的兩種方式

    ItemsControl 數(shù)據(jù)綁定的兩種方式

    這篇文章主要介紹了ItemsControl 數(shù)據(jù)綁定的兩種方式,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#裝箱與拆箱操作的深入講解

    C#裝箱與拆箱操作的深入講解

    這篇文章主要給大家介紹了關(guān)于C#裝箱與拆箱操作的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學習或者使用C#具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03

最新評論