none
Microsoft TcpClient Class (stock example) not working. Always getting error: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. RRS feed

  • Question

  • I am using the exact Microsoft example for TcpClient (http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx) and yet it is not working.

    I continue to get this error (Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.) at this line: 

    Dim bytes As Int32 = stream.Read(data, 0, data.Length)

    I know there are many very similar questions (I went through a lot) but I have not gotten anything conclusive. I did get the notion that it may have something to do with the client/server disconnecting to early. I find it interesting  that I'm having problems, this is an official Microsoft example, shouldn't it work flawlessly?

    Any help would be very much appreciated, thank you.

     

    Wednesday, October 23, 2013 3:15 AM

All replies

  • TCP can be very confusing for novices.  There are many rules that must be followed.  There are 4 different usages that isn't obvious.

         1) Chat session - Server

         2) Chat session - Client

         3) Command and Control - Server

         4) Command and Control - Client.

    I won't discuss right now the differences between a Chat session and a command and control because it doesn't make a diffeences when sending one message.

    The term client has two meanings that is often confused.  The client is the socket which can be used either at the Server application level or Client application level.  The VS class TCPClient is a socket which can be used for both the client and server application.

    1) A server appliction gets started first.  It connects to the loopback address of the local computer which is IPany (127.0.0.1) or the computer name or IP address.  It waits for a client to connect.

    2) The client application uses connect method and used the server IP address or name, not the loopback address IPany or 127.0.0.1.

    You are making a connection which has three properties

         a) Source IP address

         b) Destination IP address

         c) Port number

    Noramally the error "An existing connection was forcibly closed by the remote host" is caused by a client application when the client makes a connection and uses the loopback address 127.0.0.1 as the source IP address.  The VS library produces this error because it doesn't make any sense for the Source IP address to be the loopback.  The issue is the server appplication needs a real IP address to return messages back to the client.  The internet standard requires every device to have a loopback address 127.0.0.1.  If a server attempts to send a message to the client with IP address 127.0.0.1 the message will incorrectly end up going back to the server.


    jdweng

    Thursday, October 31, 2013 7:08 AM
  • @Joel Engineer So how can I fix the problem? I have not changed anything in either the server or client codes.
    Thursday, October 31, 2013 7:23 PM
  • @Diana191 Thank you for the answer. Unfortunately nothing there helped me.
    Thursday, October 31, 2013 7:29 PM
  • The TCP Client shown is meant for a client application.  For the sample code to work without an exception two things have to happen

    1) A server has to be listening to the port 13000

    2) There can't be another application using port 13000.

    The server code is different from  client code.  A server must listen to a local IP address.  Usually the server listens at port 127.0.0.1 which is called the loopback address.  The Net library sometimes referes to the loopback as IPany.  The server can also listen at the IP address (or computer name)of the computer the code is running on.  The server must always start before the client.  The TCPClient class can be used for both the server and client application.  The IP address is just different.  The server uses a local IP address while the client connects to the server IP address (not the loopback).


    jdweng

    Thursday, October 31, 2013 8:42 PM
  • @Joel Engineer Im sorry I forgot to mention that I am also using the exact Microsoft example for TcpListener (http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx).
    Thursday, October 31, 2013 11:16 PM
  • You did say that you were using the exact code and that is why I'm not jumping to the conclusion that you have a firewall issue.  I was a network engineer doing beta testing on network software.  One of my jobs was to reconfigure our test netwrok to various network configuration. to make sure the software worked properly.  The error you are getting was one of the most common errors I would get and there were a 1000 different reasons why the error would occur.

    As I wrote the other day to somebody your have to learn to walk before you run and right now you are trying to break the 4 minute mile before you take your 1st step.  I know nothing about your network configuration nor the IP addresses you are using.

    If you want to test the code you posted here is a simple way of performing the test

    1) Stop all  your network applications

    2) On the PC you are using open a hyperterminal program.  It is usually found  at : Start - All Programs - Accessories - Communications - hyperterminal.  there are a few different versions of the program and the menu options are slightly different with each version

             a) Enter any name for network name

             b) Select for connect using TCP/IP (winsock)

             c) Use for host address 127.0.0.1

             d) Select the same port number as your program 13000

              e) You will get an error message saying unable to connect

              f) Go to Call menu and select wait for call

    Ther hyper terminal is now configured as a server in listening mode.

    3) Now start your program which is a client.  Select for the IP address the computer you are using or the IP address of the computer (don't use 127.0.0.1 nor IPany).  Your application should connect to the hyperterminal.  The write message in your program will appear in the hyperterminal window.


    jdweng

    Friday, November 1, 2013 6:23 AM
  • @Joel Engineer Thank you for you response. I followed the steps and the message was received (Firewall did require me to allow the hyperterminal program). But when I used my TcpListener program with the same settings (server, port) instead of the hyperterminal, I got the same error. I have also tried disabling my Firewall, 

    So in the end the situation should be pretty simple: I'm using the official Microsoft example for TcpClient, and I'm using the official Microsoft example for TcpListener. I have the TcpClient connecting to my IP address using port 13000, attempting to send a simple text message, and I have the TcpListener listening to 127.0.0.1 on port 13000. The TcpClient program works fine with the hyperterminal program but not with my TcpListener program (I have not made any changes to the official TcpListener example). And lastly Firewall cannot be the problem because disabling it doesn't help. Perplexing.

    Friday, November 1, 2013 5:42 PM
  • I have seen too many different causes of connections failing to immediately jump to the conclusion that a firewall is blocking the connection.  Although firewall these days seem to be the major cause of the problems.

    I would put a break point in your server code after the start() method to see if the connection is completing.  I don't see any reason why the posted listener should connect provided you start the listener before the client.  If the listener wasn't working thebn you should of gotten ax exception in the client code.  I think the problem in the server is occuring after the connection completes.


    jdweng

    Friday, November 1, 2013 6:55 PM
  • It seems the client never ends up connecting to the server even though the error seems to imply that it did, if it connected shouldn't the line

    Console.WriteLine("Connected!")

    execute? And yes I am starting the server before the client. Just to clarify, the error is from the client.

    Is it possible the client is asking for a response (or doing something else) before the connection is completed? Maybe I should use a thread.sleep somewhere?

    Thank you very much for you help.

    Sunday, November 3, 2013 1:05 AM
  • Were you gettting on the server the message wating for a connection and then on th esame line connected?  I took another look at the posted server code and saw something I didn't like.  there was no "namespace" yet there was a main() function.  The main function in the posted code didn't have any arguements.   I suspect that your code has two main() functions.  the main function with an arugument list is running and not your code without any arguements.

    The posted code isn't correct because a console application shold have a namespace.  I'm thinking the code was never executing.  So I rewrote the server code and tested the server code by opening up a hyperterminal and using "localhost" as the IP name.  The code below worked.

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpListener server = null;
                try
                {
                    // Set the TcpListener on port 13000.
                    Int32 port = 13000;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                    // TcpListener server = new TcpListener(port);
                    server = new TcpListener(localAddr, port);
                    // Start listening for client requests.
                    server.Start();
                    // Buffer for reading data
                    Byte[] bytes = new Byte[256];
                    String data = null;
                    // Enter the listening loop. 
                    while (true)
                    {
                        Console.Write("Waiting for a connection... ");
                        // Perform a blocking call to accept requests. 
                        // You could also user server.AcceptSocket() here.
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");
                        data = null;
                        // Get a stream object for reading and writing
                        NetworkStream stream = client.GetStream();
                        int i;
                        // Loop to receive all the data sent by the client. 
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            Console.WriteLine("Received: {0}", data);
                            // Process the data sent by the client.
                            data = data.ToUpper();
                            byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                            // Send back a response.
                            stream.Write(msg, 0, msg.Length);
                            Console.WriteLine("Sent: {0}", data);
                        }
                        // Shutdown and end connection
                        client.Close();
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                finally
                {
                    // Stop listening for new clients.
                    server.Stop();
                }
                Console.WriteLine("\nHit enter to continue...");
                Console.Read();
            }
        }
    }


    jdweng

    Sunday, November 3, 2013 6:14 AM
  • I tested the original server code without any changes (VB.NET) and it actually did work with the hyperterminal. So both the server and client work with the hyperterminal but not with each other.

    Did you consider my previous suggestion? The fact that both the server and client codes work with the hyperterminal but not with each other seems to confirm that the problem is with the interaction of the two codes, and not with one specific code. So maybe either the client or the server is doing something before the other is finished doing whatever its doing?

    Sunday, November 3, 2013 6:05 PM
  • I ran both the client and server together and the only changes I made where to the client as shown below.  The client didn't have a main() and I removed "shared" from the function name.  I'm also using "localhost" as the server name.

    Imports System.Net
    Imports System.Net.Sockets
    Module Module1
        Sub Main()
            Connect("localHost", "Hello Joel")
        End Sub
        Sub Connect(ByVal server As String, ByVal message As String)
            Try
                ' Create a TcpClient. 
                ' Note, for this client to work you need to have a TcpServer  
                ' connected to the same address as specified by the server, port 
                ' combination. 
                Dim port As Int32 = 13000
                Dim client As New TcpClient(server, port)
                ' Translate the passed message into ASCII and store it as a Byte array. 
                Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
                ' Get a client stream for reading and writing. 
                '  Stream stream = client.GetStream(); 
                Dim stream As NetworkStream = client.GetStream()
                ' Send the message to the connected TcpServer. 
                stream.Write(data, 0, data.Length)
                Console.WriteLine("Sent: {0}", message)
                ' Receive the TcpServer.response. 
                ' Buffer to store the response bytes.
                data = New [Byte](256) {}
                ' String to store the response ASCII representation. 
                Dim responseData As [String] = [String].Empty
                ' Read the first batch of the TcpServer response bytes. 
                Dim bytes As Int32 = stream.Read(data, 0, data.Length)
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
                Console.WriteLine("Received: {0}", responseData)
                ' Close everything.
                stream.Close()
                client.Close()
            Catch e As ArgumentNullException
                Console.WriteLine("ArgumentNullException: {0}", e)
            Catch e As SocketException
                Console.WriteLine("SocketException: {0}", e)
            End Try
            Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
            Console.Read()
        End Sub 'Connect
    End Module


    jdweng

    Sunday, November 3, 2013 10:00 PM
  • I tested the client and server as you suggested, except I only changed the server to "localHost", and it worked. But when I tried using my actual IP address I got that same error. Shouldn't "localHost" and my IP address have the same result? I also tried using 127.0.0.1, and got the same error.

    I have port forwarding setup so that anything sent to my IP address on port 13000 gets directed to my computer. I was under the impression that localHost was equivalent to 127.0.0.1. So anything sent to my IP address on port 13000 should end up having the same result as localHost, no?

    Monday, November 4, 2013 5:54 PM
  • How many ethernet cards/adapters do you have on the PC.  You can ping localhaost to see what IP address it is set to.  On some PC is is the loopback address and others it is the IP addrress of the computer.  There is a host file which is text in the folder below that determines the IP address of localhost.

    C:\WINDOWS\system32\drivers\etc


    jdweng

    Monday, November 4, 2013 6:27 PM
  • I have only the basics: one ethernet and one wireless. according to the hosts file localHost is 127.0.0.1. So why does only "localHost" work and not my IP address (or 127.0.0.1) if they are the same?
    Monday, November 4, 2013 7:19 PM
  • So you ping local host and got 127.0.0.1? The wireless device is  equivalent to an ethernet card and has a IP address.  I need to see the the results of the IP address and mask for the two IP address of your computer.  Type the followng from a cmd.exe window

    IPCONFIG /ALL > c:\temp\abc.txt      or any file name

    I suspect you network cards are not configured properly.  One IP address mask should b e 0.0.0.0 which is the default gateway which should be on the IP address which goes towards the back bone and leads to your internet provider.  The other IP address should be more strictive and to form a subnet like a mask of 255.255.255.0 which will ojnly let 256 IP address to pass which is refered to as a 12 bit mask.  usally mask are the foolwoing

    1) 0.0.0.0   - zero bit mask

    2) 128.0.0.0  - one bit mask

    3) 192.0.0.0  - two bit mask

    4) 224.0.0.0  - three bit mask.

    5) 240.0.0.0  -  four bit mask

    6) 248.0.0.0 - five bit mask

    7) 252.0.0.0 - six bit mask

    8) 254.0.0.0 - seven bit mask

    9) 255.0.0.0  - eight bit mask

    10) 255.128.0.0

    11) 255.192.0.0

    12) 255.224.0.0

    13) 255.240.0.0

    14) 255.248.0.0

    15) 255.252.0.0

    16) 255.254.0.0

    17) 255.255.0.0

    18) 255.255.128.0

    19) 255.255.192.0

    20) 255.255.224.0

    21) 255.255.240.0

    22) 255.255.248.0

    23) 255.255.252.0

    24) 255.255.254.0

    25) 255.255.255.0

    26) 255.255.255.128

    27) 255.255.255.192

    28) 255.255.255.224

    29) 255.255.255.240

    30) 255.255.255.248 - allows 8 address to pass

    31) 255.255.255.252 - allows 4 address to pass

    32) 255.255.255.254  - allows 2 address to pass

    33) 255.255.255.255   - allow one address to pass  - 16 bit mask


    jdweng


    Monday, November 4, 2013 10:16 PM
  • Not exactly. I pinged localHost and it said it was pinging the name of the computer.

    The result of running "ipconfig /all" is:

    Windows IP Configuration
    
       Host Name . . . . . . . . . . . . : Bubby
       Primary Dns Suffix  . . . . . . . :
       Node Type . . . . . . . . . . . . : Hybrid
       IP Routing Enabled. . . . . . . . : No
       WINS Proxy Enabled. . . . . . . . : No
       DNS Suffix Search List. . . . . . : lan
    
    Wireless LAN adapter Local Area Connection* 11:
    
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
       Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
       Physical Address. . . . . . . . . : 48-D2-24-49-CD-E7
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
    
    Wireless LAN adapter Wi-Fi:
    
       Connection-specific DNS Suffix  . : lan
       Description . . . . . . . . . . . : Realtek RTL8188E Wireless LAN 802.11n PCI
    -E NIC
       Physical Address. . . . . . . . . : 48-D2-24-49-CD-E7
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
       Link-local IPv6 Address . . . . . : fe80::8c29:a291:1884:1b74%4(Preferred)
       IPv4 Address. . . . . . . . . . . : 192.168.0.100(Preferred)
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Lease Obtained. . . . . . . . . . : Sunday, November 3, 2013 8:51:26 PM
       Lease Expires . . . . . . . . . . : Monday, November 11, 2013 5:40:03 PM
       Default Gateway . . . . . . . . . : 192.168.0.1
       DHCP Server . . . . . . . . . . . : 192.168.0.1
       DHCPv6 IAID . . . . . . . . . . . : 357093924
       DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-82-27-32-00-8C-FA-68-5A-A5
    
       DNS Servers . . . . . . . . . . . : 192.168.0.1
       NetBIOS over Tcpip. . . . . . . . : Enabled
    
    Ethernet adapter Ethernet:
    
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : lan
       Description . . . . . . . . . . . : Qualcomm Atheros AR8161 PCI-E Gigabit Eth
    ernet Controller (NDIS 6.30)
       Physical Address. . . . . . . . . : 00-8C-FA-68-5A-A5
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
    
    Tunnel adapter Local Area Connection* 4:
    
       Connection-specific DNS Suffix  . :
       Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
       IPv6 Address. . . . . . . . . . . : 2001:0:5ef5:79fb:34e7:1529:bc19:6acb(Pref
    erred)
       Link-local IPv6 Address . . . . . : fe80::34e7:1529:bc19:6acb%9(Preferred)
       Default Gateway . . . . . . . . . : ::
       DHCPv6 IAID . . . . . . . . . . . : 150994944
       DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-82-27-32-00-8C-FA-68-5A-A5
    
       NetBIOS over Tcpip. . . . . . . . : Disabled
    
    Tunnel adapter isatap.lan:
    
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : lan
       Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes

    Tuesday, November 5, 2013 12:39 AM
  • There is a lot of very useful information in the data you posted.  I don't know much about the computer IP address you are trying to connect to with your  VS application.  But here is what I do know from your last posting.

    1) The ethernet interface is disabled

    2) The name of you computer is Bubby

    3) You computer IP address is 192.168.0.100

    4) Since the ethernet interface is disabled the only IP address you have routes for is 192.168.0.0 to 192.168.0.255 which is the IP address with the mask of 255.255.255.0.  This range of address is the local subnet.  Yo uwill only be able to connect to computers in the subnet by IP address.  If you want all IP address to go out the wireless interface than you need to change the mask to the default gateway using a mask of 0.0.0.0.

    5) You have DNS enabled with the DNS server of 192.168.0.1.

    6) You computer will only will connect to computers in the local subnet (see 4) by IP address.  When you use a computer name, you will only be able to connect tocomputer names recognized by the DNS server or computer names in your ARP table.  What happens is your computer will ARP for any computer by name and then the DNS server will respond back with a ARP ACK if it found the computer name.  The DNS server will either look up the computer name its telephone book of computer names, and/or forward the ARP request to another DNS server or to another subnet(s).  Eventually the ARP Request will die (timeout)  or an ACK Response will be returned indicating the computer name was found.  The ARP request contains a hop count which is the maximum number of computers the ARP message will forwarded.  Each time a Server forwards an ARP request the hop count is decreased by one.  If a Server gets a ARP request with a count of 1 it doesn't forward the ARP request so the the ARP is not forwarded forever.

    7) When you computer is turned on it will send out a ARP Request asking for any computer in the local subnet (see 4 above) to respond.  Any computer responding will be stored in the local in the ARP table.  The computers in the subnet will also store the ARP Request computer IP in their ARP table.  You can see the ARP table using the cmd.exe : ARP -a.  Periodically usually every 15 to 30 minutes a computer automaticall send out an ARP Request to update any computer in the subnet that it is still operational and any ARP Resonse will be stored in the ARP table.  Usually the ARP entires time out every 1 hour and are removed from the ARP table.  The times I mentioned above a programmable a vary a little bit between computers.  The ARP timeout is usally at least 2 times the periodical ARP time.  A ARP message and PING are both IGMP messages.  When you perform a PING the response is stored in the ARP table just like the ARP Response provided the IP is in the local subnet.


    jdweng

    Tuesday, November 5, 2013 7:57 AM
  • So I get from your post that I should enable the ethernet interface and change the default gateway to 0.0.0.0. If that's correct, then how would I do those things?

    Clarification: What I'm trying to do is have the client and server connect from two different "normal" home computers. I only have access to one computer, so I'm testing it on myself by using my external IP address for the "server" parameter (port 13000) in the client, and then have the server listen in on 127.0.0.1 (port 13000), thus simulating a connection between two computers on two different networks.

    Thanks.

    Tuesday, November 5, 2013 11:16 PM
  • I believe that all computers should have a default gateway on only one port with a mask of 0.0.0.0.  I've seen too many cases where problems occur when there isn't a dfault gaeway or when two ports or more ports are set to the default gateway.  The other rulle I tell people is no two ports when you combine the IP and the mask should result with exactly the same range of IP addresses.

    You should NOT enable the ethernet port that isn't being used.  You probable have a modem connected to your internet provider with a wireless interface.  Your main computer is probably directly connected to the modem with an ethernet cable, but you ocould have the two computers both being wireless. 

    The real problem is the two computer aren't in the same subnet.  Opening up the masks on the two computers to 0.0.0.0 will put them two computers in the same subnet.  Putting the two computers inj teh same subnet by changing IP addresses will also work

    Now the negative trade off of using a default gateway of 0.0.0.0.

           1) Every ARP IP address will end up in the ARP table.

           2) Every computer will have access to your computer which MAY be a security issue.  Some people think it will make it easier for hackers to have access to your computer.  I think the hackers will always find a wasy of getting into a computer and the changing the mask doesn't make it either easier or harder for the hackers.


    jdweng

    Wednesday, November 6, 2013 6:56 AM
  • So how would I go about following the advice in your post?
    Wednesday, November 6, 2013 11:08 PM
  • 1) Right click My Network and select properties
    2) Select connection for wireless interface.  Should be connected and right click properties
    3) Select Internet Protocol (TCP/IP) and press properties
    4) Only make changes if the IP is set manually.

    Otherwise, you have to check the your router properties.  With DNS enabled the IP is either coming from the router or the internet provider.  Most likely the wireless router isn't set up properly.  th ewireless router is usually configured using the manufuacturers utility.  You can also write you own commands to change settings but that is more complicated.  I have read through a number of wireless manuals and they are very confusing even to experts.

    I suspect you have you main computer connected by ethernet to the router and the 2nd computer over the wireless channel.  The settings aren't allowing the two computers to communicate to each other.  It could be an IP and mask issue or lots of other settings.


    jdweng

    Wednesday, November 6, 2013 11:24 PM
  • My IP address is not set manually. Also there is only one computer and it is generally connected through Wi-Fi. I searched through my routers setting, but could not find any option for changing my default gateway.
    Thursday, November 7, 2013 11:28 PM
  • The reson why I said two computers was the following :

    "Clarification: What I'm trying to do is have the client and server connect from two different "normal" home computers. I only have access to one computer, so I'm testing it on myself by using my external IP address for the "server" parameter (port 13000) in the client, and then have the server listen in on 127.0.0.1 (port 13000), thus simulating a connection between two computers on two different networks."

    For two computers to connect they need a route between the two computers.  That is why I'm was looking at the IP and mask.

    You siad the following :

    "I have port forwarding setup so that anything sent to my IP address on port 13000 gets directed to my computer. I was under the impression that localHost was equivalent to 127.0.0.1. So anything sent to my IP address on port 13000 should end up having the same result as localHost, no?"

    You don't want to use port forwarding to make a simple connection.  The server and client should use the same port number.  You server is listening on IP address 127.0.0.1 the loopback address.  A computer can have more than one IP address.  One for each network card on the PC.  All IP address onteh computer get forward to the loopback addresss so you don't have to set up a different listener when a PC has more than one network card.

    Also when you have both a client and server on the same PC you cannot have the server and client client connected to the same port number and IP address at the same time.  You need to use two different IP addresses.  So you connect the listener to the loopback and you  connect the client to the IP address of the PC.  The PC will autoimatically forward the messages between the loopback and the IP address without you doning anything.

    Please remove any port forwarding that you have setup. 


    jdweng

    Friday, November 8, 2013 1:56 AM
  • Thank you very much, your post has cleared up alot. 

    So the whole time the problem was with the fact that I was using the same computer to test out both the client and the server. The misunderstanding was because I never clearly mentioned the fact that I was using the same computer. I will test it out properly (using two different computers) when I get the chance.

    For now though, you mentioned I could test it out (on the same computer) if I forget about port forwarding. But then what port number should I use? I have to use one, right?

    P.S. Please correct me if any of the assumptions in this post are wrong.

    Friday, November 8, 2013 2:36 AM
  • I missed your statement that you were using port forwarding.  You can test on one computer.  Just don't use port fowarding.  set the listener (server) to IP 127.0.0.1.  Set client to computer IP address.  Port forwarding changes the port number.  You have your client and server set to the same IP address so no port fowarding is necessary.  the connection is made because the PC forards IP address of the computer to the IP address.  It looks like you were confusing IP address forwarding with Port forwarding.

    A firewall will use port forwarding.  You have to make sure the firewall is blocking port number 13000.  Usually firewall will only block or forward port numbers below 1023.  but you have to check the firewall settings to make sure 13000 is allowed.


    jdweng

    Friday, November 8, 2013 2:59 AM
  • I understand all that. My question was what port number should I use? TcpClient and TcpListener both require a port number to operate on. I have to specify what port number for the TcpListener to listen on, and I have to specify what port number for the TcpClient to connect on. Both give me an error if I attempt to omit the port number parameter. If I can't connect both the server and client to the same port number at the same time, then what am I supposed to do?
    Friday, November 8, 2013 5:15 PM
  • YOu must choose a port number and the number is arbitary provided the following

    1) Client and Server use the same port number

    2) Another application isn't using the port number

    3) The port number isn't being blocked by a firewall

    For custom applications it is best to use a random number above 10,000.  Since you got a hyperterminal to work on 13000 ther is no reason that you can't get your code working on the same port number.  I don't know what you were doing when you said you were using port forwarding.


    jdweng

    Friday, November 8, 2013 7:06 PM
  • I'm trying to connect a client to a server on the same computer. You said that is possible, but the two following statements seem to contradict each other:

    "You can test on one computer. Just don't use port forwarding. set the listener (server) to IP 127.0.0.1. Set client to computer IP address."

    "Also when you have both a client and server on the same PC you cannot have the server and client client connected to the same port number and IP address at the same time."

    My question was, in essence, how am I supposed to reconcile those two statements?

    Sunday, November 10, 2013 11:38 PM
  • Port forwarding is not used in your case.  Port forwarding means to move the connection to a different port number.   You code is always using the same port number.  You are using normal IP routing.

    You have two connections on your computer

    1) Server is connected to 127.0.0.1 : 13000

    2) Client is connected to LocalHost (or computer IP) : 13000

    What really happens is the Server registers an event for port number 13000 with the ethernet adapter.  The Timer tick service which handles all events will forward the packets up from the ethernet driver to the server application.

    When your client sends a message it is sent down to the ethernet driver.  The ethernet driver checks the port number to see if any application is registered for the port number.   Since the server is registered for the port the ethernet driver notifies the Timer tick Servcie to forward the packet up to the server application.


    jdweng

    Monday, November 11, 2013 1:09 AM