C language implementation of simple socket communication instance _c language

Source: Internet
Author: User

This article describes the C language to achieve a simple socket communication method, share for everyone to reference. The implementation methods are as follows:

The server-side code is as follows:

/* ============================================================================ name:server.c author:king Version: Copyright:your copyright Notice Description:hello World in C, Ansi-style =============================================
=============================== * #include <stdlib.h> #include <pthread.h> #include <sys/socket.h>  #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h>/inet (3) functions * * #include

<stdlib.h> #include <errno.h> #include <stdio.h> #include <string.h> int handle (int point); int main (void) {int SFD, ind; struct sockaddr_in addr; struct sockaddr_in clent; char resv[1024], sendbuf[1024]; char buf
[1024];

char * myaddr = "192.168.231.128"; int ret;
The return value is set socklen_t lent;
int pid; addr.sin_family = af_inet; IPV4 Internet Protocols Addr.sin_port = htons (5050); Here enter the server port number addr.sin_addr.s_addr = inet_addr (myaddr);; Inaddr_any represents the native IP//FETCH socket descriptor, IPV4ASD printf ("Socket start \ n");

SFD = socket (af_inet, sock_stream, 0);
if (SFD < 0) {printf ("Socket error \ n"); return-1} printf ("Bind start \ n"); Link the socket to the specified port if (bind (SFD, struct sockaddr *) &addr, sizeof (struct sockaddr)) < 0) {printf ("Bind error \ n"); ret
urn-1;
//Listener Socket printf ("Listen start \");

if (Listen (SFD, 1024) < 0) {printf ("Listen error \ n"); return-1;} for (;;) {//Accept information from the client printf ("Accept start \ n"); memset (&clent, 0, sizeof (clent)); lent = sizeof (clent); ind = Accept (SFD, (s
Truct sockaddr *) &clent, &lent);

if (Ind < 0) {printf ("Accept error%d \ n", Ind); return-1;}
printf ("infor \ n");

printf ("Clent addr%s porit%d\n", Inet_ntop (Af_inet, &clent.sin_addr, buf, sizeof (BUF)), Ntohs (Clent.sin_port));

PID = fork (); if (PID = = 0) {//subprocess close (SFD), handle (IND);} else if (PID < 0) {//error close (IND);} else {//parent process}} return EXI

t_success;

int handle (int point) {int retn; char buf[1024]; for (;;) {RETN = read (point, buf, sizeof (BUF)); if (Retn < 0) {printf ("read error \ n"); Close (point), break;} else if (Retn = = 0) {printf ("Client exit \ n");
OINT);
Break

printf ("client:%s\n", buf);
if (strcmp ("Exit", buf) = = 0) {printf ("Exit \ n"); close (point); return 0;}}
return 0;

 }

The

client code is as follows:

/* ============================================================================ name:client.c author:king Ve Rsion:Copyright:Your Copyright notice Description:hello World in C, Ansi-style =============================== ============================================= * * #include <stdio.h> #include <stdlib.h> #include <sys/

socket.h> #include <netinet/in.h> #include <arpa/inet.h>/* INET (3) functions */int handle (int fd);
  int main (void) {int nsd;

  Char buf[1024];
  char * myaddr = "192.168.231.128";

  struct sockaddr_in addr;
  printf ("Welcome to echo client\n");
  NSD = socket (af_inet, sock_stream, 0);
  printf ("Connect start1 \ n");
  Bzero (addr, sizeof (*ADDR));
  memset (&addr,0,sizeof (addr));
  printf ("Connect start2 \ n");
  addr.sin_family = af_inet;
  Addr.sin_port = htons (5050);

  ADDR.SIN_ADDR.S_ADDR=INET_ADDR (MYADDR);
  printf ("Connect start3 \ n"); if (Connect (NSD, struct sockaddr *) &addr, sizeof (StruCT sockaddr)) < 0) {printf ("Connect error \ n");
  return-1;
  Sleep (5);
  printf ("Handle start\n");
  Handle (NSD);
  Close (NSD);
return exit_success;

  int handle (int fd) {char sendl[1024], rev[1024];

  int retn; for (;;)
    {memset (sendl,0,sizeof (Sendl));
    memset (Rev. Rev,0,sizeof (rev));
    if (fgets (Sendl, 1024, stdin) = = NULL) {break;
    }//printf ("Wirte start\n");
    Write (FD, Sendl, strlen (Sendl));

  Read (FD, Rev,strlen (rev));
return 0;

 }

Attention:
int Connect (int sockfd, const struct SOCKADDR *addr,socklen_t Addrlen);
Remember, it must be a value addrlen.
Accept socklen_t *addrlen If a pointer

I hope this article will help you with the learning of C language network programming.

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.