linux c socket 源碼案例

來源:互聯網
上載者:User

標籤:socket   c   linux   

service端


#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/shm.h>#define MYPORT  8887#define QUEUE   20#define BUFFER_SIZE 1024int main(){    ///定義sockfd    int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);    ///定義sockaddr_in    struct sockaddr_in server_sockaddr;    server_sockaddr.sin_family = AF_INET;    server_sockaddr.sin_port = htons(MYPORT);    server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);    ///bind,成功返回0,出錯返回-1    if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)    {        perror("bind");        exit(1);    }    ///listen,成功返回0,出錯返回-1    if(listen(server_sockfd,QUEUE) == -1)    {        perror("listen");        exit(1);    }    ///用戶端通訊端    char buffer[BUFFER_SIZE];    struct sockaddr_in client_addr;    socklen_t length = sizeof(client_addr);    ///成功返回非負描述字,出錯返回-1    int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length);    if(conn<0)    {        perror("connect");        exit(1);    }    while(1)    {        memset(buffer,0,sizeof(buffer));        int len = recv(conn, buffer, sizeof(buffer),0);        if(strcmp(buffer,"exit\n")==0)            break;        fputs(buffer, stdout);        send(conn, buffer, len, 0);    }    close(conn);    close(server_sockfd);    return 0;}




client端
#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/shm.h>#define MYPORT  8887#define BUFFER_SIZE 1024int main(){    ///定義sockfd    int sock_cli = socket(AF_INET,SOCK_STREAM, 0);    ///定義sockaddr_in    struct sockaddr_in servaddr;    memset(&servaddr, 0, sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_port = htons(MYPORT);  ///伺服器連接埠    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  ///伺服器ip    ///串連伺服器,成功返回0,錯誤返回-1    if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)    {        perror("connect");        exit(1);    }    char sendbuf[BUFFER_SIZE];    char recvbuf[BUFFER_SIZE];    while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)    {        send(sock_cli, sendbuf, strlen(sendbuf),0); ///發送        if(strcmp(sendbuf,"exit\n")==0)            break;        recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收        fputs(recvbuf, stdout);        memset(sendbuf, 0, sizeof(sendbuf));        memset(recvbuf, 0, sizeof(recvbuf));    }    close(sock_cli);    return 0;}


測試:





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.