Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I have searched everywhere but couldn't find as they are all answering to send message to all clients. What I want to achieve is multiple clients request to server to request data from another client and other client sends data to server telling it that data is for requesting client and so. I don't know how to achieve this. I'm new to this.

What I want to achieve:

Visual Description of my problem[^]

I have tried with Data sending client to listen and requesting client to connect to it and transfer data. I have achieved this on local network but to make it work online it needs port forwarding and my user will be a lot of different people so port forwarding is not possible for every user. So I can rent a server which will act as a center of transfer. I programmed a test server in console which will listen to a server IP:port X and accept new clients and their data on port X and forward it to server IP:port Y but what this does is send data to all clients on port Y. I cannot send it to clients public ip address directly for obvious reasons. I understand that all the requesting clients are connected to port Y but I cannot create and assign new ports to all the clients interacting. So I want a way to determine how to request and receive the data without the need of assigning or creating new ports to different clients on same server.

What I have tried:

Server code


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

namespace Test___server
{
    class server
    {
        public static string serverIP = "192.168.0.102";
        static void Main(string[] args)
        {
            Thread listenSendingThread = new Thread(listenSending);
            listenSendingThread.IsBackground = true;
            listenSendingThread.Start();
            Thread listenReceivingThread = new Thread(listenReceiving);
            listenReceivingThread.IsBackground = true;
            listenReceivingThread.Start();
            Console.ReadKey();
        }

        public static List<TcpClient> listSending = new List<TcpClient>();
        public static List<TcpClient> listReceiving = new List<TcpClient>();
        public static TcpClient clientSending = null;
        private static void listenSending()
        {
            TcpListener listenerSending = new TcpListener(IPAddress.Parse(serverIP), 5319);
            listenerSending.Start();
            Console.WriteLine("Server listening to " + serverIP + ":5319");
            while(true)
            {
                clientSending = listenerSending.AcceptTcpClient();
                listSending.Add(clientSending);
                Console.WriteLine("Sender connection received from " + clientSending.Client.RemoteEndPoint);
            }
        }
        private static void send()
        {
            StreamWriter sw = new StreamWriter(clientSending.GetStream());
            sw.WriteLine(message);
            sw.Flush();
            Console.WriteLine("Message sent!");
        }

        public static string message = string.Empty;
            
        private static void listenReceiving()
        {
            TcpListener listener = new TcpListener(IPAddress.Parse(serverIP), 0045);
            listener.Start();
            Console.WriteLine("Server listening to " + serverIP + ":0045");
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                listReceiving.Add(client);
                Console.WriteLine("Receiver connection received from " + client.Client.RemoteEndPoint);
                StreamReader sr = new StreamReader(client.GetStream());
                message = sr.ReadLine();
                send();
            }
        }
    }
}


Requesting client code

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test____admin
{
    class admin
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Begin");
            string serverIP = "192.168.0.102";
            System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
            clientSocket.Connect(serverIP,  );
            Console.WriteLine("Connected");
            while (true)
            {
                Console.WriteLine("Reading");
                StreamReader sr = new StreamReader(clientSocket.GetStream());
                Console.WriteLine("Message: " + sr.ReadLine());
            }
        }
    }
}


Request satisfying client code

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

namespace Test___client
{
    class client
    {
        public static string serverIP = "192.168.0.102";
        static void Main(string[] args)
        {
            clientConnect();
        }

        private static void clientConnect()
        {
            try
            {
                TcpClient client = new TcpClient(serverIP, 0045);
                StreamWriter sw = new StreamWriter(client.GetStream());
                sw.WriteLine("Karan!");
                sw.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
Posted

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