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

invideo AI
invideo AI

InVideo 使用现成的模板简化视频创建

下载

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与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号