Home > Software engineering >  Linux udp socket how to package, immediately send a package?
Linux udp socket how to package, immediately send a package?

Time:09-23

Masters, ask questions, the younger brother beginners Linux socket network programming, in the realization of an algorithm, algorithm requirement is sending a udp message (1472 bytes), strictly control each udp send time, an example is the t1 time send udp1, send udp2 t2 time, t3 send udp3 time,,,, almost will send hundreds of packages, t1, t2, t3 is the time interval between I (algorithm) of strict control, not to take into consideration the disturbance of the system call on time (in the underlying contract, of course, the fastest most accurate),

I want to achieve the effect of application is a package to send out, then send the second package,,, third, fourth, but Internet learned that the system will be independent package, which means that although I call write function to send the first package, but packages in the kernel (or any other places or interfaces that are not immediately go, will wait for a period of time, and then sent out together, (because don't want to waste of resources, a package is sent a scheduling a too expensive),

Like every three seconds for example assumes that the system is to send a package, I 1 s clock write functions at the time of the first package, 2 s clock is sort of the write function to send the second package, 3 s clock call write function to send a third udp packets, but actually in the system, the first tone after the write 1 package waiting for no. 2 and 3 packages in the together and then throw (because 3 s hair once, etc.), the result is not what I want contract time sequence, 1 bag and no. 2, 3 packets to send time is the same, in fact, I then statistical time stamp is the number one package arrived for the longest time, 2 second 3 package, and every time is the result, also confirm the package waiting for 1 # 2, 3 packages

is there any way I can make the system package, send immediately? online checked 1. Call the fflush function, it can immediately send the package, fflush (FILE * stream) function how to cooperate to use with the udp socket interface? Seems to be to write a file I/O to use fflush function, this method is feasible? Fflush function how to use with the socket? 2. The Internet also went close GSO function, which is more appropriate? Thank you all!

CodePudding user response:

Udp does not naturally agreement is based on the package?

CodePudding user response:

reference 1st floor zhao4zhong1 response:
udp was not the agreement is based on the package?
yeah, is based on the package, what I mean is that the kernel will pack poly bag, such as how to make the kernel package,

CodePudding user response:

The setsockopt (,.. SO_SNDBUF,... , 0)?

CodePudding user response:

Fflush is generally in front of the hair or error when such use is more, if you want to directly every time, should be to set the size of the cache area, "7-eleven's webmaster" on the third floor of the reference answer,

CodePudding user response:

reference 4 floor jintiann response:
fflush is generally in front of the hair or errors with more, so if you want to directly every time, should go to set the size of the cache area, "7-eleven's webmaster" on the third floor of the reference answer,


1 # include "unp. H"
2 void
3 str_echo (int sockfd)
4 {
5 char line [MAXLINE];
6 fpin FILE *, * fpout;
7 fpin=Fdopen (sockfd, "r");
8 fpout=Fdopen (sockfd, "w");
9 the while (the Fgets (line, MAXLINE fpin)!=NULL)
10 Fputs (line, fpout);
11}

Above is about the meaning of this code, a simple example, I can add a fflush (fpout); Something like that, I wonder if you can understand what I mean

CodePudding user response:

Jintiann
reference 4 floor response:
fflush is generally in front of the hair or error when such use is more, if you want to directly every time, should go to set the size of the cache area, "7-eleven's webmaster" on the third floor of the reference answer,
you said this buffer is the kernel of send buffer, so just tell the socket immediately copy the data to the kernel stack send buffer, the specific kernel packages such as hair or hair immediately, this has nothing to do both if the setsockopt option is not the actual effect, so to understand?

CodePudding user response:

reference VisualEleven reply: 3/f
setsockopt (,,. SO_SNDBUF,... , 0)?
what do you say this buffer is the kernel of send buffer, so just tell the socket immediately copy the data to the kernel stack send buffer, the kernel package such as hair or hair immediately, this has nothing to do both if the setsockopt option is not the actual effect, so to understand?

CodePudding user response:

The setsockopt (,.. SO_SNDBUF,... , 0) is a positive solution, Linger waiting for things like TCP, udp no wait,
Even if other processes in the use of udp send, udp does not conflict with you or in interference, each socket buffer is independent,
As long as you put the send buffer is set to 0, that is to send immediately, of course could send failure not to go out, test the return value
Anyway, the buffer is 0, the system will immediately sent to the IP layer, but unpredictable failure or not
  • Related