2

Now, I'm interested to know - if I have a program of mine connection to a Server through TCP and the server sends a message to my program, but I am sending UDP packets at the same time, will the TCP packet get to me? Everything is in one class!

Thanks for your help!

Daaksin
  • 834
  • 3
  • 13
  • 28

3 Answers3

0

Short answer - Yes. You can have many connections at once, all sending and receiving; assuming that you've got multiple sockets.

You don't mention the number of clients that your server will have to deal with, or what you are doing with the data being sent/received. Dependent on your implementation, multiple threads may also be required (As Dariusz Wawer points out, this is not essential, but I mention them because a scalable solution that can handle larger numbers of clients will likely use threads).

Check out this post on TCP and UDP Ports for some further info: TCP and UDP Ports Explained

A good sample C# tutorial can be found here: C# Tutorial - Simple Threaded TCP Server

HaemEternal
  • 2,229
  • 6
  • 31
  • 50
  • 1
    Yes, I do have separately named Sockets - thank you for your help! – Daaksin Jan 23 '13 at 11:32
  • @DariuszWawer Unless you try to process information that is being read while processing info to be written... – HaemEternal Jan 23 '13 at 11:33
  • 2
    @HaemEternal Still, threads and sockets read/write are completely unrelated subjects! Thread safety is applicable only if you use threads. You can write perfectly fine single-threaded application which handles multiple connections. – Dariusz Jan 23 '13 at 11:40
  • @DariuszWawer That is true, and I'm not really talking about thread safety so much as trying to suggest a good implementation that will work efficiently. You are correct, this isn't a thread issue, but its an issue where threads may become important. I've edited my post to try and reflect that. – HaemEternal Jan 23 '13 at 11:55
  • @HaemEternal sorry, I still can't agree with you. Threads will only improve efficiency if there is enough work to fill a single core. Otherwise there is just multithreading overhead and no gain. A *Good implementation* should be simple, clean and readable - again, threads don't usually help fulfill these goals. – Dariusz Jan 23 '13 at 11:59
0

Depends on what you mean by "at the same time". Usually the answer is "yes", you can have multiple TCP/IP connections and multiple UDP sockets all transmitting and receiving at the same time.

Unless you're really worried about latency - where a few microseconds can cause you trouble. If that's the case, one connection may interfere with the other.

zmbq
  • 38,013
  • 14
  • 101
  • 171
0

Your question is actually on the border of several issues that all network application programmers must know and consider.

First of all: all data received from the network is stored in operating system's internal buffer, where it awaits to be read. The buffer is not infinite, so if you wait long enough, some data may be dropped. Usually the chunks of data that are written there are single packets, but not always. You can never make any assumptions of how much data will be available for reading in TCP/IP communication. In UDP, on the other hand, you must always read a single packet, otherwise the data will be lost. You can use recvfrom to read UDP packets and I suggest using it.

Secondly: using blocking and non-blocking approach is one of the most important decisions for your network app. There is a lot of information about it in the Internet: C- Unix Sockets - Non-blocking read , what is the definition of blocking read vs non- blocking read? or a non-blocking tutorial.

As for threads: threads are never required to write a multiple connection handler application. Sometimes they will simplify your code, sometimes they will make it run faster. There are some well-known programming patterns for using threads, like handling each separate connection in a separate thread. More often than not, especially for an inexperienced programmer, using threads will only be a source of strange errors and headaches.

I hope that my post answers your question and addresses the discussion I've been having below another answer.

Community
  • 1
  • 1
Dariusz
  • 21,561
  • 9
  • 74
  • 114