欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > c# >内容正文

c#

c# socket编程第二篇 -凯发k8官方网

发布时间:2024/10/8 c# 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 c# socket编程第二篇 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

     既然有了方向,确定了方案就要一步一步的执行方案。socket udp通信要考虑的问题很多,比如大文件传输的时候如何分包,如何校验,如何判断是否丢包,丢包后从发,如何保证文件的完整性等等一系列问题。

      在考虑这些问题的最开始,我们要解决的实现socket通信,如果通信都不能实现,又谈何程序的完整性。这贴一个socket udp的demo。新建两个控制台应用程序,一个是服务端,一个是客户端。模拟客服端和服务端通信

服务端 using system;
using system.collections.generic;
using system.text;
using system.net;
using system.net.sockets;

namespace bose.xuelecn.com.filereceivecontrol
{
class program
{
staticvoid main(string[] args)
{
int recv;
byte[] data =newbyte[1024];

//构建tcp 服务器

//得到本机ip,设置tcp端口号
ipendpoint ipep =new ipendpoint(ipaddress.any, 8001);
socket newsock
=new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp);

//绑定网络地址
newsock.bind(ipep);

console.writeline(
"this is a server, host name is {0}", dns.gethostname());

//等待客户机连接
console.writeline("waiting for a client");

//得到客户机ip
ipendpoint sender =new ipendpoint(ipaddress.any, 0);
endpoint remote
= (endpoint)(sender);
recv
= newsock.receivefrom(data, ref remote);
console.writeline(
"message received from {0}: ", remote.tostring());
console.writeline(encoding.ascii.getstring(data,
0, recv));

//客户机连接成功后,发送欢迎信息
string welcome ="welcome ! ";

//字符串与字节数组相互转换
data = encoding.ascii.getbytes(welcome);

//发送信息
newsock.sendto(data, data.length, socketflags.none, remote);
while (true)
{
data
=newbyte[1024];
//发送接受信息
recv = newsock.receivefrom(data, ref remote);
console.writeline(encoding.ascii.getstring(data,
0, recv));
newsock.sendto(data, recv, socketflags.none, remote);
}
}
}
}
客户端 using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.net;
using system.net.sockets;

namespace filedistributecontrol
{
class program
{
staticvoid main(string[] args)
{
byte[] data =newbyte[1024];
string input, stringdata;

//构建tcp 服务器

console.writeline(
"this is a client, host name is {0}", dns.gethostname());

//设置服务ip,设置tcp端口号
ipendpoint ipep =new ipendpoint(ipaddress.parse("127.0.0.1"), 8001);

//定义网络类型,数据连接类型和网络协议udp
socket server =new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp);

string welcome ="hello! ";
data
= encoding.ascii.getbytes(welcome);
server.sendto(data, data.length, socketflags.none, ipep);
ipendpoint sender
=new ipendpoint(ipaddress.any, 0);
endpoint remote
= (endpoint)sender;

data
=newbyte[1024];
//对于不存在的ip地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.setsocketoption(socketoptionlevel.socket, socketoptionname.receivetimeout, 100);
int recv = server.receivefrom(data, ref remote);
console.writeline(
"message received from {0}: ", remote.tostring());
console.writeline(encoding.ascii.getstring(data,
0, recv));
while (true)
{
input
= console.readline();
if (input =="exit")
break;
server.sendto(encoding.ascii.getbytes(input), remote);
data
=newbyte[1024];
recv
= server.receivefrom(data, ref remote);
stringdata
= encoding.ascii.getstring(data, 0, recv);
console.writeline(stringdata);
}
console.writeline(
"stopping client.");
server.close();
}
}
}
先运行服务端,再运行客户端,否则会报错。通信实现了,下面我们要做的就是在这基础上完善我们的程序,我们的最终目的是用socket来传输文件。

转载于:https://www.cnblogs.com/_fyz/archive/2011/05/05/2037794.html

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是凯发k8官方网为你收集整理的c# socket编程第二篇的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图