LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C(pure) Network programming questions (https://www.linuxquestions.org/questions/programming-9/c-pure-network-programming-questions-941621/)

alaios 04-25-2012 03:48 AM

C(pure) Network programming questions
 
Dear all,
I am reading and programming these days sockets, which I find great.
I have some questions though :) If anyone wants to share the fun please join


a) I found that the maximum udp packet I can send is of 1607 bytes. A packet of 1608 will make sendto() to return -1. How I can find what are the maximum packet size for UDP and TCP? When the programmer has to care about the fragmentation?

b) I am using eclipse to write c :) When I start new c projects eclipse "asks" Do you want to use Cross gcc or Linux gcc. Where I can find more info regarding those differences?

c)
Code:

#include <sys/types.h>          /* See NOTES */
      #include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

I am not sure I fully understand the function definition of bind. The second parameters is a const struct sockaddr. Even though I made it work with (struct sockaddr*)&addr (where addr is struct sockaddr_in addr) I do not understand what is the reason for the constant. If I understand it right constant says I am only declared once and I cannot change (from web: "The const qualifier explicitly declares a data object as something that cannot be changed.")


d) In function prototypes like the c, the *datatype..."means": give me an address. Either by &mydatatype or *pointer=&mydatatype and then pass pointer. Right?

e) When I use funtions like socket, bind e.t.c I use perror to control when something went wrong. Example
Code:

if ((sockfd=socket(domain,type, protocol))==-1)
            perror("socket initialization error");

If I understand it right perror only prints the error message but does not make the programn to exit. How you should make a c program to exit and with what error code?

f) When setting the port (of a socket) I use
Code:

        struct sockaddr_in addr;
        addr.sin_port= htons(33333);

I have found that htons works while htonl return a number that does not look like a valid port(at least not the one I wanted!) Of course this some mistake I do with the data types. The data type needed is the
Code:

in_port_t      sin_port;  /* port in network byte order */
. I have found that the in_port_t is a 32bit integet (I have read the .h files....) How I know if this should be treated as short or long? (Is that system specific?)


g) A typical thing in c is the number of warning one gets. I am not quite sure which ones you can ignore and which not. For example I found many times the "implicit declaration" ... how do you handle the warnings?

I would like to thank you in advance for the time spent
Regards
Alex

pan64 04-25-2012 03:53 AM

e) http://pubs.opengroup.org/onlinepubs...ns/perror.html
see example also


All times are GMT -5. The time now is 01:39 PM.