博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C socket demo
阅读量:6678 次
发布时间:2019-06-25

本文共 2896 字,大约阅读时间需要 9 分钟。

一、服务端-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 32 void 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 fprintf void 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); }  

转载地址:http://msyao.baihongyu.com/

你可能感兴趣的文章
理解单例模式
查看>>
从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑...
查看>>
软件工程结对作业01
查看>>
面向对象----方法的重载
查看>>
阿花宝宝 Java基础笔记 之 多态
查看>>
589. N叉树的前序遍历
查看>>
二项式展开
查看>>
推荐系统-03-简单基于用户的推荐
查看>>
Android scaleType属性与ImagView中图片的显示的关系
查看>>
十、cent OS开启APR模式报错:configure: error: Found APR 1.3.9. You need version 1.4.3 or newer installed...
查看>>
中文字符按拼音首字母排序(转)
查看>>
【mysql】一次有意思的数据库查询分析。
查看>>
C++获取window临时路径
查看>>
Python(面向对象编程—1)
查看>>
自己封装的数据通信服务组件DotNet.ServiceModel
查看>>
Docker创建虚机和swarm
查看>>
JSON入门学习
查看>>
一个很好的UML工具
查看>>
[转]无需看到你的脸就能认出你——实现Beyond Frontal Faces: Improving Person Recognition Using Multiple Cues...
查看>>
函数Curry化
查看>>