Linux under Socket programming example __HTML5

Source: Internet
Author: User
Tags htons

This example is a client/server structure that demonstrates how the client establishes a connection and sends data to the remote end, how the Server listens to the system connection request, receives the request and establishes the connection, and obtains the data from the client. Although the code is short, but describes the entire communication process, Linux can be programmed under the network to play a role bar:-)

Client-side code:
--------------------------------------------------------------
/* sockclnt.c*/
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>/*for struct sockaddr_in*/

#define DEST_IP "65.52.207.217"
#define DEST_PORT 4000

int main ()
{
int res;
int sockfd;
struct sockaddr_in dest_addr;

Char *msg = "Hello world\n";
int Len, bytes_sent;

/* Get a socket * *
SOCKFD = socket (af_inet, sock_stream, 0);
if (sockfd = = 1) {
Perror ("socket ()");
Exit (1);
}

/* Set remote connection information.
dest_addr.sin_family = af_inet; /* Note Host byte order * *
Dest_addr.sin_port = htons (Dest_port); /* Remote connection port, note network byte order *
DEST_ADDR.SIN_ADDR.S_ADDR = inet_addr (DEST_IP); /* Remote IP Address, inet_addr () will return the network byte order * *
Bzero (& (Dest_addr.sin_zero), 8); * * The rest of the structure must be placed 0*/

/* Connect remote host, error return -1*/
res = connect (sockfd, (struct sockaddr *) &dest_addr, sizeof (struct sockaddr_in));
if (res = = 1) {
Perror ("Connect ()");
Exit (1);
}

Len = strlen (msg);
Bytes_sent = Send (SOCKFD,/* Connection Descriptor * *
MSG,/* Send content * *
Len,/* Content Length/*
0); /* Send tag, general set 0*/

/* Close Connection * *
Close (SOCKFD);
}


Server-Side code:
-----------------------------------------------------------------------------------------
/* socksrv.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>/* for struct sockaddr_in*/

#define BACKLOG 10
#define MYPORT 4000

int main ()
{
Char *addr;
int sockfd;
int new_fd;
struct sockaddr_in my_addr, their_addr;

int res;
int sin_size;

Char *buf;
/* Get SOCKET Descriptor * *
SOCKFD = socket (af_inet,/* domain*/
Sock_stream, * type*/
0); /* protocol*/

if (sockfd = = 1) {
Perror ("socket");
Exit (1);
}

/* Init sockaddr_in * *
my_addr.sin_family = af_inet; /* Note: Host byte order should be used * *
My_addr.sin_port = htons (MyPort); /* Note: network byte order should be used * *
MY_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any); /* Use your own IP address */
Bzero (& (My_addr.sin_zero), 8); /* The rest of the structure must be placed 0*/

/* Specify the address and port to use for a socket * *
res = bind (SOCKFD, (struct sockaddr*) &my_addr, sizeof (struct sockaddr));
if (res = = 1) {
Perror ("bind");
Exit (1);
}

/* Listening request, waiting for connection * *
res = Listen (SOCKFD,
BACKLOG); /* The maximum number of unhandled connection request queues to hold * *
if (res = = 1) {
Perror ("Listen");
Exit (1);
}

/* Accept each other's connection request, establish a connection, and return a new connection descriptor.
* and the first socket descriptor is still on your machine the original port listen ()
*/
sin_size = sizeof (struct sockaddr_in);
NEW_FD = Accept (SOCKFD, (void *) &their_addr, &sin_size);

BUF = (char *) malloc (255);
if (buf = = NULL) {
printf ("malloc failed\n");
Exit (1);
}

* * Accept the data sent from the other party.
res = recv (new_fd, buf, 255, 0);
if (res = = 1) {
Perror ("recv ()");
Exit (1);
}

/* Close this connection * *
Close (NEW_FD);

/* Shut down System Monitor/*
Close (SOCKFD);

printf ("Recv data:%s\n", buf);
Free (BUF);
return 0;
}

Compile:
------------------------------------------------
Gcc-o sockclnt sockclnt.c
Gcc-o Socksrv socksrv.c

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.