TCP套接字练习

TCP套接字练习

ID:37713128

大小:33.00 KB

页数:5页

时间:2019-05-29

TCP套接字练习_第1页
TCP套接字练习_第2页
TCP套接字练习_第3页
TCP套接字练习_第4页
TCP套接字练习_第5页
资源描述:

《TCP套接字练习》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、此为控制台程序,服务器启动侦听,客户端进行连接,并向服务器发送字符串,服务器将其转化为大写并发挥客户机(在同一台主机上运行)。服务器端usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;namespaceSynTCPServer{classProgram{staticvoidMain(string[]args){SocketListener=newSocket(AddressFamily.InterNetwor

2、k,SocketType.Stream,ProtocolType.Tcp);//IPAddressmyip=Dns.GetHostAddresses(Dns.GetHostName())[1];//Stringstr=Dns.GetHostName();//Console.WriteLine(str);//Console.WriteLine(myip);//foreach(IPAddressipinmyip)//{//Console.WriteLine(ip);//}Console.Read();IPEndPointiep=newIPEndPoint(IPAddress.Parse("127

3、.0.0.1"),5555);Listener.Bind(iep);Listener.Listen(10);Console.WriteLine("等待连接");Socketsocket=Listener.Accept();Console.WriteLine("连接成功,等待接受数据");Byte[]buffer=newbyte[128];intcount=socket.Receive(buffer,0,buffer.Length,SocketFlags.None);Console.WriteLine("成功接受数据");StringstrRcv=Encoding.ASCII.GetStrin

4、g(buffer,0,count);stringstrsnt=strRcv.ToUpper();buffer=Encoding.ASCII.GetBytes(strsnt);count=socket.Send(buffer,0,buffer.Length,SocketFlags.None);Console.WriteLine("发送数据");socket.Shutdown(SocketShutdown.Both);socket.Close();Listener.Close();Console.Read();}}}客户端usingSystem;usingSystem.Collections.G

5、eneric;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;namespaceSynTcpclient{classProgram{staticvoidMain(string[]args){Socketsocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPEndPointRmtEP=newIPEndPoint(IPAddress.Parse("127.0.0.1"),5555);soc

6、ket.Connect(RmtEP);Console.WriteLine();StringStrInput=Console.ReadLine();Byte[]data=Encoding.ASCII.GetBytes(StrInput);socket.Send(data,0,data.Length,SocketFlags.None);Int32count=socket.Receive(data,0,data.Length,SocketFlags.None);StringstrRcv=Encoding.ASCII.GetString(data,0,count);Console.WriteLine

7、(strRcv);socket.Shutdown(SocketShutdown.Both);socket.Close();Console.Read();}}}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。