Visual C#.Net網路程式開發-Tcp篇(3)
2004-12-05 21:54
357 查看
綜合運用上面的知識,下面的實例實現了簡單的網路通訊-雙機互連,針對用戶端和服務端分別編制了應用程式。用戶端創建到服務端的連接,向遠端主機發送連接請求連接信號,並發送交談內容;遠端主機端接收來自用戶的連接,向用戶端發回確認連接的信號,同時接收並顯示用戶端的交談內容。在這個基礎上,發揮你的創造力,你完全可以開發出一個基於程式語言(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();
}
}
用戶端主要源代碼:
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();
}
}
相关文章推荐
- 我的此BLOGS停止更新。请到http://blog.yesky.com/Blog/foxleoly查看。
- java-Java的垃圾收集(Garbage Collection) (摘抄自网络)
- 宿主程序为windows服务的remoting
- 引自天天安全网 ——※ 中华民族传统文化精髓___二十四孝 ※
- XML技术上传文件
- 如何下載《水晶音樂網(http://music.36g.net)》上的歌曲?
- Winsock控件,实现网络点对点通信
- 用Java的New IO开发网络协议
- 网络最经典命令行--网络安全工作者的必杀技
- 网络聊天软件现状及展望
- 深度包检测技术简介
- NGNc首页更新,正式更名为"NGNc开发者网络"
- NGNc首页更新,正式更名为"NGNc开发者网络"
- 对UTF8编码的初步认识!
- OSGi技术简介(一)
- 《NGNc大事记》
- 《NGNc商务合作》
- NGNc“零移植”开发伙伴计划
- 网络的解释
- red hat 9装在虚拟机vmware上之后,连接网络是出现问题 Determining IP information for eth0... failed; no link present. Check cable?