Click here to Skip to main content
15,891,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to create some networking apps with C# socket programming and use them over the Internet so I have to use Public IP to access Computers over the internet network.
A way to do this is do port forwarding on server side router but my apps maybe use on many computers anywhere so modify router settings and do port forwarding on all of routers is impossible.
what is other methods to use C# networking apps over the Internet that they do not need port forwarding?
please help me
thanks

What I have tried:

This program is very simple C# networking app to send a message from client to server.
It works on LAN network and I want to use it over the internet network without port forwarding:

Client side program:

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

namespace ConsoleSocketClient
{
    class Program
    {
        static void Main()
        {
            connection:
            try
            {
                TcpClient client = new TcpClient("192.168.1.7", 1302);
                string messageToSend = "Test Request";
                int byteCount = Encoding.ASCII.GetByteCount(messageToSend + 1);
                byte[] sendData = Encoding.ASCII.GetBytes(messageToSend);

                NetworkStream stream = client.GetStream();
                stream.Write(sendData, 0, sendData.Length);
                Console.WriteLine("sending data to server...");

                StreamReader sr = new StreamReader(stream);
                string response = sr.ReadLine();
                Console.WriteLine(response);

                stream.Close();
                client.Close();
                Console.ReadKey();
            }
            catch {
                Console.WriteLine("failed to connect...");
                goto connection;
            }
        }
    }
}


Server side program:

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

namespace ConsoleSocketServer
{
    class Program
    {
        static void Main()
        {
            TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 1302);
            listener.Start();
            while (true)
            {
                Console.WriteLine("Waiting for a connection.");
                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine("Client accepted.");
                NetworkStream stream = client.GetStream();
                StreamReader sr = new StreamReader(client.GetStream());
                StreamWriter sw = new StreamWriter(client.GetStream());
                try
                {
                    byte[] buffer = new byte[1024];
                    stream.Read(buffer, 0, buffer.Length);
                    int recv = 0;
                    foreach (byte b in buffer)
                    {
                        if (b != 0)
                        {
                            recv++;
                        }
                    }
                    string request = Encoding.UTF8.GetString(buffer, 0, recv);
                    Console.WriteLine("request received");
                    sw.WriteLine("You rock!");
                    sw.Flush();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Something went wrong.");
                    sw.WriteLine(e.ToString());
                }
            }
        }
    }
}
Posted
Updated 13-Dec-20 9:08am
v3

1 solution

Take a look at SignalR, here is an example on CodeProject: Beginner's Guide to Using SignalR via ASP.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900