說明:此樣本在.net2005"xp下運行通過
樣本程式是同步通訊端程式,功能很簡單,只是用戶端發給伺服器一條資訊,伺服器向用戶端返回一條資訊;這裡只是一個簡單的樣本,是一個最基本的socket編程流程,在接下來的文章中,會依次記錄通訊端的同步和非同步,以及它們的區別。
下面是樣本程式的簡單步驟說明
伺服器端:
第一步:用指定的連接埠號碼和伺服器的ip建立一個EndPoint對像;
第二步:建立一個Socket對像;
第三步:用socket對像的Bind()方法綁定EndPoint;
第四步:用socket對像的Listen()方法開始監聽;
第五步:接受到用戶端的串連,用socket對像的Accept()方法建立新的socket對像用於和請求的用戶端進行通訊;
第六步:通訊結束後一定記得關閉socket;
代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace server
{
class Program
{
static void Main(string[] args)
{
int port = 2000;
string host = "127.0.0.1";
///建立終結點(EndPoint)
IPAddress ip = IPAddress.Parse(host);//把ip地址字串轉換為IPAddress類型的執行個體
IPEndPoint ipe = new IPEndPoint(ip, port);//用指定的連接埠和ip初始化IPEndPoint類的新執行個體
///建立socket並開始監聽
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//建立一個socket對像,如果用udp協議,則要用SocketType.Dgram類型的通訊端
s.Bind(ipe);//綁定EndPoint對像(2000連接埠和ip地址)
s.Listen(0);//開始監聽
Console.WriteLine("等待用戶端串連");
///接受到client串連,為此串連建立新的socket,並接受資訊
Socket temp = s.Accept();//為建立串連建立新的socket
Console.WriteLine("建立串連");
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//從用戶端接受資訊
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
///給client端返回資訊
Console.WriteLine("server get message:{0}", recvStr);//把用戶端傳來的資訊顯示出來
string sendStr = "ok!Client send message successful!";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
temp.Send(bs, bs.Length, 0);//返回資訊給用戶端
temp.Close();
s.Close();
Console.ReadLine();
}
}
}
server結果:
用戶端:
第一步:用指定的連接埠號碼和伺服器的ip建立一個EndPoint對像;
第二步:建立一個Socket對像;
第三步:用socket對像的Connect()方法以上面建立的EndPoint對像做為參數,向伺服器發出串連請求;
第四步:如果串連成功,就用socket對像的Send()方法向伺服器發送資訊;
第五步:用socket對像的Receive()方法接受伺服器發來的資訊 ;
第六步:通訊結束後一定記得關閉socket;
代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client
{
class Program
{
static void Main(string[] args)
{
try
{
int port = 2000;
string host = "127.0.0.1";
///建立終結點EndPoint
IPAddress ip = IPAddress.Parse(host);
//IPAddress ipp = new IPAddress("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和連接埠轉化為IPEndpoint執行個體
///建立socket並串連到伺服器
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//建立Socket
Console.WriteLine("Conneting…");
c.Connect(ipe);//串連到伺服器
///向伺服器發送資訊
string sendStr = "hello!This is a socket test";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);//把字串編碼為位元組
Console.WriteLine("Send Message");
c.Send(bs, bs.Length, 0);//發送資訊
///接受從伺服器返回的資訊
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);//從伺服器端接受返回資訊
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("client get message:{0}", recvStr);//顯示伺服器返回資訊
///一定記著用完socket後要關閉
c.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("argumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException:{0}", e);
}
Console.WriteLine("Press Enter to Exit");
}
}
}
Client端結果:
Trackback:http://www.cnblogs.com/sopper/archive/2007/01/23/627924.aspx