0

For some reason I am having a hard time sending and receiving data from server. Anyways here is my client code:

 int recv = server.Receive(data);
    stringData = Encoding.ASCII.GetString(data, 0, recv);
    Console.WriteLine(stringData);

    while (true)
    {
        input = Console.ReadLine();
        if (input == "exit")
            break;
        server.Send(Encoding.ASCII.GetBytes(input));
        data = new byte[1024];
        recv = server.Receive(data);
        stringData = Encoding.ASCII.GetString(data, 0, recv);
        Console.WriteLine(stringData);
    }
    Console.WriteLine("Disconnecting from server...");
    server.Shutdown(SocketShutdown.Both);
    server.Close();

Basically I want to use get some properties about network. I know how to get The properties but it's not work when I try to use it with client.

Here is the server code :

    while (true)
    {
        data = new byte[1024];
        recv = client.Receive(data);
        if (recv == 0)
            break;

        Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
        client.Send(data, recv, SocketFlags.None);
    }

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

here is my properties code:

 ("SettingID: " + mo[0]["SettingID"]
  ("DNSHostName: " + mo[0]["DNSHostName"]
   Console.WriteLine("new Blocking: {0}",test.Blocking);
  Console.WriteLine("Connected: {0}", test.Connected);

  test.Bind(ie);
  IPEndPoint iep = (IPEndPoint)test.LocalEndPoint;
  Console.WriteLine("Local EndPoint: {0}",
                  iep.ToString());
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • check [this](http://stackoverflow.com/questions/34586733/sending-a-value-from-server-to-client-with-sockets/34669446#34669446) for quick testing using TCP server-client. What you send can be adjusted. – Ian Mar 02 '16 at 05:31
  • 1) Don't use raw sockets. Use `TcpClient` and `TcpListener`; 2) TCP is connection-based - you connect, and then you have a bi-directional stream of data; 3) TCP is stream-based, not message-based. You need to add framing to your "messages", and you need to read them properly. Writing your own TCP protocol isn't exactly easy - you might want to consider something a bit dev-friendlier, like WCF. If you *need* to go low-level, read a book. It's quite a complex topic. – Luaan Mar 02 '16 at 07:07

0 Answers0