Visual C#.Net网络程序设计-Tcp篇(3)

综合运用上面的知识,下面的实例实现了简单的网络通讯-双机互连,针对客户端和服务端分别编制了应用程序。客户端创建到服务端的连接,向远程主机发送连接请求连接信号,并发送交谈内容;远程主机端接收来自客户的连接,向客户端发回确认连接的信号,同时接收并显示客户端的交谈内容。在这个基础上,发挥你的创造力,你完全可以开发出一个基于程序语言(C#)级的聊天室!

客户端主要源代码:

public void SendMeg()//发送信息
{
try
{

int port=Int32.Parse(textBox3.Text.ToString());//远程主机端口
try
{
tcpClient=new TcpClient(textBox1.Text,port);//创建TcpClient对象实例 }
catch(Exception le)
{
MessageBox.Show("TcpClient Error:"+le.Message);
}
string strDateLine=DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToLongTimeString();//得到发送时客户端时间
netStream=tcpClient.GetStream();//得到网络流
sw=new StreamWriter(netStream);//创建TextWriter,向流中写字符
string words=textBox4.Text;//待发送的话
string content=strDateLine+words;//待发送内容
sw.Write(content);//写入流
sw.Close();//关闭流写入器
netStream.Close();//关闭网络流
tcpClient.Close();//关闭客户端连接
}
catch(Exception ex)
{
MessageBox.Show("Sending Message Failed!"+ex.Message);
}
textBox4.Text="";//清空
}


服务器端主要源代码:

public void StartListen()//侦听特定端口的用户请求
{
//ReceiveMeg();
isLinked=false; //连接标志
try
{
int port=Int32.Parse(textBox1.Text.ToString());//本地待侦听端口
serverListener=new TcpListener(port);//创建TcpListener对象实例
serverListener.Start(); //启动侦听
}
catch(Exception ex)
{
MessageBox.Show("Can't Start Server"+ex.Message);
return;
}
isLinked=true;
while(true)//进入无限循环等待用户端连接
{
try
{
tcpClient=serverListener.AcceptTcpClient();//创建客户端连接对象
netStream=tcpClient.GetStream();//得到网络流
sr=new StreamReader(netStream);//流读写器
}
catch(Exception re)
{
MessageBox.Show(re.Message);
}
string buffer="";
string received="";
received+=sr.ReadLine();//读流中一行
while(received.Length!=0)
{
buffer+=received;
buffer+="/r/n";
//received="";
received=sr.ReadLine();
}
listBox1.Items.Add(buffer);//显示
//关闭
sr.Close();
netStream.Close();
tcpClient.Close();
}
}    
非常好用的C#.netTCP控件,this.vmTcpIpServer1.Collapse = false; this.vmTcpIpServer1.EnableLog = false; this.vmTcpIpServer1.IdleTime = -1; this.vmTcpIpServer1.LocalUsingIpAddr = "127.0.0.1"; this.vmTcpIpServer1.Location = new System.Drawing.Point(9, 17); this.vmTcpIpServer1.LogFilePath = "D:\\AppLog"; this.vmTcpIpServer1.MaxLogShownLines = 30; this.vmTcpIpServer1.Name = "vmTcpIpServer1"; this.vmTcpIpServer1.PackageHeader = UNYC.TcpIp.PackageHeader.None; this.vmTcpIpServer1.PackageTailer = UNYC.TcpIp.PackageTailer.None; this.vmTcpIpServer1.PortNum = 30000; this.vmTcpIpServer1.SaveToLogFile = false; this.vmTcpIpServer1.ShowTransContents = false; this.vmTcpIpServer1.Size = new System.Drawing.Size(266, 405); this.vmTcpIpServer1.TabIndex = 0; // // vmTcpIpClient1 // this.vmTcpIpClient1.AutoRecover = false; this.vmTcpIpClient1.Collapse = false; this.vmTcpIpClient1.ConnRetries = -1; this.vmTcpIpClient1.EnableLog = false; this.vmTcpIpClient1.IdleTime = -1; this.vmTcpIpClient1.IpAddr = "192.168.100.231"; this.vmTcpIpClient1.Location = new System.Drawing.Point(311, 17); this.vmTcpIpClient1.LogFilePath = "D:\\AppLog"; this.vmTcpIpClient1.MaxLogShownLines = 100; this.vmTcpIpClient1.Name = "vmTcpIpClient1"; this.vmTcpIpClient1.PackageHeader = UNYC.TcpIp.PackageHeader.None; this.vmTcpIpClient1.PackageTailer = UNYC.TcpIp.PackageTailer.None; this.vmTcpIpClient1.PingInterval = 500; this.vmTcpIpClient1.PortNum = 912815; this.vmTcpIpClient1.SaveToLogFile = false; this.vmTcpIpClient1.ShowTransContents = false; this.vmTcpIpClient1.Size = new System.Drawing.Size(266, 405);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值