Socketapi: One of the simplest servers and the corresponding client C language implementation

Source: Internet
Author: User
Tags htons

based on Linux, this instance implements a service-side pass on a hello world to the client.
Socket () Create socket
Bind () Bind socket to IP address and port
Listen () server listens for client connections
Connect () client connects to server
The Accept () application accepts a client connection that completes a 3-time handshake
Send () recv () write () read () between machines sending data to each other
Close () closes the socket
gethostbyname () gethostbyaddr () V4 Proprietary
Select () poll () handles read, write, and error states for multiple connections
GetSockOpt () Get the option value of the corresponding socket
SetSockOpt () Sets the option value for the corresponding socket


The specific code is as follows:


Service-side section:

#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 (int argc, char** argv)
{
Char hello[] = "Hello World";
struct sockaddr_in sa;
int SOCKETFD = socket (pf_inet, sock_stream, ipproto_tcp);

if ( -1 = = socketfd) {
Perror ("Cannot create socket");
Exit (Exit_failure);
}
memset (&sa, 0, sizeof SA);

sa.sin_family = af_inet;
Sa.sin_port = htons (2222);
SA.SIN_ADDR.S_ADDR = htonl (Inaddr_any);

if ( -1 = = Bind (socketfd, (struct sockaddr*) &sa, sizeof sa)) {
Perror ("Bind failed");
Close (SOCKETFD);
Exit (Exit_failure);
}
if ( -1 = = Listen (SOCKETFD, 10)) {
Perror ("Listen Failed");
Close (SOCKETFD);
Exit (Exit_failure);

}

for (;;) {
int connectfd = accept (SOCKETFD, NULL, NULL);

if (0 > Connectfd) {
Perror ("Accept failed");
Close (SOCKETFD);
Exit (Exit_failure);
}

int writesize = 0;
size_t totalwrite = 0;
while (Totalwrite < sizeof (hello)) {
Writesize =
Write (connectfd, hello + totalwrite, sizeof (hello)-totalwrite);
if ( -1 = = writesize) {
Perror ("Write Failed");
Close (CONNECTFD);
Close (SOCKETFD);
Exit (Exit_failure);
}
Totalwrite + = Writesize;
}
if ( -1==shutdown (Connectfd,shut_rdwr)) {
Perror ("Shutdown failed");
Close (CONNECTFD);
Close (SOCKETFD);
Exit (Exit_failure);
}
Close (CONNECTFD);
}
Close (SOCKETFD);
return exit_success;
}


Client section:

#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 (int argc, char** argv)
{
Char hello[] = "Hello World";
struct sockaddr_in sa;
int SOCKETFD = socket (pf_inet, sock_stream, ipproto_tcp);

if ( -1 = = socketfd) {
Perror ("Cannot create socket");
Exit (Exit_failure);
}
memset (&sa, 0, sizeof SA);

sa.sin_family = af_inet;
Sa.sin_port = htons (2222);
res = Inet_pton (af_inet, "127.0.0.1", &sa.sin_addr);

if ( -1 = = Connect (socketfd, (struct sockaddr*) &sa, sizeof sa)) {
Perror ("Connect failed");
Close (SOCKETFD);
Exit (Exit_failure);
}
Char buffer[512];
int totalread = 0;
for (;;) {
int readsize = 0;
ReadSize = Read (socketfd, buffer + totalread, sizeof (buffer)-totalread);
if (readsize = = 0) {
Break
}
else if (readsize = =-1) {
Perroe ("read failed");
Close (SOCKETFD);
Exit (Exit_failure);
}
Totalread + = ReadSize;
}
Buffer[totalread] = 0;
printf ("Get from server:%s\n", buffer);

(void) shutdown (SOCKETFD, shut_rdwr);
Close (SOCKETFD);
return exit_success;
}


Which side of the client and service side actively calls the close function, which side enters the time_wait

Socketapi: One of the simplest servers and the corresponding client C language implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.