都是最简单的用来记忆。

this is my 的git地址:https://github.com/yanjinyun/cLanguageTcpUdp

tcp最简单的服务器:

int main(int argc, const char *argv[])
{
int listenfd, acceptfd;
struct sockaddr_in sin, cin;
socklen_t clen;
char buf[]; signal(SIGCHLD, SIG_IGN); listenfd = socket(PF_INET, SOCK_STREAM, ); sin.sin_family = PF_INET;
sin.sin_port = htons(atoi(argv[]));
sin.sin_addr.s_addr = inet_addr(argv[]);
bind(listenfd, (struct sockaddr *)&sin, sizeof(sin)); listen(listenfd, ); while()
{
clen = sizeof(cin);
printf("listen ......\n");
acceptfd = accept(listenfd, (struct sockaddr *)&cin, &clen);
printf("client %s %d connect\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port)); if(fork() == )
{
close(listenfd); while()
{
bzero(buf, sizeof(buf));
if(recv(acceptfd, buf, sizeof(buf), ) == )
{
printf("client %s %d exit\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));
exit();
}
printf("recv(%s %d): %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
}
} close(acceptfd);
} return ;
}

tcp最简单的客户端:

int main(int argc, const char *argv[])
{
int cfd = socket(PF_INET, SOCK_STREAM, );
struct sockaddr_in cin;
char buf[]; cin.sin_family = PF_INET;
cin.sin_port = htons(atoi(argv[]));
cin.sin_addr.s_addr = inet_addr(argv[]);
connect(cfd, (struct sockaddr *)&cin, sizeof(cin)); printf("connect ip(%s) port(%s) server ok\n", argv[], argv[]); if(fork() == )
{
while()
{
bzero(buf, sizeof(buf));
recv(cfd, buf, sizeof(buf), );
printf("recv: %s\n", buf);
}
} while()
{
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-] = '\0';
send(cfd, buf, strlen(buf)+, );
} return ;
}

udp的最简单的服务器:

int main(int argc, const char *argv[])
{
int sfd;
struct sockaddr_in sin, cin;
char buf1[1024], buf2[1024], buf3[1024];
socklen_t clen; sfd = socket(PF_INET, SOCK_DGRAM, 0); sin.sin_family = PF_INET;
sin.sin_port = htons(atoi(argv[2]));
sin.sin_addr.s_addr = inet_addr(argv[1]);
bind(sfd, (struct sockaddr *)&sin, sizeof(sin)); if(fork() == 0)
{
while(1)
{
clen = sizeof(cin);
bzero(buf1, sizeof(buf1));
recvfrom(sfd, buf1, sizeof(buf1), 0, (struct sockaddr *)&cin, &clen);
printf("recvfrom: %s %d %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf1);
}
} while(1)
{
fscanf(stdin, "%s%s%s", buf1, buf2, buf3);
cin.sin_family = PF_INET;
cin.sin_port = htons(atoi(buf2));
cin.sin_addr.s_addr = inet_addr(buf1);
sendto(sfd, buf3, strlen(buf3)+1, 0, (struct sockaddr *)&cin, sizeof(cin));
} return 0;
}

udp最简单的客户端:

int main(int argc, const char *argv[])
{
int sfd;
struct sockaddr_in cin;
char buf1[1024], buf2[1024], buf3[1024];
socklen_t clen; sfd = socket(PF_INET, SOCK_DGRAM, 0); if(fork() == 0)
{
while(1)
{
clen = sizeof(cin);
bzero(buf1, sizeof(buf1));
recvfrom(sfd, buf1, sizeof(buf1), 0, (struct sockaddr *)&cin, &clen);
printf("recvfrom: %s %d %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf1);
}
} while(1)
{
fscanf(stdin, "%s%s%s", buf1, buf2, buf3);
cin.sin_family = PF_INET;
cin.sin_port = htons(atoi(buf2));
cin.sin_addr.s_addr = inet_addr(buf1);
sendto(sfd, buf3, strlen(buf3)+1, 0, (struct sockaddr *)&cin, sizeof(cin));
} return 0;
}

ftp最简单的服务器器:

#define N 256

void list(int connectfd)
{
DIR *dir;
struct dirent *d;
char buf[N]; if((dir=opendir((const char *)get_current_dir_name())) == NULL)
{
perror("opendir");
exit(1);
}
while((d=readdir(dir)) != NULL)
{
if(d->d_name[0] == '.')
{
continue;
}
strcpy(buf, d->d_name);
send(connectfd, buf, N, 0);
} closedir(dir);
} void get(int connectfd, char *filename)
{
char buf[N];
FILE *fp;
int n; if((fp=fopen(filename, "r")) == NULL)
{
strcpy(buf, "no");
send(connectfd, buf, sizeof(buf), 0);
} strcpy(buf, "yes");
send(connectfd, buf, sizeof(buf), 0); while((n=fread(buf, 1, sizeof(buf), fp)) > 0)
{
send(connectfd, buf, n, 0);
} fclose(fp);
} void put(int connectfd, char *filename)
{
char buf[N];
FILE *fp;
int n; if((fp=fopen(filename, "r")) == NULL)
{
strcpy(buf, "no");
send(connectfd, buf, sizeof(buf), 0);
fp = fopen(filename, "w");
while((n=recv(connectfd, buf, sizeof(buf), 0)) > 0)
{
fwrite(buf, 1, n, fp);
}
fclose(fp); return;
} strcpy(buf, "yes");
send(connectfd, buf, sizeof(buf), 0);
} int main(int argc, const char *argv[])
{
FILE *fp;
int n, listenfd, connectfd, i;
struct sockaddr_in s, c;
struct servent *sport;
socklen_t clen;
char *linep = NULL, buf[N], *arg[32]; signal(SIGCHLD, SIG_IGN); if((fp=fopen("/etc/ftp.conf", "r")) == NULL)
{
perror("fopen");
exit(1);
} if(getline(&linep, &n, fp) == -1)
{
perror("getline");
exit(0);
}
linep[strlen(linep)-1] = '\0';
chdir(linep); if((sport=getservbyname("myftp", "tcp")) == NULL)
{
perror("getservbyname");
exit(1);
} #ifdef DEBUG
printf("%d %s\n", n, linep);
#endif if((listenfd=socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
} s.sin_family = PF_INET;
s.sin_port = sport->s_port;
s.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listenfd, (struct sockaddr *)&s, sizeof(s)) == -1)
{
perror("bind");
exit(1);
} listen(listenfd, 5); while(1)
{
clen = sizeof(c);
if((connectfd=accept(listenfd, (struct sockaddr *)&c, &clen)) == -1)
{
perror("accept");
exit(1);
} if(fork() == 0)
{
close(listenfd); recv(connectfd, buf, sizeof(buf), 0); arg[0] = strtok(buf, " ");
for(i=1; arg[i-1]!=NULL; i++)
{
arg[i]=strtok(NULL, " ");
} if(strcmp(arg[0], "list") == 0)
{
list(connectfd);
} if(strcmp(arg[0], "get") == 0)
{
get(connectfd, arg[1]);
} if(strcmp(arg[0], "put") == 0)
{
put(connectfd, arg[1]);
} close(connectfd);
exit(0);
} close(connectfd);
}
}

  

  

ftp最简单的客户端器:

#define N 256

void list(int connectfd)
{
DIR *dir;
struct dirent *d;
char buf[N]; if((dir=opendir((const char *)get_current_dir_name())) == NULL)
{
perror("opendir");
exit(1);
}
while((d=readdir(dir)) != NULL)
{
if(d->d_name[0] == '.')
{
continue;
}
strcpy(buf, d->d_name);
send(connectfd, buf, N, 0);
} closedir(dir);
} void get(int connectfd, char *filename)
{
char buf[N];
FILE *fp;
int n; if((fp=fopen(filename, "r")) == NULL)
{
strcpy(buf, "no");
send(connectfd, buf, sizeof(buf), 0);
} strcpy(buf, "yes");
send(connectfd, buf, sizeof(buf), 0); while((n=fread(buf, 1, sizeof(buf), fp)) > 0)
{
send(connectfd, buf, n, 0);
} fclose(fp);
} void put(int connectfd, char *filename)
{
char buf[N];
FILE *fp;
int n; if((fp=fopen(filename, "r")) == NULL)
{
strcpy(buf, "no");
send(connectfd, buf, sizeof(buf), 0);
fp = fopen(filename, "w");
while((n=recv(connectfd, buf, sizeof(buf), 0)) > 0)
{
fwrite(buf, 1, n, fp);
}
fclose(fp); return;
} strcpy(buf, "yes");
send(connectfd, buf, sizeof(buf), 0);
} int main(int argc, const char *argv[])
{
FILE *fp;
int n, listenfd, connectfd, i;
struct sockaddr_in s, c;
struct servent *sport;
socklen_t clen;
char *linep = NULL, buf[N], *arg[32]; signal(SIGCHLD, SIG_IGN); if((fp=fopen("/etc/ftp.conf", "r")) == NULL)
{
perror("fopen");
exit(1);
} if(getline(&linep, &n, fp) == -1)
{
perror("getline");
exit(0);
}
linep[strlen(linep)-1] = '\0';
chdir(linep); if((sport=getservbyname("myftp", "tcp")) == NULL)
{
perror("getservbyname");
exit(1);
} #ifdef DEBUG
printf("%d %s\n", n, linep);
#endif if((listenfd=socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
} s.sin_family = PF_INET;
s.sin_port = sport->s_port;
s.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listenfd, (struct sockaddr *)&s, sizeof(s)) == -1)
{
perror("bind");
exit(1);
} listen(listenfd, 5); while(1)
{
clen = sizeof(c);
if((connectfd=accept(listenfd, (struct sockaddr *)&c, &clen)) == -1)
{
perror("accept");
exit(1);
} if(fork() == 0)
{
close(listenfd); recv(connectfd, buf, sizeof(buf), 0); arg[0] = strtok(buf, " ");
for(i=1; arg[i-1]!=NULL; i++)
{
arg[i]=strtok(NULL, " ");
} if(strcmp(arg[0], "list") == 0)
{
list(connectfd);
} if(strcmp(arg[0], "get") == 0)
{
get(connectfd, arg[1]);
} if(strcmp(arg[0], "put") == 0)
{
put(connectfd, arg[1]);
} close(connectfd);
exit(0);
} close(connectfd);
}
}

