Server gives exception and fails to start












1















lately I started to learn about Tcp/ip sockets in c#, so I watched a tutorials and read online about it. I followed tutorial and I made client-server app that is sending simple text to the server on the same network. I tried to "upgrade" and make client communicate with other client that is on different network thru server. But now server fails to start saying that "The requested address is not valid in its context".



First was server running as localhost, but now when I want to clients that are in different networks communicate through server , server needs to run on my ip address so other client in different network could connect.  I did IPAddress ip = IPAddress.Parse("my IP"); and  TcpListener  server = new TcpListener(ip, 10000); I tried using different ports but its still the same. 


This is what exceptions tells me
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()

IPAddress ip = IPAddress.Parse("192.168.1.5"); //Mock IP

TcpListener server = new TcpListener(ip, 8080);


TcpClient client = default(TcpClient);
TcpClient client2 = default(TcpClient);

try
{
server.Start();
Console.WriteLine("Server started...");


}
catch(Exception ex)
{
Console.WriteLine("Server failed to start... {0}",ex.ToString());
Console.Read();

}

while (true)
{
client = server.AcceptTcpClient();

client2 = server.AcceptTcpClient();

byte receivedBuffer = new byte[1000];

byte receivedBuffer2 = new byte[1000];

NetworkStream stream = client.GetStream();

NetworkStream stream2 = client2.GetStream();

stream.Read(receivedBuffer, 0, receivedBuffer.Length);

stream2.Read(receivedBuffer2, 0, receivedBuffer2.Length);

StringBuilder message = new StringBuilder();
foreach (byte b in receivedBuffer)
{
if (b.Equals(126))
{
break;
}
else
{
message.Append(Convert.ToChar(b).ToString());
}
}
StringBuilder message2 = new StringBuilder();
foreach (byte g in receivedBuffer2)
{
if (g.Equals(126))
{
break;
}
else
{
message2.Append(g);
}
}
//string message = Encoding.ASCII.GetString(receivedBuffer, 0, receivedBuffer.Length);

Console.WriteLine(message.ToString() + message.Length);
Console.WriteLine(message2.ToString() +message2.Length);









share|improve this question

























  • What line is throwing the exception?

    – Symon
    Dec 31 '18 at 15:11











  • Line 26 when server is supposed to start.

    – LordRed
    Dec 31 '18 at 15:20











  • Here is MSDN reference for using TcpListener just as a reference. -- Firstly, I would hope that isn't your actual IP given in the question, and if it is, I recommend changing it (within the question) for security. Then , I would verify if anything is already running on port 8080. Have you tried different ports?

    – Symon
    Dec 31 '18 at 15:35











  • I tried to change ports but its saying that ip its not valid for some reason but when i start it from local ipv4 address with same port it works.

    – LordRed
    Dec 31 '18 at 17:03











  • It is working only with local ip.

    – LordRed
    Dec 31 '18 at 17:54
















1















lately I started to learn about Tcp/ip sockets in c#, so I watched a tutorials and read online about it. I followed tutorial and I made client-server app that is sending simple text to the server on the same network. I tried to "upgrade" and make client communicate with other client that is on different network thru server. But now server fails to start saying that "The requested address is not valid in its context".



First was server running as localhost, but now when I want to clients that are in different networks communicate through server , server needs to run on my ip address so other client in different network could connect.  I did IPAddress ip = IPAddress.Parse("my IP"); and  TcpListener  server = new TcpListener(ip, 10000); I tried using different ports but its still the same. 


This is what exceptions tells me
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()

IPAddress ip = IPAddress.Parse("192.168.1.5"); //Mock IP

TcpListener server = new TcpListener(ip, 8080);


TcpClient client = default(TcpClient);
TcpClient client2 = default(TcpClient);

try
{
server.Start();
Console.WriteLine("Server started...");


}
catch(Exception ex)
{
Console.WriteLine("Server failed to start... {0}",ex.ToString());
Console.Read();

}

while (true)
{
client = server.AcceptTcpClient();

client2 = server.AcceptTcpClient();

byte receivedBuffer = new byte[1000];

byte receivedBuffer2 = new byte[1000];

NetworkStream stream = client.GetStream();

NetworkStream stream2 = client2.GetStream();

stream.Read(receivedBuffer, 0, receivedBuffer.Length);

stream2.Read(receivedBuffer2, 0, receivedBuffer2.Length);

StringBuilder message = new StringBuilder();
foreach (byte b in receivedBuffer)
{
if (b.Equals(126))
{
break;
}
else
{
message.Append(Convert.ToChar(b).ToString());
}
}
StringBuilder message2 = new StringBuilder();
foreach (byte g in receivedBuffer2)
{
if (g.Equals(126))
{
break;
}
else
{
message2.Append(g);
}
}
//string message = Encoding.ASCII.GetString(receivedBuffer, 0, receivedBuffer.Length);

Console.WriteLine(message.ToString() + message.Length);
Console.WriteLine(message2.ToString() +message2.Length);









share|improve this question

























  • What line is throwing the exception?

    – Symon
    Dec 31 '18 at 15:11











  • Line 26 when server is supposed to start.

    – LordRed
    Dec 31 '18 at 15:20











  • Here is MSDN reference for using TcpListener just as a reference. -- Firstly, I would hope that isn't your actual IP given in the question, and if it is, I recommend changing it (within the question) for security. Then , I would verify if anything is already running on port 8080. Have you tried different ports?

    – Symon
    Dec 31 '18 at 15:35











  • I tried to change ports but its saying that ip its not valid for some reason but when i start it from local ipv4 address with same port it works.

    – LordRed
    Dec 31 '18 at 17:03











  • It is working only with local ip.

    – LordRed
    Dec 31 '18 at 17:54














1












1








1








lately I started to learn about Tcp/ip sockets in c#, so I watched a tutorials and read online about it. I followed tutorial and I made client-server app that is sending simple text to the server on the same network. I tried to "upgrade" and make client communicate with other client that is on different network thru server. But now server fails to start saying that "The requested address is not valid in its context".



First was server running as localhost, but now when I want to clients that are in different networks communicate through server , server needs to run on my ip address so other client in different network could connect.  I did IPAddress ip = IPAddress.Parse("my IP"); and  TcpListener  server = new TcpListener(ip, 10000); I tried using different ports but its still the same. 


This is what exceptions tells me
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()

IPAddress ip = IPAddress.Parse("192.168.1.5"); //Mock IP

TcpListener server = new TcpListener(ip, 8080);


TcpClient client = default(TcpClient);
TcpClient client2 = default(TcpClient);

try
{
server.Start();
Console.WriteLine("Server started...");


}
catch(Exception ex)
{
Console.WriteLine("Server failed to start... {0}",ex.ToString());
Console.Read();

}

while (true)
{
client = server.AcceptTcpClient();

client2 = server.AcceptTcpClient();

byte receivedBuffer = new byte[1000];

byte receivedBuffer2 = new byte[1000];

NetworkStream stream = client.GetStream();

NetworkStream stream2 = client2.GetStream();

stream.Read(receivedBuffer, 0, receivedBuffer.Length);

stream2.Read(receivedBuffer2, 0, receivedBuffer2.Length);

StringBuilder message = new StringBuilder();
foreach (byte b in receivedBuffer)
{
if (b.Equals(126))
{
break;
}
else
{
message.Append(Convert.ToChar(b).ToString());
}
}
StringBuilder message2 = new StringBuilder();
foreach (byte g in receivedBuffer2)
{
if (g.Equals(126))
{
break;
}
else
{
message2.Append(g);
}
}
//string message = Encoding.ASCII.GetString(receivedBuffer, 0, receivedBuffer.Length);

Console.WriteLine(message.ToString() + message.Length);
Console.WriteLine(message2.ToString() +message2.Length);









share|improve this question
















lately I started to learn about Tcp/ip sockets in c#, so I watched a tutorials and read online about it. I followed tutorial and I made client-server app that is sending simple text to the server on the same network. I tried to "upgrade" and make client communicate with other client that is on different network thru server. But now server fails to start saying that "The requested address is not valid in its context".



First was server running as localhost, but now when I want to clients that are in different networks communicate through server , server needs to run on my ip address so other client in different network could connect.  I did IPAddress ip = IPAddress.Parse("my IP"); and  TcpListener  server = new TcpListener(ip, 10000); I tried using different ports but its still the same. 


This is what exceptions tells me
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()

IPAddress ip = IPAddress.Parse("192.168.1.5"); //Mock IP

TcpListener server = new TcpListener(ip, 8080);


TcpClient client = default(TcpClient);
TcpClient client2 = default(TcpClient);

try
{
server.Start();
Console.WriteLine("Server started...");


}
catch(Exception ex)
{
Console.WriteLine("Server failed to start... {0}",ex.ToString());
Console.Read();

}

while (true)
{
client = server.AcceptTcpClient();

client2 = server.AcceptTcpClient();

byte receivedBuffer = new byte[1000];

byte receivedBuffer2 = new byte[1000];

NetworkStream stream = client.GetStream();

NetworkStream stream2 = client2.GetStream();

stream.Read(receivedBuffer, 0, receivedBuffer.Length);

stream2.Read(receivedBuffer2, 0, receivedBuffer2.Length);

StringBuilder message = new StringBuilder();
foreach (byte b in receivedBuffer)
{
if (b.Equals(126))
{
break;
}
else
{
message.Append(Convert.ToChar(b).ToString());
}
}
StringBuilder message2 = new StringBuilder();
foreach (byte g in receivedBuffer2)
{
if (g.Equals(126))
{
break;
}
else
{
message2.Append(g);
}
}
//string message = Encoding.ASCII.GetString(receivedBuffer, 0, receivedBuffer.Length);

Console.WriteLine(message.ToString() + message.Length);
Console.WriteLine(message2.ToString() +message2.Length);






c# tcp websocket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 15:51









Sorceri

6,48011630




6,48011630










asked Dec 31 '18 at 15:05









LordRedLordRed

133




133













  • What line is throwing the exception?

    – Symon
    Dec 31 '18 at 15:11











  • Line 26 when server is supposed to start.

    – LordRed
    Dec 31 '18 at 15:20











  • Here is MSDN reference for using TcpListener just as a reference. -- Firstly, I would hope that isn't your actual IP given in the question, and if it is, I recommend changing it (within the question) for security. Then , I would verify if anything is already running on port 8080. Have you tried different ports?

    – Symon
    Dec 31 '18 at 15:35











  • I tried to change ports but its saying that ip its not valid for some reason but when i start it from local ipv4 address with same port it works.

    – LordRed
    Dec 31 '18 at 17:03











  • It is working only with local ip.

    – LordRed
    Dec 31 '18 at 17:54



















  • What line is throwing the exception?

    – Symon
    Dec 31 '18 at 15:11











  • Line 26 when server is supposed to start.

    – LordRed
    Dec 31 '18 at 15:20











  • Here is MSDN reference for using TcpListener just as a reference. -- Firstly, I would hope that isn't your actual IP given in the question, and if it is, I recommend changing it (within the question) for security. Then , I would verify if anything is already running on port 8080. Have you tried different ports?

    – Symon
    Dec 31 '18 at 15:35











  • I tried to change ports but its saying that ip its not valid for some reason but when i start it from local ipv4 address with same port it works.

    – LordRed
    Dec 31 '18 at 17:03











  • It is working only with local ip.

    – LordRed
    Dec 31 '18 at 17:54

















What line is throwing the exception?

– Symon
Dec 31 '18 at 15:11





What line is throwing the exception?

– Symon
Dec 31 '18 at 15:11













Line 26 when server is supposed to start.

– LordRed
Dec 31 '18 at 15:20





Line 26 when server is supposed to start.

– LordRed
Dec 31 '18 at 15:20













Here is MSDN reference for using TcpListener just as a reference. -- Firstly, I would hope that isn't your actual IP given in the question, and if it is, I recommend changing it (within the question) for security. Then , I would verify if anything is already running on port 8080. Have you tried different ports?

– Symon
Dec 31 '18 at 15:35





Here is MSDN reference for using TcpListener just as a reference. -- Firstly, I would hope that isn't your actual IP given in the question, and if it is, I recommend changing it (within the question) for security. Then , I would verify if anything is already running on port 8080. Have you tried different ports?

– Symon
Dec 31 '18 at 15:35













I tried to change ports but its saying that ip its not valid for some reason but when i start it from local ipv4 address with same port it works.

– LordRed
Dec 31 '18 at 17:03





I tried to change ports but its saying that ip its not valid for some reason but when i start it from local ipv4 address with same port it works.

– LordRed
Dec 31 '18 at 17:03













It is working only with local ip.

– LordRed
Dec 31 '18 at 17:54





It is working only with local ip.

– LordRed
Dec 31 '18 at 17:54












1 Answer
1






active

oldest

votes


















0














The error message The requested address is not valid in its context means that the IP address you have used is not tied to any network interface on your machine.
You typically don't want to hard-code a specific IP address that you listen to (e.g. if your machine get's it IP through DHCP it could change).



Better way is to use IPAddress.Any to listen on all IP addresses (i.e. network interface) available on the machine.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988833%2fserver-gives-exception-and-fails-to-start%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The error message The requested address is not valid in its context means that the IP address you have used is not tied to any network interface on your machine.
    You typically don't want to hard-code a specific IP address that you listen to (e.g. if your machine get's it IP through DHCP it could change).



    Better way is to use IPAddress.Any to listen on all IP addresses (i.e. network interface) available on the machine.






    share|improve this answer




























      0














      The error message The requested address is not valid in its context means that the IP address you have used is not tied to any network interface on your machine.
      You typically don't want to hard-code a specific IP address that you listen to (e.g. if your machine get's it IP through DHCP it could change).



      Better way is to use IPAddress.Any to listen on all IP addresses (i.e. network interface) available on the machine.






      share|improve this answer


























        0












        0








        0







        The error message The requested address is not valid in its context means that the IP address you have used is not tied to any network interface on your machine.
        You typically don't want to hard-code a specific IP address that you listen to (e.g. if your machine get's it IP through DHCP it could change).



        Better way is to use IPAddress.Any to listen on all IP addresses (i.e. network interface) available on the machine.






        share|improve this answer













        The error message The requested address is not valid in its context means that the IP address you have used is not tied to any network interface on your machine.
        You typically don't want to hard-code a specific IP address that you listen to (e.g. if your machine get's it IP through DHCP it could change).



        Better way is to use IPAddress.Any to listen on all IP addresses (i.e. network interface) available on the machine.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 9:57









        johlojohlo

        4,65811128




        4,65811128
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988833%2fserver-gives-exception-and-fails-to-start%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas