-1
$\begingroup$

I\’m a novice programmer that\’s learning how to program on his own currently. I\’m currently fiddling around with a server/client program that allows me to send a signal from one computer to another. I tested both on the same computer and they did rather well for the most part. however, I want to see if I can connect with computers on a different network.

-update- i'm currently programming in C# and have both my client and server programs all written out.

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

namespace TCPServer { class Program { static void Main(string[] args) { TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8000); Console.WriteLine("About to initialize port"); listener.Start(); Console.WriteLine("Listening for a connection"); try { using (TcpClient client = listener.AcceptTcpClient()) { Console.WriteLine("connection accepted."); NetworkStream stream = client.GetStream(); using (BinaryWriter w = new BinaryWriter(stream)) { using (BinaryReader r = new BinaryReader(stream)) { if (r.ReadString() == Sharedata.RequestConnect) { w.Write(Sharedata.AcknowledgeOK); Console.WriteLine("Connection complete.");

                            while ( r.ReadString() != Sharedata.Disconnect)
                            {
                            }
                            Console.WriteLine(Environment.NewLine);
                            Console.WriteLine("Disconnect request received.");
                        }
                        else
                        {
                            Console.WriteLine("Can't complete connection");
                        }
                    }
                }
            }
            Console.WriteLine("Connection Complete");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            listener.Stop();
            Console.WriteLine("Listener stopped");
        }
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine("Main method complete. Press Enter");
        Console.ReadLine();

    }
}
public class Sharedata
{
    public const string AcknowledgeOK = "OK";
    public const string AcknowledgeCancel = "Cancel";
    public const string Disconnect = "Bye";
    public const string RequestConnect = "Hello";
}

}

and here's the client code

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

namespace TCPClient { class Program { static void Main(string[] args) { TcpClient client = new TcpClient();

        try
        {
            Console.WriteLine("Attempting to connect to the server on port 8000");
            client.Connect(IPAddress.Parse("127.0.0.1"), 8000);
            Console.WriteLine("Connection establish");
            NetworkStream stream = client.GetStream();
            using (BinaryWriter w = new BinaryWriter(stream))
            {
                using (BinaryReader r = new BinaryReader(stream))
                {
                    w.Write(Shareddata.RequestConnect);
                    if ( r.ReadString() == Shareddata.AcknowledgeOK)
                    {
                        Console.WriteLine("Connected.");
                        Console.WriteLine("Press Enter to disconnect.");
                        Console.ReadLine();
                        Console.WriteLine("Disconnecting...");
                        w.Write(Shareddata.Disconnect);
                    }
                    else
                    {
                        Console.WriteLine("Connection not completed.");
                    }
                }
            }
        }
        catch (Exception err)
        {
            Console.WriteLine(err.ToString());
        }
        finally
        {
            client.Close();
            Console.WriteLine("port closed.");
        }
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine("Main method complete. Press Enter");
        Console.ReadLine();
    }
}
public class Shareddata
{
    public const string AcknowledgeOK = "OK";
    public const string AcknowledgeCancel = "Cancel";
    public const string Disconnect = "Bye";
    public const string RequestConnect = "hello";
}

}

so far I ran both programs together and so far it seems the client one is having issue when it's ran for extended periods of time.

$\endgroup$

closed as off-topic by Pseudonym, jmite, David Richerby, Rick Decker, D.W. Feb 5 '16 at 4:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – Pseudonym, jmite, D.W.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Well the answer is yes, of course, anything is possible! But you need to provide some details and let us know what you have tried, please read this and update your question thanks! cs.stackexchange.com/help/how-to-ask Perhaps telling us what language and providing sample code would be a good start, and I wonder if this question, asked properly, would be a better fit on StackOverflow.com? $\endgroup$ – JimLohse Feb 5 '16 at 0:50
  • $\begingroup$ I posted an answer designed to get you going in the right direction but I am sure it's not perfect, you will have to figure out some things. Also I did flag this to be moved to StackOverflow.com, I don't know if the moderators will agree but this is not a theory question, this is really a standard programming question that belongs over there. You don't actually say what problem your client has or what error you are getting, " is having issue" is pretty vague so I did not address that. $\endgroup$ – JimLohse Feb 5 '16 at 2:17
1
$\begingroup$

I intend this to give you a general idea of what to do next, I have not reviewed your code 100% so as a student you have to figure out where to plug in the pieces I give you.

In your server code you don't need to specify an IP address, using a call like this will get it automatically:

TcpListener serverSocket = new TcpListener(8000);
            TcpClient clientSocket = default(TcpClient);

Then on your client, assuming you are running on the same subnet, you put in the servers IP address and (in this case) port 8000. Let's assume your server is on 192.168.1.10:

clientSocket.Connect("192.168.1.10", 8000);
            serverStream = clientSocket.GetStream();

Now that's just a simple answer designed to get you going in the right direction, perhaps someone else will provide a more complete answer.

In addition you are assuming the connection is established, you probably will want to study Exceptions at some point, a simple example on the client side is:

try
        {
            clientSocket.Connect("192.168.1.10", 8000);
            serverStream = clientSocket.GetStream();
            // do some processing here
        }
        catch (SocketException ex)
        {
            // oops something specific failed!
            Console.WriteLine(ex);
        }
        catch (Exception ex)
        {
            // oops an exception we didn't expect happened!
            Console.WriteLine(ex);
        }

Here's the information you can look up telling what exceptions clientSocket can throw

Take a look at this StackOverflow question and you'll see where I got the try-catch example.

Here are more examples: http://csharp.net-informations.com/communications/csharp-chat-server.htm http://csharp.net-informations.com/communications/csharp-chat-client.htm

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.