C Language Programming example of socket communication program based on TCP and UDP protocol _c language

Source: Internet
Author: User

TCP multithreaded Server and client program
Server program:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include < sys/types.h> #include <sys/socket.h> #include <unistd.h> #define PORT 8082 #define BufSize Char Buf[buf

SIZE+1];
    void* Fun (void* x) {//printf ("enter thread!\r\n");
  int new_fd=* ((int*) x); while (1) {int z=read (new_fd,buf,bufsize);//step 6th reads socket if (z==0) {printf ("Client close!");
  break;};
  Buf[z]= ' ";
printf ("%s\r\n", buf);//print};
int newfd[512];
int inewfd=0;
  int main () {//step 1th creates socket int sockfd=socket (af_inet,sock_stream,0);
  The 2nd step is to set the address structure body struct sockaddr_in svraddr;
  svraddr.sin_family=af_inet;//uses Internet Protocol svraddr.sin_port=htons (port);
  Inet_aton ("0.0.0.0", &svraddr.sin_addr);
    Step 3rd Binding int Ret=bind (SOCKFD, (struct sockaddr*) &svraddr,sizeof (SVRADDR));
  if (ret<0) {printf ("error bind!\r\n"); exit (-1);};
  4th Step Monitoring Listen (sockfd,128); while (1) {newfd[inewfd++]=accept (Sockfd,null,nulL);
    The 5th step receives pthread_t Ntid; 
  Pthread_create (&ntid,null,fun, (void*) & (Newfd[inewfd-1));

 }
}

Attention:

GCC Server.c-o server-lpthread

Client program CLI.C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h >
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BufSize
Char buf[bufsize+1];
int main ()
{
  //step 1th creates an individual socket
  int sockfd=socket (af_inet,sock_stream,0);
  The 2nd step is to set up the addr structure body
  struct sockaddr_in svraddr;
  svraddr.sin_family=af_inet;//uses Internet Protocol
  svraddr.sin_port=htons (port);
  Inet_aton ("127.0.0.1", &svraddr.sin_addr);
  The 3rd step connects server Connect
  (SOCKFD, (struct sockaddr*) &svraddr,sizeof (svraddr));
  while (1)
  {
  scanf ("%s", buf);
  Write (Sockfd,buf,strlen (BUF)); Step 4th writes the string to a socket
  }
}


server programs and client programs for UDP

Server program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h >
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BufSize
Char buf[bufsize+1];
int main ()
{
  //step 1th creates socket
  int sockfd=socket (af_inet,sock_dgram,0);
  The 2nd step is to set the address structure body
  struct sockaddr_in svraddr;
  svraddr.sin_family=af_inet;//uses Internet Protocol
  svraddr.sin_port=htons (port);
  Inet_aton ("0.0.0.0", &svraddr.sin_addr);
  Step 3rd binding
  int Ret=bind (SOCKFD, (struct sockaddr*) &svraddr,sizeof (svraddr));
  if (ret<0) {printf ("Cannot bind!\r\n"); exit ( -1);};
  while (1)
  {
        struct sockaddr_in cli;
        int len=sizeof (CLI);
    int Z=recvfrom (sockfd,buf,bufsize,0, (struct sockaddr*) &cli,&len);//6th step read socket  
    buf[z]= ' "";
    printf ("%s\r\n", buf);//Print
  }

}

Client program CLI.C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h >
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BufSize
Char buf[bufsize+1];
int main ()
{
  //step 1th creates an individual socket
  int sockfd=socket (af_inet,sock_dgram,0);
  The 2nd step is to set up the addr structure body
  struct sockaddr_in svraddr;
  svraddr.sin_family=af_inet;//uses Internet Protocol
  svraddr.sin_port=htons (port);
  Inet_aton ("127.0.0.1", &svraddr.sin_addr);
  The 3rd step connects
  the server//connect (SOCKFD, (struct sockaddr*) &svraddr,sizeof (svraddr));
  while (1)
  {
  scanf ("%s", buf);
  SendTo (Sockfd,buf,strlen (BUF), 0, (struct sockaddr*) &svraddr,sizeof (SVRADDR)); Step 4th writes the string to a socket
  }
}

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.