简体   繁体   中英

C# multiple sockets using the same port on separate threads

I have multiple threads using multiple sockets but all pointing to one port. Will it work? I have seen some threads saying multiple Applications cant use the same ports but I'm not sure it directly addresses this question

Example of the code

       static IPEndPoint b = new IPEndPoint(IPADRESS, PORT);
       static Thread listenThread = new Thread(listen);
       static Thread sendThread = new Thread(send);

       static void listen(){
          Socket socket = new Socket;
            socket.bind(b);
          while(exit == false){

          Socket.listen(10);
           //handle Data

                }

           static void send(){
          Socket socket = new Socket;
            socket.bind(b);
          while(exit == false){

          Socket.send(msg)
                }

If you have only one listening thread and one sending thread on the same port you are ok. But if more then make one thread listen to the port and push whatever received into queue. Other threads should read from the queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM