Socket server:tcp multi-threaded version

Source: Internet
Author: User
Tags htons

One, the server side

#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include

        <netinet/in.h> int main (int argc, char*argv[]) {int SOCKFD = socket (af_inet, sock_stream, 0);
        struct sockaddr_in addrsrv;
        Bzero (&addrsrv, sizeof (struct sockaddr_in));
        ADDRSRV.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
        addrsrv.sin_family = af_inet;
        Addrsrv.sin_port = htons (6000);

        Bind (SOCKFD, (struct sockaddr*) &addrsrv, sizeof (struct sockaddr));

        Listen (SOCKFD, 3);
        struct sockaddr_in addrconn;
        socklen_t len = sizeof (struct sockaddr_in);

                while (1) {int sockconn = accept (SOCKFD, (struct sockaddr*) &addrconn, &len);
                pid_t pid = fork ();
                        if (PID = = 0) {close (SOCKFD); while (1) {char buf[1024] = {0};
                                int n = read (Sockconn, buf, 1024);
                                if (n = = 0) {break;
                                printf ("Server recv:%s\n", buf);
                        Write (Sockconn, "Server Data", strlen ("Server data");
                Close (Sockconn);
                else if (PID > 0) {close (sockconn);

        } close (SOCKFD);
return 0; }

Second, client side

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h >
#include <netinet/in.h>

int main (int argc, char*argv[])
{
        int sockfd = socket (af_inet, sock _stream, 0);

        struct sockaddr_in addrsrv;
        Bzero (&addrsrv, sizeof (struct sockaddr_in));
        ADDRSRV.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");
        addrsrv.sin_family = af_inet;
        Addrsrv.sin_port = htons (6000);
        Connect (SOCKFD, (struct sockaddr*) &addrsrv, sizeof (struct sockaddr));

        Char temp[1024] = {0};
        while (1)
        {
                gets (temp);
                Write (SOCKFD, temp, strlen (temp));

                Char buf[1024] = {0};
                Read (SOCKFD, buf, 1024);
                printf ("Client recv:%s\n", buf);
        }


        Close (SOCKFD);

        return 0;
}

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.