R.E
R.E

Reputation: 107

Create socket connection (TCP or UDP) without ip address

I want to connect my server. My server is listening on port 50000.

            NetworkStream socketStream = null;
            Socket AcceptedClient;// stream for receiving data           
            byte[] bCode;

            TcpListener listener = null;       
            IPAddress local = IPAddress.Any;
            listener = new TcpListener(local, 50000);

In client computer:

I don't know IP and host name of it.

Our IP is in 192.168.1.x

I must have IP or host name of it.

Is it possible that I create socket connection on this scenario?

My question is: Is it possible to connect other computer without IP address?(TCP or UDP) Thank you.

Upvotes: 1

Views: 4965

Answers (2)

VOLVO
VOLVO

Reputation: 561

Create UDP Listener object

UdpClient client = new UdpClient();

Define End point for send Code in broadcast mode

IPEndPoint end = new IPEndPoint(IPAddress.Parse("192.168.0.255"),50001);
byte[] bCodeMelli = System.Text.Encoding.Unicode.GetBytes(strCodeMelli);
client.SendAsync(bCodeMelli, bCodeMelli.Length, end);
byte[] bInfo = client.Receive(ref end);

Upvotes: 2

ma7r
ma7r

Reputation: 396

If the client doesn't know the address of the server, but is on the same subnet, then consider using a UDP broadcast from the client (that the server will listen for). Have a look at this question: Sending UDP broadcast, receiving multiple messages

Upvotes: 2

Related Questions