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 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>
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