0

0

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

黄舟

黄舟

发布时间:2016-12-22 13:27:13

|

1437人浏览过

|

来源于php中文网

原创

同步客户端套接字示例   

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

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

Artifact News
Artifact News

由AI驱动的个性化新闻推送

下载

相关专题

更多
c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

57

2026.01.23

c++空格相关教程合集
c++空格相关教程合集

本专题整合了c++空格相关教程,阅读专题下面的文章了解更多详细内容。

57

2026.01.23

yy漫画官方登录入口地址合集
yy漫画官方登录入口地址合集

本专题整合了yy漫画入口相关合集,阅读专题下面的文章了解更多详细内容。

237

2026.01.23

漫蛙最新入口地址汇总2026
漫蛙最新入口地址汇总2026

本专题整合了漫蛙最新入口地址大全,阅读专题下面的文章了解更多详细内容。

393

2026.01.23

C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

17

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

103

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

73

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

81

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

70

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.4万人学习

golang socket 编程
golang socket 编程

共2课时 | 0.1万人学习

swoole入门物联网开发与实战
swoole入门物联网开发与实战

共15课时 | 1.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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