Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set IP address of client manually in .net socket program

I created a multithreaded client-server chat application and want to test my application with multiple clients. I'm planning to create a simulator in client side which create a random port and IP. By that I mean my client system should run with multiple ports (without running multiple times).

I tried to find out the part of the code which gives client IP and port number in the client class, but couldn't figure it out. I only found the part which gives server IP and port.

This is my connection establishing part

private void cmdConnect_Click(object sender, System.EventArgs e)
    {
        try
        {
            //create a new client socket ...
            m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            String szIPSelected  = txtIPAddress.Text;
            String szPort = txtPort.Text;
            int  alPort = System.Convert.ToInt16 (szPort,10);


            System.Net.IPAddress    remoteIPAddress  = System.Net.IPAddress.Parse(szIPSelected);
            System.Net.IPEndPoint   remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
            m_socWorker.Connect(remoteEndPoint);
        }
        catch (System.Net.Sockets.SocketException se)
        {
            MessageBox.Show ( se.Message );
        }
    }

and my data sending part

private void cmdSendData_Click(object sender, System.EventArgs e)
    {
        try
        {
            Object objData = txtData.Text;
            byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
            m_socWorker.Send (byData);
        }
        catch(System.Net.Sockets.SocketException se)
        {
            MessageBox.Show (se.Message );
        }
    }
like image 208
Sachin K S Avatar asked Dec 17 '12 11:12

Sachin K S


People also ask

What is IP address in socket programming?

If the IP address is like an office building main phone number, a socket is like the extension numbers for offices. So the IP and socket, often called the port, uniquely identify an "office" (server process). You will see unique identifiers like 192.168. 2.100:80 where 80 is the port.

Does a socket have an IP address?

A socket consists of the IP address of a system and the port number of a program within the system. The IP address corresponds to the system and the port number corresponds to the program where the data needs to be sent: Sockets can be classified into three categories: stream, datagram, and raw socket.

What is the command to assign port number to client and server?

This can be done using a bind() system call specifying a particular port number in a client-side socket. Below is the implementation Server and Client program where a client will be forcefully get assigned a port number.


1 Answers

If you really need to set the client socket address before making a connection to the server, and it is often a valid scenario when one wants to force the kernel to bind to a specific local address on multihomed systems, you may call Socket.Bind() before you call Socket.Connect():

IPAddress  localIPAddress = IPAddress.Parse(szLocalIP);
IPEndPoint localEndPoint  = new IPEndPoint(localIPAddress, 0);
m_socWorker.Bind(localEndPoint);

m_socWorker.Connect(remoteEndPoint);

You may also explicitly specify the local port number in the IPEndPoint constructor, but you have to make sure, that the port is not in use. If you leave the port number equal to 0, then the system would pick an (almost) arbitrary free port number.

Note that you cannot just pick an arbitrary client IP address - it must be one of the addresses, configured on your system's enabled network interfaces. See the output of ipconfig /all for what is configured. You can assign more than one IP address to an interface if you simply want to test.

like image 113
Hristo Iliev Avatar answered Sep 17 '22 05:09

Hristo Iliev