Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:51:00+00:00 2026-06-06T23:51:00+00:00

Table of Contents Preface Serverside Code Clientside Code Local Computer Example ( Successfull )

  • 0

Table of Contents

  • Preface
  • Serverside Code
  • Clientside Code
  • Local Computer Example (Successfull)
  • İnternet Network Example (Unsuccessfull)
  • My Possibility considerations

Preface

I take these codes from MSDN’s class library examples. So it must work correctly. Yes, I’m partially right. When running on my computer it succeed. But over internet network, it failes. I’m taking ConnectionRefused SocketErrorCode each time.
I’ll give source code below, initially, you must know my modified tests.

For Server Code:

  • I used IPAddress.IPv6Any instead of IPAddress.Any.
  • Also I tried without server.AllowNatTraversal(true)

But it failed

Serverside Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ServerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = null;
            try
            {
                int port = 13000;
                server = new TcpListener(IPAddress.Any, port);
                server.AllowNatTraversal(true);
                server.Start();
                byte[] bytes = new byte[256];
                string data = null;
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected: {0}", client.Client.LocalEndPoint.ToString());
                    data = null;
                    NetworkStream stream = client.GetStream();
                    int i;
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);
                        data = data.ToUpper();
                        byte[] msg = Encoding.ASCII.GetBytes(data);
                        stream.Write(msg, 0, msg.Length);
                        Console.WriteLine("Sent: {0}", data);
                    }
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Socket exception message: {0}", e.Message);
                Console.WriteLine("Socket Error Code: {0}", e.SocketErrorCode.ToString());
            }
            finally
            {
                server.Stop();
            }
            Console.WriteLine("\nHit enter to continue...");
            Console.ReadKey();            
        }
    }
}

Clientside Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Started...");
            Console.WriteLine("İP Address:");
            string hostName = Console.ReadLine();
            if(hostName.Length == 0)
            {
                hostName = Dns.GetHostName();
            }
            Connect(hostName, "hello");
        }

        static void Connect(string server, string message)
        {
            try
            {
                int port = 13000;
                TcpClient client = new TcpClient(server, port);
                byte[] data = Encoding.ASCII.GetBytes(message);
                NetworkStream stream = client.GetStream();
                stream.Write(data, 0, data.Length);
                Console.WriteLine("Sent: {0}", message);
                data = new byte[256];
                string responseData = string.Empty;
                int bytes = stream.Read(data, 0, data.Length);
                responseData = Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e.Message);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Socket Error Code: {0}", e.SocketErrorCode.ToString());
            }
            Console.WriteLine("Hit enter to continue...");
            Console.ReadKey();
        }
    }
}

Local Computer Example (Successfull)

I tried it only on my computer. First I opened ServerTest program then I ran ClientText program.

Clientside Output

Started…
İP Address:

Sent hello
Received HELLO
Hit enter to continue

Serverside Output

Waiting for a connection…
Connected: 192.168.1.3:13000
Received: hello
Sent: HELLO
Waiting for a connection

İnternet Network Example (Unsuccessfull)

I tried with my friend whose ip address is “88.226.0.218”. He learnt his ip address from whatismyipaddress.com I ran ClientTest program and he ran ServerTest program. Outputs are below:

Clientside Output

Started…
İP Address:
88.226.0.218
SocketErrorCode: ConnectionRefused
Hit enter to continue

Serverside Output

Waiting for a connection…

And nothing. ServerTest program only waits.

My Possibility Considerations

  • It can be using only İPv6 when I used İPAddress.İPv6Any
  • My modem firewall can be preventing NatTraversal. If so, how can I enable modem’s NatTraversal programatically?
  • AllowNatTraversal method of TcpListener class, have come with .Net4, doesn’t work properly. (I don’t believe, but for the sake of possibility)

Any help would be appreciated. This is very much important for me. I have been delaying my project almost for a month due to not succeeding this failure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:51:02+00:00Added an answer on June 6, 2026 at 11:51 pm

    One thing to try is a test between two computers on the same network. If that works, you can turn your attention away from your computer being the problem.

    In any case, the culprit is very likely firewalls, as Christopher said. I would actually check your friend’s firewall before your own, as some (most?) firewalls don’t hinder outgoing connections.

    In addition, if your friend is behind a router, you will need to have him forward port 13000 to his computer’s local address (most likely 192.168.1.x) on port 13000.

    More information (and guides!) on firewalls and port forwarding can be found here: http://portforward.com/help/start_here.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table contents many fields db.define_table('i2l_templates', Field('id','id', represent=lambda id:SPAN(A(T('View'),_href=URL('view_template',args=id)),' | ', A(T('Edit'),_href=URL('edit_template',args=id)))),
I want to convert the mysql database table contents to an Excel(.xls) or comma
Im having trouble making my table contents vertically aligned and was wondering if you
I added a UISwitch in a UITableViewCell, the table contents are dynamics, which means
DDMS views Database only in eclipse. I want to view table contents from CMD.
How to add table of contents to R Markdown HTML file using pandoc but
I have a table with contents that look similar to this: id | title
I'm trying to generate a table of contents from a block of HTML (not
I'm using rake to create a Table of contents from a bunch of static
I have a recursive function reading a table of contents of documents from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.