c语言的tcp和udp客户端和服务器的更多相关文章

  1. python网络编程socket编程(TCP、UDP客户端服务器)

    摘录 python核心编程 使用socket()模块函数创建套接字——通信端点 >>> from socket import * >>> tcpSock = soc ...

  2. UNIX网络编程——使用select函数的TCP和UDP回射服务器程序

    服务器程序: #include <sys/wait.h> #include <string.h> #include <string.h> #include < ...

  3. C语言Socket-模拟远程CMD(客户端向服务器发送命令,服务器执行该命令)

    服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...

  4. TCP/UDP客户端

    Python 网络编程----模块socekt 在渗透测试的过程中,经常会遇到需要创建一个TCP客户端来连接服务器.发送垃圾数据.进行模糊测试活进行其他任务的情况. 简单的TCP客户端代码: #!/u ...

  5. LwIP应用开发笔记之三:LwIP无操作系统UDP客户端

    前一节我们实现了基于RAW API的UDP服务器,在接下来,我们进一步利用RAW API实现UDP客户端. 1.UDP协议简述 UDP协议全称是用户数据报协议,在网络中它与TCP协议一样用于处理数据包 ...

  6. Windows下C语言的Socket编程例子(TCP和UDP)

    原文:Windows下C语言的Socket编程例子(TCP和UDP) 刚刚学windows编程,所以想写学习笔记,这是一个简单的Socket程序例子,开发环境是vc6: 首先是TCP server端: ...

  7. 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤

    福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑         Java全栈大联盟   ...

  8. TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端

    目录 说明 TCP/UDP通信主要结构 管理多个Socket的解决方案 框架中TCP部分的使用 框架中UDP部分的使用 框架源码结构 补充说明 源码地址 说明 之前有好几篇博客在讲TCP/UDP通信方 ...

  9. Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)

    转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...

