C socket demo

來源:互聯網
上載者:User

標籤:os   art   cti   io   div   amp   

一、服務端-server.c

#include <stdio.h>#include <sys/socket.h>#include <arpa/inet.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <netinet/in.h>#define MAXPENDING 5#define BUFFSIZE 32void Die(char *mess){ perror(mess); exit(1);}void HandleClient(int sock){ char buffer[BUFFSIZE]; int received = -1; if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) { Die("Failed to recevie inital bytes from client"); } while (received > 0) { if (send(sock, buffer, received, 0) != received) { Die("Failed to send bytes to client"); } if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) { Die("Failed to receive additional bytes from client"); } } close(sock);}int main(int argc, char *argv[]){ int serversock, clientsock; struct sockaddr_in echoserver, echoclient; if (argc != 2) { fprintf(stderr, "USAGE: echoserver <port>/n"); exit(1); } if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { Die("Failed to create socket"); } memset(&echoserver, 0, sizeof(echoserver)); echoserver.sin_family = AF_INET; echoserver.sin_addr.s_addr = htonl(INADDR_ANY); echoserver.sin_port = htons(atoi(argv[1])); if (bind(serversock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) { Die("Failed to bind the server socket"); } if (listen(serversock, MAXPENDING) < 0) { Die("Failed to listen on server socket"); } while (1) { unsigned int clientlen = sizeof(echoclient); if ((clientsock = accept(serversock, (struct sockaddr *) &echoclient, &clientlen)) < 0 ) { Die("Failed to accept client connection"); } fprintf(stdout, "Client connected:%s/n", inet_ntoa(echoclient.sin_addr)); HandleClient(clientsock); }} 

 

二、用戶端-client.c

#include <stdio.h>#include <sys/socket.h>#include <arpa/inet.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <netinet/in.h>#define BUFFSIZE 32#define FP fprintfvoid Die(char *mess){ perror(mess); exit(1);}int main(int argc, char *argv[]){ int sock; struct sockaddr_in echoserver; char buffer[BUFFSIZE]; unsigned int echolen; int received = 0; if (argc != 4) { FP(stderr, "USAGE: TCP echo <server_ip> <word> <port>/n"); exit(1); } if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { Die("Failed to create socket"); } memset(&echoserver, 0, sizeof(echoserver)); echoserver.sin_family = AF_INET; echoserver.sin_addr.s_addr = inet_addr(argv[1]); echoserver.sin_port = htons(atoi(argv[3])); if (connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) { Die("Failed to connect with server"); } echolen = strlen(argv[2]); if (send(sock, argv[2], echolen, 0) != echolen) { Die("Mismatch in number of sent bytes"); } FP(stdout, "Received: "); while (received < echolen) { int bytes = 0; if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1) { Die("Failed to receive bytes from server"); } received += bytes; buffer[bytes] = ‘/0‘; FP(stdout, buffer); } FP(stdout, "/n"); close(sock); exit(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.