Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Berkeley sockets
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Client-server example using UDP== The [[User Datagram Protocol]] (UDP) is a [[connectionless]] protocol with no guarantee of delivery. UDP packets may arrive out of order, multiple times, or not at all. Because of this minimal design, UDP has considerably less overhead than TCP. Being connectionless means that there is no concept of a stream or permanent connection between two hosts. Such data are referred to as datagrams ([[datagram socket]]s). UDP address space, the space of UDP port numbers (in ISO terminology, the [[TSAP]]s), is completely disjoint from that of TCP ports. ===Server=== An application may set up a UDP server on port number 7654 as follows. The programs contains an infinite loop that receives UDP datagrams with function ''recvfrom()''. <syntaxhighlight lang="c" highlight="32,25,24"> #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <unistd.h> /* for close() for socket */ #include <stdlib.h> int main(void) { int sock; struct sockaddr_in sa; char buffer[1024]; ssize_t recsize; socklen_t fromlen; memset(&sa, 0, sizeof sa); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY); sa.sin_port = htons(7654); fromlen = sizeof sa; sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (bind(sock, (struct sockaddr *)&sa, sizeof sa) == -1) { perror("error bind failed"); close(sock); exit(EXIT_FAILURE); } for (;;) { recsize = recvfrom(sock, (void*)buffer, sizeof buffer, 0, (struct sockaddr*)&sa, &fromlen); if (recsize < 0) { fprintf(stderr, "%s\n", strerror(errno)); exit(EXIT_FAILURE); } printf("recsize: %d\n ", (int)recsize); sleep(1); printf("datagram: %.*s\n", (int)recsize, buffer); } } </syntaxhighlight> ===Client=== The following is a client program for sending a UDP packet containing the string "Hello World!" to address 127.0.0.1 at port number 7654. <syntaxhighlight lang="c" highlight="40"> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <unistd.h> #include <arpa/inet.h> int main(void) { int sock; struct sockaddr_in sa; int bytes_sent; char buffer[200]; strcpy(buffer, "hello world!"); /* create an Internet, datagram, socket using UDP */ sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock == -1) { /* if socket failed to initialize, exit */ printf("Error Creating Socket"); exit(EXIT_FAILURE); } /* Zero out socket address */ memset(&sa, 0, sizeof sa); /* The address is IPv4 */ sa.sin_family = AF_INET; /* IPv4 addresses is a uint32_t, convert a string representation of the octets to the appropriate value */ sa.sin_addr.s_addr = inet_addr("127.0.0.1"); /* sockets are unsigned shorts, htons(x) ensures x is in network byte order, set the port to 7654 */ sa.sin_port = htons(7654); bytes_sent = sendto(sock, buffer, strlen(buffer), 0,(struct sockaddr*)&sa, sizeof sa); if (bytes_sent < 0) { printf("Error sending packet: %s\n", strerror(errno)); exit(EXIT_FAILURE); } close(sock); /* close the socket */ return 0; } </syntaxhighlight> In this code, ''buffer'' is a pointer to the data to be sent, and ''buffer_length'' specifies the size of the data.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Berkeley sockets
(section)
Add topic