A small example of Linux socket

Source: Internet
Author: User

Server:

#include <stdlib.h>#include <sys/types.h>#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <string.h>int main(int argc, char** argv){    int serv_sock, cli_sock;    char buf[1024];    struct sockaddr_in seraddr, cliaddr;    if((serv_sock=socket(AF_INET, SOCK_STREAM, 0)) == -1){    printf("create socket fail\n");exit(1);    }     printf("create socket ok\n");        bzero(&seraddr, sizeof(struct sockaddr_in));    seraddr.sin_family = AF_INET;    seraddr.sin_addr.s_addr = htonl(INADDR_ANY);    seraddr.sin_port = htons(6666);    printf("s_addr = %#x ,port : %#x\r\n", seraddr.sin_addr.s_addr, seraddr.sin_port);    if(bind(serv_sock, (struct sockaddr*)(&seraddr), sizeof(struct sockaddr)) == -1){printf("bind socket fail\n");    exit(1);    }    printf("bind ok\n");    if(listen(serv_sock, 5) == -1){printf("listen socket fail\n");    }    printf("listen ok\n");    while(1){int len = sizeof(struct sockaddr);if((cli_sock = accept(serv_sock, (struct sockaddr*)(&cliaddr), &len)) == -1){printf("accept fail\n");exit(1);}    printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",ntohl(cliaddr.sin_addr.s_addr),ntohs(cliaddr.sin_port));   int ret = read(cli_sock, buf, sizeof(buf));printf("read from client %s\n", buf);    close(cli_sock);     }     close(serv_sock);     exit(0);}

Client:

#include <stdlib.h>#include <sys/types.h>#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <string.h>int main(){int sock;struct sockaddr_in ser_addr;char buff[1024];if((sock=socket(AF_INET, SOCK_STREAM, 0)) == -1){printf("client create socket fail\n");exit(1);}printf("create socket ok\n");bzero(&ser_addr, sizeof(struct sockaddr_in));ser_addr.sin_family = AF_INET;ser_addr.sin_port = htons(6666);;ser_addr.sin_addr.s_addr = inet_addr("192.168.1.104");if(connect(sock, (struct sockaddr *)(&ser_addr), sizeof(struct sockaddr)) == -1){printf("connect fail\n");exit(1);}printf("connect ok\n");//buff = {'a', 'b'};buff[0] = 'a';buff[1] = 'b';buff[2] = 'f';if(write(sock, buff, strlen(buff)) == -1){printf("write fail\n");exit(1);}printf("write ok\n");close(sock);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.