[学习笔记]C# Socket初试-客户端

来源:互联网 发布:linux shell是什么 编辑:程序博客网 时间:2024/06/08 02:02
namespace Client{    class Program    {        static void Main(string[] args)        {            try            {                //创建socket                Socket tcpClinet = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                //tcpClinet.Connect("192.168.0.109", 7788);                IPAddress ipaddress = IPAddress.Parse("192.168.0.109");                EndPoint point = new IPEndPoint(ipaddress, 7788);                tcpClinet.Connect(point);                Byte[] receiveBytes = new Byte[1024];                int length = tcpClinet.Receive(receiveBytes);                string message = Encoding.UTF8.GetString(receiveBytes, 0, length);                Console.WriteLine("Receive data:" + message);                string inputMessage = Console.ReadLine();                tcpClinet.Send(Encoding.UTF8.GetBytes(inputMessage));            }            catch (Exception e)            {                if (e.GetType() == typeof(System.Net.Sockets.SocketException))                {                    Console.WriteLine("无法连接服务端");                }            }        }    }}

0 0
原创粉丝点击