c#

位置:IT落伍者 >> c# >> 浏览文章

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


发布日期:2021年08月06日
 
c#(Socket)同步套接字代码示例

同步客户端套接字示例

下面的示例程序创建一个连接到服务器的客户端该客户端是用同步套接字生成的因此挂起客户端应用程序的执行直到服务器返回响应为止该应用程序将字符串发送到服务器然后在控制台显示该服务器返回的字符串

C#

using System;

using SystemNet;

using SystemNetSockets;

using SystemText;

public class SynchronousSocketClient {

public static void StartClient() {

// Data buffer for incoming data

byte[] bytes = new byte[];

// Connect to a remote device

try {

// Establish the remote endpoint for the socket

// This example uses port on the local computer

IPHostEntry ipHostInfo = DnsResolve(DnsGetHostName())

IPAddress ipAddress = ipHostInfoAddressList[];

IPEndPoint remoteEP = new IPEndPoint(ipAddress);

// Create a TCP/IP socket

Socket sender = new Socket(AddressFamilyInterNetwork

SocketTypeStream ProtocolTypeTcp );

// Connect the socket to the remote endpoint Catch any errors

try {

senderConnect(remoteEP);

ConsoleWriteLine(Socket connected to {}

senderRemoteEndPointToString());

// Encode the data string into a byte array

byte[] msg = EncodingASCIIGetBytes(This is a test<EOF>);

// Send the data through the socket

int bytesSent = senderSend(msg);

// Receive the response from the remote device

int bytesRec = senderReceive(bytes);

ConsoleWriteLine(Echoed test = {}

EncodingASCIIGetString(bytesbytesRec));

// Release the socket

senderShutdown(SocketShutdownBoth);

senderClose();

} catch (ArgumentNullException ane) {

ConsoleWriteLine(ArgumentNullException : {}aneToString());

} catch (SocketException se) {

ConsoleWriteLine(SocketException : {}seToString());

} catch (Exception e) {

ConsoleWriteLine(Unexpected exception : {} eToString());

}

} catch (Exception e) {

ConsoleWriteLine( eToString());

}

}

public static int Main(String[] args) {

StartClient();

return ;

}

}

同步服务器套接字示例 下面的示例程序创建一个接收来自客户端的连接请求的服务器该服务器是用同步套接字生成的

因此在等待来自客户端的连接时挂起服务器应用程序的执行该应用程序接收来自客户端的字符串

在控制台显示该字符串然后将该字符串回显到客户端来自客户端的字符串必须包含字符串<EOF>

以发出表示消息结尾的信号

C#

复制代码

using System;

using SystemNet;

using SystemNetSockets;

using SystemText;

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[];

// Establish the local endpoint for the socket

// DnsGetHostName returns the name of the

// host running the application

IPHostEntry ipHostInfo = DnsResolve(DnsGetHostName());

IPAddress ipAddress = ipHostInfoAddressList[];

IPEndPoint localEndPoint = new IPEndPoint(ipAddress );

// Create a TCP/IP socket

Socket listener = new Socket(AddressFamilyInterNetwork

SocketTypeStream ProtocolTypeTcp );

// Bind the socket to the local endpoint and

// listen for incoming connections

try {

listenerBind(localEndPoint);

listenerListen();

// Start listening for connections

while (true) {

ConsoleWriteLine(Waiting for a connection);

// Program is suspended while waiting for an incoming connection

Socket handler = listenerAccept();

data = null;

// An incoming connection needs to be processed

while (true) {

bytes = new byte[];

int bytesRec = handlerReceive(bytes);

data += EncodingASCIIGetString(bytesbytesRec);

if (dataIndexOf(<EOF>) > ) {

break;

}

}

// Show the data on the console

ConsoleWriteLine( Text received : {} data);

// Echo the data back to the client

byte[] msg = EncodingASCIIGetBytes(data);

handlerSend(msg);

handlerShutdown(SocketShutdownBoth);

handlerClose();

}

} catch (Exception e) {

ConsoleWriteLine(eToString());

}

ConsoleWriteLine(\nPress ENTER to continue);

ConsoleRead();

}

public static int Main(String[] args) {

StartListening();

return ;

}

}

               

上一篇:ADO.NET实现应用程序数据访问层

下一篇:用C#+WMI获取w3wp进程对应的程序池