????C#????????
?????????????????????????????ip???????EndPoint????
??????????????????Socket????
??????????????socket?????Bind()??????EndPoint??
?????????????socket?????Listen()?????????????
???????崠????????????????????socket?????Accept()?????????μ?socket????????????????????????;
?????????????????????????ù??socket
int port = 8889;
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(SocketType.Stream?? ProtocolType.Tcp);//???????socket?????????udpЭ?飬?????SocketType.Dgram??????????
s.Bind(ipe);//??EndPoint????2000????ip?????
s.Listen(0);//???????
Console.WriteLine("??????????????????{0}:{1}"??host??port);
///?????client????????????????μ?socket???????????
Socket temp = s.Accept();//????????????μ?socket
Console.WriteLine("????????");
string recvStr = "";
byte[] recvBytes = new byte[1024];
int 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();
????C#?????
?????????????????????????????ip???????EndPoint????
??????????????????Socket????
??????????????socket?????Connect()?????????潨????EndPoint????????????????????????????????
????????????????????????socket?????Send()????????????????????
???????崠????socket?????Receive()?????????????????????? ;
?????????????????????????ù??socket??
int port = 8889;
string host = "127.0.0.1";
///????????EndPoint
IPAddress ip = IPAddress.Parse(host);
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);//???????
///??????????????????
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes?? recvBytes.Length?? 0);//??????????????????
string recvStr = Encoding.ASCII.GetString(recvBytes?? 0?? bytes);
Console.WriteLine("client get message:{0}"?? recvStr);//????????????????
///???????????socket??????
c.Close();