Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Berkeley sockets
(section)
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 TCP== The [[Transmission Control Protocol]] (TCP) is a [[Connection-oriented communication|connection-oriented]] protocol that provides a variety of error correction and performance features for transmission of byte streams. A process creates a TCP socket by calling the {{code|socket()}} function with the parameters for the protocol family ({{mono|''[[PF INET]]'', PF_INET6}}), the socket mode for [[stream socket]]s ({{mono|SOCK_STREAM}}), and the IP protocol identifier for TCP ({{mono|IPPROTO_TCP}}). ===Server=== Establishing a TCP server involves the following basic steps: * Creating a TCP socket with a call to ''socket().'' * Binding the socket to the listening port ''bind()'' after setting the port number. * Preparing the socket to listen for connections (making it a listening socket), with a call to ''listen()''. * Accepting incoming connections (''accept()''). This blocks the process until an incoming connection is received, and returns a socket descriptor for the accepted connection. The initial descriptor remains a listening descriptor, and ''accept()'' can be called again at any time with this socket, until it is closed. * Communicating with the remote host with the API functions ''send()'' and ''recv()'', as well as with the general-purpose functions ''write()'' and ''read()''. * Closing each socket that was opened after use with function ''close()'' The following program creates a TCP server listening on port number 1100: <syntaxhighlight lang="c"> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(void) { struct sockaddr_in sa; int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (SocketFD == -1) { perror("cannot create socket"); exit(EXIT_FAILURE); } memset(&sa, 0, sizeof sa); sa.sin_family = AF_INET; sa.sin_port = htons(1100); sa.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(SocketFD,(struct sockaddr *)&sa, sizeof sa) == -1) { perror("bind failed"); close(SocketFD); exit(EXIT_FAILURE); } if (listen(SocketFD, 10) == -1) { perror("listen failed"); close(SocketFD); exit(EXIT_FAILURE); } for (;;) { int ConnectFD = accept(SocketFD, NULL, NULL); if (ConnectFD == -1) { perror("accept failed"); close(SocketFD); exit(EXIT_FAILURE); } /* perform read write operations ... read(ConnectFD, buff, size) */ if (shutdown(ConnectFD, SHUT_RDWR) == -1) { perror("shutdown failed"); close(ConnectFD); close(SocketFD); exit(EXIT_FAILURE); } close(ConnectFD); } close(SocketFD); return EXIT_SUCCESS; } </syntaxhighlight> ===Client=== Programming a TCP client application involves the following steps: * Creating a TCP socket. * Connecting to the server (''connect()''), by passing a {{code|sockaddr_in}} structure with the {{code|sin_family}} set to {{mono|AF_INET}}, {{code|sin_port}} set to the port the endpoint is listening (in network byte order), and {{code|sin_addr}} set to the IP address of the listening server (also in network byte order). * Communicating with the remote host with the API functions ''send()'' and ''recv()'', as well as with the general-purpose functions ''write()'' and ''read()''. * Closing each socket that was opened after use with function ''close().'' <syntaxhighlight lang="c"> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(void) { struct sockaddr_in sa; int res; int SocketFD; SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (SocketFD == -1) { perror("cannot create socket"); exit(EXIT_FAILURE); } memset(&sa, 0, sizeof sa); sa.sin_family = AF_INET; sa.sin_port = htons(1100); res = inet_pton(AF_INET, "192.168.1.3", &sa.sin_addr); if (connect(SocketFD, (struct sockaddr *)&sa, sizeof sa) == -1) { perror("connect failed"); close(SocketFD); exit(EXIT_FAILURE); } /* perform read write operations ... */ close(SocketFD); return EXIT_SUCCESS; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)