首页 > 代码库 > C#中使用TCP通信
C#中使用TCP通信
TCP通信需要通信双方都在线,所以需要先启动服务端进行监听,客户端才能获得连接,服务端代码:
static void Main(string[] args) { TcpClient client = null; NetworkStream stream = null; byte[] buffer = null; string receiveString = null; IPAddress localIP = IPAddress.Parse("127.0.0.1"); int localPort = 11000; TcpListener listener = new TcpListener(localIP, localPort);//用本地IP和端口实例化Listener listener.Start();//开始监听 while (true) { client = listener.AcceptTcpClient();//接受一个Client buffer = new byte[client.ReceiveBufferSize]; stream = client.GetStream();//获取网络流 stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据 stream.Close();//关闭流 client.Close();//关闭Client receiveString = Encoding.Default.GetString(buffer).Trim(‘\0‘);//转换成字符串 Console.WriteLine(receiveString); } }
只有服务端开启监听后,客户端才能正确连接,所以服务端要一直开启监听,客户端每次发送数据,都要首先与服务端建立连接,连接建立完成后才进行数据发送。客户端代码:
static void Main(string[] args) { string sendString = null;//要发送的字符串 byte[] sendData = http://www.mamicode.com/null;//要发送的字节数组 TcpClient client = null;//TcpClient实例 NetworkStream stream = null;//网络流 IPAddress remoteIP = IPAddress.Parse("127.0.0.1");//远程主机IP int remotePort = 11000;//远程主机端口 while (true)//死循环 { sendString = Console.ReadLine();//获取要发送的字符串 sendData = http://www.mamicode.com/Encoding.Default.GetBytes(sendString);//获取要发送的字节数组 client = new TcpClient();//实例化TcpClient try { client.Connect(remoteIP, remotePort);//连接远程主机 } catch (System.Exception ex) { Console.WriteLine("连接超时,服务器没有响应!");//连接失败 Console.ReadKey(); return; } stream = client.GetStream();//获取网络流 stream.Write(sendData, 0, sendData.Length);//将数据写入网络流 stream.Close();//关闭网络流 client.Close();//关闭客户端 } }
<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1401215697&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3754968.html&dt=1401215700520&shv=r20140520&cbv=r20140417&saldr=sb&correlator=1401215700098&frm=20&ga_vid=1304086684.1400769066&ga_sid=1401204879&ga_hid=1574041340&ga_fc=1&u_tz=480&u_his=474&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=31496&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=U9OBmcSFYp&p=http%3A//www.cnblogs.com&dtd=48" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1401215697&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3754968.html&dt=1401215700576&shv=r20140520&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1401215700098&frm=20&ga_vid=1304086684.1400769066&ga_sid=1401204879&ga_hid=1574041340&ga_fc=1&u_tz=480&u_his=474&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=31746&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=avKqp8BuVr&p=http%3A//www.cnblogs.com&dtd=50" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。