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

黄舟
发布: 2016-12-22 13:27:13
原创
1394人浏览过

同步客户端套接字示例   

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

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)!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号