C# Socket学习笔记一

Socket 初步操作

1.客户端

public void FirstMain(string[] args)
        {
            byte[] data = new byte[1024];
            //实例化socket对象
            Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //设置远程服务器的IP及端口
            Console.WriteLine("please input the server ip: ");
            string ipadd = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine("please input the server port: ");
            int port = Convert.ToInt32(Console.ReadLine());
            IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port); //实例化对象

            try
            {
                //进行连接
                newclient.Connect(ie);
            }
            catch (SocketException e)
            {
                Console.WriteLine("unable to connect to server...");
                Console.WriteLine(e.ToString());
                string kk = Console.ReadLine();
                return;
            }
            //接收远程服务器的数据
            int recv = newclient.Receive(data);
            string stringdata = Encoding.ASCII.GetString(data, 0, recv);//得到字符串
            Console.WriteLine(stringdata);

            //循环处理
            while (true)
            {
                string input = Console.ReadLine();
                if (input == "exit")
                {
                    break;
                }
                //发送数据到远程服务器
                newclient.Send(Encoding.ASCII.GetBytes(input));

                //设置缓冲区大小,进行数据接收
                data = new byte[1024];
                recv = newclient.Receive(data);
                stringdata = Encoding.ASCII.GetString(data, 0, recv);
                Console.WriteLine(stringdata);
            }
            Console.WriteLine("disconnect from server...");
            string kkt = Console.ReadLine();
            newclient.Shutdown(SocketShutdown.Both);
            newclient.Close();
        }

 

2.服务端

public void testMain(string[] args)
        {
            int recv;
            //设置数据缓冲区
            byte[] data = new byte[1024];
            //实例化IPEndPoint对象
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 19888);
            //实例化socket对象
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //进行绑定
            newsock.Bind(ipep);
            //设置监听客户端的连接数
            newsock.Listen(10);
            Console.WriteLine("waitting for a client......");
            //监听,进行连接
            Socket client = newsock.Accept();
            Console.WriteLine("waittingkkkk for a client......");
            //得到客户端连接的信息
            IPEndPoint clientip = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("connect with client:" + clientip.Address + " at port:" + clientip.Port);
            //构建数据,进行发送
            string welcome = "welcome here!";
            data = Encoding.UTF8.GetBytes(welcome);
            client.Send(data, data.Length, SocketFlags.None);//发送
            //循环处理
            while (true)
            {
                //进行数据的接收
                data = new byte[1024];
                recv = client.Receive(data);
                //在控制台显示数据
                Console.WriteLine("recv=" + recv);
                if (recv == 0)
                {
                    break;
                }
                Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
                //控制台输入数据,并进行发送
                string inputserver = Console.ReadLine();
                client.Send(Encoding.UTF8.GetBytes(inputserver));
            }
            Console.WriteLine("Disconnected from " + clientip.Address);
            client.Close();
            newsock.Close();
        }

posted @ 2012-12-26 13:57  LiGang  阅读(231)  评论(0编辑  收藏  举报