随机推荐

  1. php与oracle11g经典分页

    <?php $t1 = xdebug_time_index(); $conn = oci_connect("SCOTT","TIGER","19 ...

  2. CSS 换行知多少: word-wrap && word-break && white-space && word-spacing

    word-wrap : 首先提一下,word-wrap 这个 CSS 属性在CSS3中已经被更名为 overflow-wrap,这样语义化也是为了避免与 word-break 混淆: Referenc ...

  3. 记Tfs2010 Tfs_Warehouse路径配置更新

    继上一次tfs数据库迁移问题续. Tfs数据库迁移成功后遗留了部分问题,本次记录问题处理过程: 原服务器地址10.58.8.231,迁移至新服务器10.58.1.230后TFS管理控制台中 应用层 – ...

  4. let 与 var

    前言let与var最大的区别就是var会变量提升.var会被覆盖.var变量没有块级作用域,而let都将弥补这些bug.传统语言都不会有‘变量提升.重复声明被覆盖.变量没有块级作用’这些问题,这是js ...

  5. 127. Word Ladder(单词变换 广度优先)

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  6. [转]Ubuntu使用Wireshark找不到interface的解决方法

    Wireshark是一款强大的有图形界面的网络封包分析工具. dumpcap需要root权限才能使用的,以普通用户打开Wireshark,Wireshark当然没有权限使用dumpcap进行截取封包. ...

  7. C语言基础温故

    一.C语言中数组动态增长有哪些方法? 1.在原数组单元后面是没法再扩长的,因为后面的单元没法保证一定有.所以,数组原址动态增长肯定是不行的: 2.要么定义长一点的数组,要么自已把N个数组用链表串起来, ...

  8. Storm完整例子

    import backtype.storm.spout.SpoutOutputCollector; import backtype.storm.task.TopologyContext; import ...

  9. Topic与Queue

    总结自:https://blog.csdn.net/qq_21033663/article/details/52458305 队列(Queue)和主题(Topic)是JMS支持的两种消息传递模型: 1 ...

  10. 为什么@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

    Spring Boot会自动根据jar包的依赖来自动配置项目,例如当你项目下面有HSQLDB的依赖,Spring Boot会自动创建默认的内存数据库的数据源DataSource, 但我们使用Mybat ...