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