开发者

Problem with simple tcp\ip client-server

i'm trying to write simple tcp\ip client-server.

here is server code:

 internal class Program
    {
        private const int _localPort = 7777;

        private static void Main(string[] args)
        {
            TcpListener Listener;
            Socket ClientSock; 
            string data;
            byte[] cldata = new byte[1024]; 

            Listener = new TcpListener(_localPort);
            Listener.Start(); 
            Console.WriteLine("Waiting connections [" + Convert.ToString(_localPort) + "]...");
            try
            {
                ClientSock = Listener.AcceptSocket(); 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            int i = 0;

            if (ClientSock.Connected)
            {
                while (true)
                {
                    try
                    {
                        i = ClientSock.Receive(cldata); 
                    }
                    catch
                    {
                    }

                    try
                    {
                        if (i > 0)
                        {

                            data = Encoding.ASCII.GetString(cldata).Trim();

                            ClientSock.Send(cldata);
                        }
                    }
                    catch
                    {
                        ClientSock.Close(); 
                        Listener.Stop();
                        Console.WriteLine(
                            "Server closing. Reason: client offline. Type EXIT to quit the application.");
                    }
                }
            }
        }
    }

And here is client code:

void Main()
{
         string data; // Юзерская дата
            byte[] remdata ={ };
            TcpClient Client = new TcpClient();

            string ip = "127.0.0.1";
            int port = 7777;

            Console.WriteLine("\r\nConnecting to server...");
            try
            {
                Client.Connect(ip, port);
            }
            catch
            {
                Console.WriteLine("Cannot connect to remote host!");
                return;
            }
            Console.Write("done\r\nTo end, type 'END'");
            Socket Sock = Client.Client; 

            while (true) 
            {
                Console.Write("\r\n>");
                data = Console.ReadLine();
                if (data == "END")
  开发者_StackOverflow社区                  break;
                Sock.Send(Encoding.ASCII.GetBytes(data)); 
                Sock.Receive(remdata);
                Console.Write("\r\n<" + Encoding.ASCII.GetString(remdata));
            }

            Sock.Close();
            Client.Close();
}

When i'm sending to my server i cannt receive data back answer. Sock.Receive(remdata) returns nothing! Why?


You're trying to receive to an empty buffer. You should allocate the buffer with a sensible size, and then take note of the amount of data received:

byte[] buffer = new byte[1024];

...

int bytesReceived = socket.Receive(buffer);
string text = Encoding.ASCII.GetString(buffer, 0, bytesReceived);

(It's somewhat unconventional to use PascalCase for local variables, by the way. I'd also urge you not to just catch Exception blindly, and not to swallow exceptions without logging them.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