locked
i cant receive pocket

    質問

  • hi guys

    i want write a software to manage internet bind width in our office . for test this i write to part software(server and client working by tcp on port 80).

    when i sent message from client to server it work but when i sent massage to ip out of my network server didnt receive.

    i set client gateway to server ip.

    server side code :

                 int servPort = 80;

                Socket server = null;

                try
                {
                    // Create a socket to accept client connections
                    server = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                                        ProtocolType.Tcp);

                    server.Bind(new IPEndPoint(IPAddress.Any, servPort));
                    server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
                    server.Listen(BACKLOG);
                }
                catch (SocketException se)
                {
                    Console.WriteLine(se.ErrorCode + ": " + se.Message);
                    Environment.Exit(se.ErrorCode);
                }

                Console.WriteLine("Server in Running ...");

                byte[] rcvBuffer = new byte[BUFSIZE]; // Receive buffer
                int bytesRcvd;                        // Received byte count

                for (; ; )
                { // Run forever, accepting and servicing connections

                    Socket client = null;
                    Console.WriteLine("Ready ... ");
                    try
                    {
                        client = server.Accept(); // Get client connection

                        Console.Write("Handling client at " + client.RemoteEndPoint + " - ");

                        // Receive until client closes connection, indicated by 0 return value
                        int totalBytesEchoed = 0;
                        while ((bytesRcvd = client.Receive(rcvBuffer, 0, rcvBuffer.Length,
                                                           SocketFlags.None)) > 0)
                        {
                            client.Send(rcvBuffer, 0, bytesRcvd, SocketFlags.None);
                            totalBytesEchoed += bytesRcvd;
                        }
                        Console.WriteLine("echoed {0} bytes.", totalBytesEchoed);

                        //client.Close();   // Close the socket. We are done with this client!

                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        //client.Close();
                    }

    2012年3月27日 6:03

すべての返信

  • To diagnose this, can you run a "netstat -nab" while trying to connect local and see if the addresses being used matches (e.g.: IPv4 or IPv6 address?)
    2012年3月28日 3:30