Socket set up simple top C/s program

Source: Internet
Author: User

Server-side

 
   #include  <stdio.h>
#include <arpa/inet.h>
#include  <sys/socket.h>
#include  <cstring>
#include   <sys/types.h>
#include   <stdlib.h>
#include   <netinet/in.h>
#include   <unistd.h>
int main (void)
{
    printf ("a1\n");
    int listenfd;//Create a socket
   if ((Listenfd=socket (PF_INET,SOCK_STREAM,IPPROTO_TCP)) <0)
       printf ("Socket errer");
    struct sockaddr_in servaddr;
    memset (&servaddr,0,sizeof (SERVADDR));
    Servaddr.sin_family=af_inet;
    Servaddr.sin_port=htons (5188);
    Servaddr.sin_addr.s_addr=htonl (Inaddr_any);//Initialize Address
    int on=1;
    if (setsockopt (listenfd,sol_socket,so_reuseaddr,&on,sizeof (ON)) <0)//Monitor
        printf ("setsockopt error");
if (bind (LISTENFD, struct sockaddr *) &servaddr,sizeof (SERVADDR))///BIND address
        printf ("Bind Errer");
    if (Listen (listenfd,somaxconn) <0)
        printf ("Listen Errer");
    struct sockaddr_in peeraddr;
    socklen_t peerlen=sizeof (PEERADDR);
    int conn;
    if ((Conn=accept (LISTENFD, (struct sockaddr*) &peeraddr,&peerlen) <0)
        printf ("Accept errer");
    printf ("ip=%s port=%d\n", Inet_ntoa (PEERADDR.SIN_ADDR), Ntohs (Peeraddr.sin_port));
    Char recvbuf[1024];
    while (1) {
        memset (recvbuf,0,sizeof (RECVBUF));
        int  ret =read (conn,recvbuf,sizeof (RECVBUF));
        Fputs (recvbuf,stdout);
        Write (Conn,recvbuf,ret);
    }
    Close (conn);
    Close (LISTENFD);
     printf ("a2\n");
    return 0;
}
Client
 
   #include  <stdio.h>
#include <arpa/inet.h>
#include  <sys/socket.h>
#include  <cstring>
#include   <sys/types.h>
#include   <stdlib.h>
#include   <netinet/in.h>
#include   <unistd.h>
int main (void)
{
    printf ("b1\n");
    int sock;
   if ((Sock=socket (PF_INET,SOCK_STREAM,IPPROTO_TCP)) <0)
       printf ("Socket errer");
    struct sockaddr_in servaddr;
    memset (&servaddr,0,sizeof (SERVADDR));
    Servaddr.sin_family=af_inet;
    Servaddr.sin_port=htons (5188);
    SERVADDR.SIN_ADDR.S_ADDR=INET_ADDR ("127.0.0.1");
   if (Connect (sock, (struct sockaddr*) &servaddr,sizeof (servaddr)) <0)
       printf ("Connect Errer");
   Char sendbuf[1024]={0};
   Char recvbuf[1024]={0};
   while (Fgets (sendbuf,sizeof (SENDBUF), stdin)!=null)
   {
       Write (Sock,sendbuf,strlen (SENDBUF));
       Read (Sock,recvbuf,strlen (RECVBUF));
        Fputs (recvbuf,stdout);
   }
   Close (sock);
    printf ("b2\n");
    return 0;
}


Socket set up simple top C/s program

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.