codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Client-Server Program: Client can connect to server easily but server can't respond cause target machine actively refuses connections


By : , Category : c#

You need to design your client and server so that the client-to-server connection remains open to recieve the responce and then do something like this:

public static bool sendTo(IPEndPoint ip, String data)
{
    TcpClient client = null;
    NetworkStream stream = null;
    StreamWriter writer = null;
    StreamReader reader = null;

    try
    {
        client = new TcpClient();
        client.Connect(ip);
        stream = c.GetStream();
        writer = new StreamWriter(stream);
        writer.WriteLine(data);
        writer.Flush();

        reader = new StreamReader(stream);
        string response = reader.ReadLine();

        if (response == "roger-alpha-tango")
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
    finally
    {
        if (writer != null) writer .Dispose();
        if (reader != null) reader .Dispose();
        if (stream != null) stream.Dispose();
        if (client != null) { client.Close(); client.Dispose(); }
    }

    return false;
}
ReLated :

Ok so the problem was vary simple in this function I'm manipulating to parameters locally which lead to peerListener.Start() to be started only locally , changing it to static did the Trick...

  CreateNewTcpListenerANDstart()
 {
  peerListener = new TcpListener(IPAddress.Any, ComSettings.Port);
  peerListener.Start();
 }

As the server, just write back into the very same NetworkStream you read the string from! On the client side - nothing different - read freom the NetworkStream you just wrote the string to.

Your NetworkStream objects should simply work in both directions ;)

To connect in your code you use:

Socket socket = new Socket(server_IP,server_Port);

So you could use:

Socket socket = new Socket("192.168.1.4", 5555);

It looks like you have this in your code so I'm not sure what problem you're having.

Don't forget that you have to setup your router to forward ports if it is located outside of your local network.

http://www.wikihow.com/Set-Up-Port-Forwarding-on-a-Router

Don't forget that if you are running a firewall, this can also interfere with the connection.

You've left out much of the code. The part in the server that sends data on the accepted socket. So the method that calls accept() just exits, the socket is garbage-collected, and closed, and the client keeps doing I/O to the other end of the connection, which is invalid,so you get an exception.

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .