示例#1
0
        public static void Client()
        {
            int sent;

            byte[] data = new byte[1024];
            string input, stringData;

            IPEndPoint ipep = new IPEndPoint(
                IPAddress.Parse("127.0.0.1"), 9050);

            Socket server = new Socket(AddressFamily.InterNetwork,
                                       SocketType.Stream, ProtocolType.Tcp);

            try
            {
                server.Connect(ipep);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Unable to connect to server.");
                Console.WriteLine(e.ToString());
                return;
            }

            data = SenderReciever.SendRecieve.RecieveData(server);

            stringData = Encoding.ASCII.GetString(data);
            Console.WriteLine(stringData);

            string message1 = "First Message Check";

            sent = SendRecieve.SendData(server, Encoding.ASCII.GetBytes(message1));

            while (true)
            {
                string Message = Console.ReadLine();
                SendRecieve.SendData(server, Encoding.ASCII.GetBytes(Message));

                if (Message == "exit")
                {
                    break;
                }
            }
            Console.WriteLine("Disconnecting from server...");
            server.Shutdown((SocketShutdown.Both));
            server.Close();
        }
示例#2
0
        public static void Srv()
        {
            int recv;

            byte[]     data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
                                             9050);

            Socket newsock = new Socket(AddressFamily.InterNetwork,
                                        SocketType.Stream, ProtocolType.Tcp);

            newsock.Bind(ipep);
            newsock.Listen(10);
            Console.WriteLine("Waiting for a user");
            Socket     client   = newsock.Accept();
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;

            Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);
            string welcome = "Server is active";

            data = Encoding.ASCII.GetBytes(welcome);
            int sent = SendRecieve.SendData(client, data);

            while (true)
            {
                data = SendRecieve.RecieveData(client);
                Console.WriteLine(Encoding.ASCII.GetString(data));
                if (Encoding.ASCII.GetString(data) == "exit")
                {
                    break;
                }
            }

            Console.WriteLine("Disconnected from {0}", clientep.Address);
            client.Close();
            newsock.Close();
        }