- using System;
- using Sy;
- using Sy;
- using Sy;
- using Sy.Sockets;
- namespace Client
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- int port = 2000;
- string host = "127.0.0.1";
- ///创建终结点EndPoint
- IPAddress ip = IPAddre(host);
- //IPAddress ipp = new IPAddress("127.0.0.1");
- IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndpoint实例
-
- ///创建Socket并连接到服务器
- Socket c = new Socke, Socke, Pro);//创建Socket
- Con("Conneting…");
- c.Connect(ipe);//连接到服务器
-
- ///向服务器发送信息
- string sendStr = "hello!This is a socket test";
- byte[] bs = Encoding.ASCII.Getbytes(sendStr);//把字符串编码为字节
- Con("Send Message");
- c.Send(bs, bs.Length, 0);//发送信息
-
- ///接受从服务器返回的信息
- string recvStr = "";
- byte[] recvBytes = new byte[1024];
- int bytes;
- bytes = c.Receive(recvBytes, recvBy, 0);//从服务器端接受返回信息
- recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
- Con("client get message:{0}", recvStr);//显示服务器返回信息
- ///一定记着用完socket后要关闭
- c.Close();
- }
- catch (ArgumentNullException e)
- {
- Con("argumentNullException: {0}", e);
- }
- catch (SocketException e)
- {
- Con("SocketException:{0}", e);
- }
- Con("Press Enter to Exit");
- }
- }
- }