c语言的tcp和udp客户端和服务器
都是最简单的用来记忆。
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客户端和服务器的更多相关文章
- python网络编程socket编程(TCP、UDP客户端服务器)
摘录 python核心编程 使用socket()模块函数创建套接字——通信端点 >>> from socket import * >>> tcpSock = soc ...
- UNIX网络编程——使用select函数的TCP和UDP回射服务器程序
服务器程序: #include <sys/wait.h> #include <string.h> #include <string.h> #include < ...
- C语言Socket-模拟远程CMD(客户端向服务器发送命令,服务器执行该命令)
服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...
- TCP/UDP客户端
Python 网络编程----模块socekt 在渗透测试的过程中,经常会遇到需要创建一个TCP客户端来连接服务器.发送垃圾数据.进行模糊测试活进行其他任务的情况. 简单的TCP客户端代码: #!/u ...
- LwIP应用开发笔记之三:LwIP无操作系统UDP客户端
前一节我们实现了基于RAW API的UDP服务器,在接下来,我们进一步利用RAW API实现UDP客户端. 1.UDP协议简述 UDP协议全称是用户数据报协议,在网络中它与TCP协议一样用于处理数据包 ...
- Windows下C语言的Socket编程例子(TCP和UDP)
原文:Windows下C语言的Socket编程例子(TCP和UDP) 刚刚学windows编程,所以想写学习笔记,这是一个简单的Socket程序例子,开发环境是vc6: 首先是TCP server端: ...
- 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
- TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端
目录 说明 TCP/UDP通信主要结构 管理多个Socket的解决方案 框架中TCP部分的使用 框架中UDP部分的使用 框架源码结构 补充说明 源码地址 说明 之前有好几篇博客在讲TCP/UDP通信方 ...
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...
随机推荐
- byte[]数组和int之间的转换
这里简单记录下两种转换方式: 第一种: 1.int与byte[]之间的转换(类似的byte short,long型) /** * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高 ...
- Python 能干什么
二.Python 只适合测试? 关于Python是一种什么样的语言,这里不打算说对象.类之类的术语.我们可以先来看一看,时至今日 Python 都在哪些领域里得以应用: 电信基础设施 (Twilio) ...
- python3 用requests 保存网页以及BeautifulSoup保存图片,并且在本地可以正常显示文章的内容和图片
用requests 模块做了个简单的爬虫小程序,将博客的一篇文章以及图片保存到本地,文章格式存为'.html'.当文章保存到本地后,图片的连接可能是目标站点的绝对或者相对路径,所以要是想在本地也显示图 ...
- Vmwaretools
先下载Vmwaretools 这一步是设置ubuntu的超级用户root的密码我设置为dong 转换为root用户操作 执行 perl程序 然后就是一路Enter,开始关机重启就行了 来自为知笔记 ...
- jQuery HTML操作学习笔记
学习资料 jQuery教程 获取 1.获取.设置元素的内容 1.1获取或设置目标元素的文本内容 语法 $(selector).text(); 获取元素文本内容 $(selector).text(con ...
- td中不包含汉字的字符串不换行,包含汉字的能换行的问题原因及解决方法
今天项目中遇到一个问题,一长串的字符串如:003403FF0014E54016030CC655BC3242,但是如:中国河北省石家庄市裕华区槐安路雅清街交口 这样的就可以换行. 原因是:英文字母之间如 ...
- Spring MVC 复习笔记01
1. springmvc框架 1.1 什么是springmvc spring mvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合.spring mvc是一个 ...
- PHP jsonencode 已经json中包含 汉字的处理
<?php $arr = array ( 'Name'=>'希亚', ); $jsonencode = json_encode($arr); echo $jsonencode; ?> ...
- Python3.x:正则 re.findall()的用法
Python3.x:正则 re.findall()的用法 概念: 语法:findall(pattern, string, flags=0) 说明:返回string中所有与pattern相匹配的全部字串 ...
- jQuery中this 和 $(this)
var node = $('#id'); node.click(function(){ this.css('display','block'); //报错 this是一个html元素,不是jquer ...