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

最新文章

  1. ORM之殇,我们需要什么样的ORM框架?
  2. 本机,同机房,同城,异地,不同城,腾讯云ping延时值
  3. C语言宏定义时#(井号)和##(双井号)的用法1
  4. js小效果-双色球
  5. 深入SpringBoot:自定义Endpoint
  6. 2015弱校联盟(1) -A. Easy Math
  7. 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu
  8. iOS-多线程之NSOperation
  9. 多个list合并
  10. SpinLock 实现
  11. FineUI上传控件
  12. [转载] 50个Android开发人员必备UI效果源码
  13. UVA 10391 Compound Words
  14. Eclipse的Spring IDE插件的安装和使用
  15. vue插件编写与实战
  16. MVPHelper更新日志 --- 新增常规分包模式
  17. Android使用google breakpad捕获分析native cash
  18. 认识多线程中start和run方法的区别?
  19. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
  20. Linux 系统目录

热门文章

  1. mysql sql灵活运用
  2. CF Rook, Bishop and King
  3. android fragment嵌套fragment出现的问题:no activity
  4. Android include和merge标签的使用
  5. tbody添加垂直滚动条
  6. Android 内核初识(8)Binder
  7. hadoop异常: 到目前为止解决的最牛逼的一个异常(java.io.IOException: Incompatible clusterIDs)
  8. 宣布正式发布 Azure 媒体服务内容保护服务
  9. 谈谈分布式事务之二:基于DTC的分布式事务管理模型[下篇]
  10. BZOJ2818: Gcd 莫比乌斯反演