最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間: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ù)器是用同步套接字生成的,
因此在等待來自客戶端的連接時(shí)掛起服務(wù)器應(yīng)用程序的執(zhí)行。該應(yīng)用程序接收來自客戶端的字符串,
在控制臺顯示該字符串,然后將該字符串回顯到客戶端。來自客戶端的字符串必須包含字符串“<EOF>”,
以發(fā)出表示消息結(jié)尾的信號。



 




C#
 復(fù)制代碼

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# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法

    C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法

    這篇文章主要介紹了C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法,需要的朋友可以參考下
    2015-09-09
  • 快速學(xué)習(xí)c# 枚舉

    快速學(xué)習(xí)c# 枚舉

    這篇文章主要介紹了c# 枚舉的相關(guān)知識,文中講解非常細(xì)致,示例代碼幫助大家學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片的示例

    c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片的示例

    本文主要介紹了c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片并保存的示例,代碼中使用了WebBrowser控件來完成這個功能,大家參考使用吧
    2014-01-01
  • 詳解C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用

    詳解C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用

    這篇文章主要介紹了C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用,注意區(qū)分一下簡單工廠模式、工廠方法模式和抽象工廠模式概念之間的區(qū)別,需要的朋友可以參考下
    2016-02-02
  • C#使用CryptoStream類加密和解密字符串的實(shí)現(xiàn)

    C#使用CryptoStream類加密和解密字符串的實(shí)現(xiàn)

    CryptoStream設(shè)計(jì)用于在內(nèi)容以流的形式輸出到文件時(shí)加密和解密內(nèi)容,本文主要介紹了C#使用CryptoStream類加密和解密字符串的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • C#中的==運(yùn)算符用法講解

    C#中的==運(yùn)算符用法講解

    本文詳細(xì)講解了C#中的==運(yùn)算符的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • C#在Unity游戲開發(fā)中進(jìn)行多線程編程的方法

    C#在Unity游戲開發(fā)中進(jìn)行多線程編程的方法

    這篇文章主要介紹了C#在Unity游戲開發(fā)中進(jìn)行多線程編程的方法,文中總結(jié)了Unity中使用多線程的幾種方式以及一款多線程插件的介紹,需要的朋友可以參考下
    2016-04-04
  • C#中倒序輸出字符串的方法示例

    C#中倒序輸出字符串的方法示例

    這篇文章主要給大家介紹了C#中倒序輸出字符串的方法示例,本文中的字符串倒序指的是將“嗎? 好 近 最”輸出“最 近 好 嗎?”,文中給出了兩種方法,需要的朋友可以參考借鑒,下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • c#打開py文件的方法

    c#打開py文件的方法

    在本篇內(nèi)容里小編給大家分享的是關(guān)于c#打開py文件的方法和步驟,需要的朋友們可以跟著學(xué)習(xí)下。
    2018-12-12
  • C#獲取真實(shí)IP地址實(shí)現(xiàn)方法

    C#獲取真實(shí)IP地址實(shí)現(xiàn)方法

    這篇文章主要介紹了C#獲取真實(shí)IP地址實(shí)現(xiàn)方法,對比了C#獲取IP地址的常用方法并實(shí)例展示了C#獲取真實(shí)IP地址的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10

最新評論

泗阳县| 新晃| 汤原县| 云南省| 广西| 乌拉特中旗| 微博| 濉溪县| 介休市| 安乡县| 沽源县| 肃北| 五家渠市| 疏勒县| 马关县| 金秀| 汝城县| 磐安县| 沁水县| 汉中市| 达孜县| 濉溪县| 江华| 莱州市| 新竹县| 贡嘎县| 天长市| 锦屏县| 宜阳县| 吉木萨尔县| 彩票| 即墨市| 绥阳县| 万盛区| 垦利县| 木里| 沛县| 金阳县| 呼图壁县| 张家港市| 平乐县|