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

来源:这里教程网 时间:2026-02-21 13:06:43 作者:

同步客户端套接字示例   

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

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"); 
// 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; 


同步服务器套接字示例 下面的示例程序创建一个接收来自客户端的连接请求的服务器。该服务器是用同步套接字生成的, 
因此在等待来自客户端的连接时挂起服务器应用程序的执行。该应用程序接收来自客户端的字符串, 
在控制台显示该字符串,然后将该字符串回显到客户端。来自客户端的字符串必须包含字符串“”, 
以发出表示消息结尾的信号。 



  




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("") > -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; 

 以上就是c#(Socket)同步套接字代码示例的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关推荐