tcp_udp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tcp_udp [2017/05/02 21:57] – dwallace | tcp_udp [2017/05/02 22:45] (current) – dwallace | ||
---|---|---|---|
Line 93: | Line 93: | ||
==== Server ==== | ==== Server ==== | ||
- | Let's break down the server code. The first block is where we include all the necessary libraries and define essential constants. | + | Let's break down the server code. The server is a program that binds to a certain port, and send a predefined message to any client that connects to it. The first block is where we include all the necessary libraries and define essential constants. |
<code c> | <code c> | ||
Line 254: | Line 254: | ||
Note, don't forget that all of the code after the first two blocks belongs in main( ), and that we must return 0 at the end of main( ). | Note, don't forget that all of the code after the first two blocks belongs in main( ), and that we must return 0 at the end of main( ). | ||
+ | |||
+ | Here is what it looks like when we launch the server: | ||
+ | |||
+ | {{ dylanw: | ||
+ | \\ | ||
+ | And here is what it looks like when something connects to the server: | ||
+ | |||
+ | {{ dylanw: | ||
==== Client ==== | ==== Client ==== | ||
- | Now, let's go over the client. This program is much simpler, as it only need to handle one connection during its run. We start out with the includes and the constant declarations, | + | Now, let's go over the client. The client is a program that connects to server port at a given IP address, and receives the message that the server sends. This program is much simpler, as it only need to handle one connection during its run. We start out with the includes and the constant declarations, |
<code c> | <code c> | ||
Line 373: | Line 381: | ||
Just as with the server, we must ensure that everything after the first two blocks is in the main( ) function, and that we return 0 at the end. | Just as with the server, we must ensure that everything after the first two blocks is in the main( ) function, and that we return 0 at the end. | ||
+ | |||
+ | Here is what it looks like when the client is launched, and receives a message: | ||
+ | |||
+ | {{ dylanw: | ||
+ | |||
===== UDP Talker & Listener ===== | ===== UDP Talker & Listener ===== | ||
Line 383: | Line 396: | ||
==== Listener ==== | ==== Listener ==== | ||
- | This listener has very similar style to the TCP server we created. As usual it starts with the includes and constant declarations. | + | This listener has very similar style to the TCP server we created. The listener is a program that binds to a predefined port, and listens to any messages received on that port, like a " |
<code c> | <code c> | ||
Line 495: | Line 508: | ||
As with the others, make sure to include all of this in main( ), and return 0 at the end. | As with the others, make sure to include all of this in main( ), and return 0 at the end. | ||
+ | |||
+ | Here is what it looks like when the listener is launched: | ||
+ | |||
+ | {{ dylanw: | ||
+ | \\ | ||
+ | And here is what it looks like when the listener receives a message: | ||
+ | |||
+ | {{ dylanw: | ||
==== Talker ==== | ==== Talker ==== | ||
+ | The talker is probably the simplest program of all of these, with just 65 lines of code. The talker is a program that send given message to a predefined port on the given IP address. It doesn' | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define SERVERPORT " | ||
+ | </ | ||
+ | |||
+ | Unlike all of the other programs, no need to define a get_in_addr function, because we are simply sending a UDP packet to the given IP address at the defined port. | ||
+ | |||
+ | So we move onto the main( ) function, with command line arguments. We start off with the usual: declaring structures, checking argument count, filling out the IP address structure, and calling getaddrinfo for the given IP address. | ||
+ | |||
+ | <code c> | ||
+ | int sockfd; // Socket descriptor | ||
+ | struct addrinfo hints, *servinfo, *p; // Structs to store IP address info | ||
+ | int rv; // Error checking | ||
+ | int numbytes; // Number of bytes to send | ||
+ | |||
+ | if (argc != 3) { // Check command line arguments | ||
+ | fprintf(stderr," | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | memset(& | ||
+ | hints.ai_family = AF_UNSPEC; // We don't care if IPv4 or IPv6 (set to AF_INET to force IPv4) | ||
+ | hints.ai_socktype = SOCK_DGRAM; // UDP type | ||
+ | |||
+ | if ((rv = getaddrinfo(argv[1], | ||
+ | fprintf(stderr, | ||
+ | return 1; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Next, we loop through the servinfo linked-list trying to create a socket, and then check if we actually created one. | ||
+ | |||
+ | <code c> | ||
+ | // Loop through all the results and make a socket | ||
+ | for(p = servinfo; p != NULL; p = p-> | ||
+ | if ((sockfd = socket(p-> | ||
+ | p-> | ||
+ | perror(" | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | break; | ||
+ | } | ||
+ | |||
+ | if (p == NULL) { // Check if anything actually ever was created | ||
+ | fprintf(stderr, | ||
+ | return 2; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Finally, we attempt to send the message packet to the IP address given and the port defined, free the IP address structure, print the message detailing the number of bytes sent, and finally close the socket. | ||
+ | |||
+ | <code c> | ||
+ | if ((numbytes = sendto(sockfd, | ||
+ | | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | freeaddrinfo(servinfo); | ||
+ | |||
+ | printf(" | ||
+ | close(sockfd); | ||
+ | </ | ||
+ | |||
+ | As usual, make sure that the code besides the first block is within main( ), with Cl arguments, and you return 0 at the end. | ||
+ | |||
+ | Here is what it looks like when the talker is launched, and sends a message: | ||
+ | |||
+ | {{ dylanw: | ||
+ | |||
+ | |||
+ | This is basically it. Once you understand the basic network calls, and the looping structure, then it should be very easy to implement this yourself. Try out the program given yourself, and then try writing your own, and see if you can make it more efficient, or unique in your own way. Again, this is just the basics, and more difficult concepts will be covered in the future. | ||
===== Final Words ===== | ===== Final Words ===== |
tcp_udp.1493787427.txt.gz · Last modified: 2017/05/02 21:57 by dwallace