view plainprint?
//Socket基本編程
//服務端
using System
using System
using System
using System
Thread mythread
Socket socket
// 清理所有正在使用的資源
protected override void Dispose( bool disposing )
{
try
{
socket
mythread
}
catch{ }
if( disposing )
{
if (components != null)
{
components
}
}
base
}
public static IPAddress GetServerIP()
{
IPHostEntry ieh=Dns
return ieh
}
private void BeginListen()
{
IPAddress ServerIp=GetServerIP()
IPEndPoint iep=new IPEndPoint(ServerIp
socket=new Socket(AddressFamily
byte[] byteMessage=new byte[
this
socket
// do
while(true)
{
try
{
socket
Socket newSocket=socket
newSocket
string sTime = DateTime
string msg=sTime+
msg+=newSocket
this
}
catch(SocketException ex)
{
this
}
}
// while(byteMessage!=null)
}
//開始監聽
private void button
{
try
{
mythread = new Thread(new ThreadStart(BeginListen))
mythread
}
catch(System
{
MessageBox
}
}
//客戶端
using System
using System
using System
private void button
{
BeginSend()
}
private void BeginSend()
{
string ip=this
string port=this
IPAddress serverIp=IPAddress
int serverPort=Convert
IPEndPoint iep=new IPEndPoint(serverIp
byte[] byteMessage
// do
// {
Socket socket=new Socket(AddressFamily
socket
byteMessage=Encoding
socket
socket
socket
// }
// while(byteMessage!=null)
}
基於TCP協議的發送和接收端
TCP協議的接收端
using System
using System
using System
int port =
private Thread thThreadRead
private TcpListener tlTcpListen
private bool blistener = true
private NetworkStream nsStream
private StreamReader srRead
private System
private System
private System
private TcpClient tcClient
private void Listen ( )
{
try
{
tlTcpListen = new TcpListener ( port )
tlTcpListen
statusBar
tcClient = tlTcpListen
nsStream = tcClient
srRead=new StreamReader(nsStream)
statusBar
while( blistener ) //循環偵聽
{
string sMessage = srRead
if ( sMessage ==
{
tlTcpListen
nsStream
srRead
statusBar
thThreadRead
return
}
string sTime = DateTime
listBox
}
}
catch ( System
{
MessageBox
}
}
//開始監聽
private void button
{
thThreadRead = new Thread ( new ThreadStart ( Listen ) )
thThreadRead
button
}
// 清理所有正在使用的資源
protected override void Dispose( bool disposing )
{
try
{
tlTcpListen
nsStream
srRead
thThreadRead
}
catch{}
if( disposing )
{
if (components != null)
{
components
}
}
base
}
TCP協議的發送端
using System
using System
using System
using System
private StreamWriter swWriter
private NetworkStream nsStream
private TcpClient tcpClient
private System
private System
private System
private System
private System
private System
private System
private bool tcpConnect = false
//連接
private void button
{
IPAddress ipRemote
try
{
ipRemote = IPAddress
}
catch //判斷給定的IP地址的合法性
{
MessageBox
return
}
IPHostEntry ipHost
try
{
ipHost = Dns
}
catch //判斷IP地址對應主機是否在線
{
MessageBox
return
}
string sHostName = ipHost
try
{
TcpClient tcpClient = new TcpClient(sHostName
nsStream = tcpClient
swWriter = new StreamWriter(nsStream)
button
button
tcpConnect = true
statusBar
}
catch
{
MessageBox
return
}
}
//發送
private void button
{
if (textBox
{
swWriter
swWriter
}
else
{
MessageBox
}
}
// 清理所有正在使用的資源
protected override void Dispose( bool disposing )
{
if ( tcpConnect )
{
swWriter
swWriter
nsStream
swWriter
}
if( disposing )
{
if (components != null)
{
components
}
}
base
}
資料引用
From:http://tw.wingwit.com/Article/program/net/201311/12468.html