#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "192.168.2.255"//广播地址
#define PORT 6666//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
int broad = 1;
//设置允许广播
setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad));
//绑定发送方自身的地址信息结构体----》非必须绑定
//若不绑定操作系统会自动给发送方绑定一个IP和端口
//填充服务器的地址信息结构体,给sendto函数使用
//真实的地址信息结构体根据地址族指定AF_INET:man 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET;
sin.sin_port = htons(PORT);//1024~49151,网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//ifconfig出来的本机ip
char buf[128] = "";
while(1){
//发送数据
printf("发送端发出消息:");
scanf(" %s",buf);
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("sendto");
return -1;
}
printf("send success__%d__\n",__LINE__);
}
//关闭套接字
close(sfd);
return 0;
}
发送方:
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "192.168.2.255"//ifconfig出来的本机IP
#define PORT 6666//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
//填充接收方的地址信息结构体
//真实的地址信息结构体根据地址族指定AF_INET:man 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET;
sin.sin_port = htons(PORT);//1024~49151,网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//广播IP
//绑定地址信息结构体
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success__%d__\n",__LINE__);
char buf[128] = "";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1){
bzero(buf,sizeof(buf));
//接收
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen)<0){
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
}
//关闭套接字
close(sfd);
return 0;
}
3.组播
发送方:
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "224.1.2.3"//组播IP 224.0.0.0~239.255.255.255或0.0.0.0
#define PORT 6666//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
//绑定客户端自身的地址信息结构体----》非必须绑定
//若不绑定操作系统会自动给客户端绑定一个IP和端口
//填充服务器的地址信息结构体,给sendto函数使用
//真实的地址信息结构体根据地址族指定AF_INET:man 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET;
sin.sin_port = htons(PORT);//1024~49151,网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//组播ip
char buf[128] = "";
struct sockaddr_in rcvaddr;
socklen_t addrlen = sizeof(rcvaddr);
while(1){
//发送数据
printf("发送方发出消息:");
scanf(" %s",buf);
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("sendto");
return -1;
}
printf("send success__%d__\n",__LINE__);
}
//关闭套接字
close(sfd);
return 0;
}
接收方:
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d: %s %s\n", __LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
#define GRP_IP "224.1.2.3" //组播IP 224.0.0.0-239.255.255.255
#define LOL_IP "192.168.2.155" //ifconfig, 本机IP
#define PORT 6666 //1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success sfd=%d __%d__\n", sfd, __LINE__);
//加入多播组
struct ip_mreqn mq;
mq.imr_multiaddr.s_addr = inet_addr(GRP_IP); //组播IP的网络字节序
mq.imr_address.s_addr = inet_addr(LOL_IP); //ifconfig出来的本机IP
mq.imr_ifindex = 2; //网络设备索引号
if(setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mq, sizeof(mq)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("加入多播组 %s 成功 __%d__\n", GRP_IP, __LINE__);
//填充接收方的地址信息结构体,
//真实的地址信息结构体根据地址族指定AF_INET:man 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(GRP_IP); //组播IP 224.0.0.0-239.255.255.255
//绑定地址信息结构体
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n", __LINE__);
char buf[128] = "";
struct sockaddr_in cin; //存储数据包是从哪里来的
socklen_t addrlen = sizeof(cin);
while(1)
{
bzero(buf, sizeof(buf));
//接收数据
if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
}
//关闭套接字
close(sfd);
return 0;
}
4.多进程并发服务器
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d: %s %s\n", __LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
int del_cli_msg(int newfd, struct sockaddr_in cin);
#define IP "192.168.2.155" //ifconfig出来的本机IP
#define PORT 6666 //1024~49151,网络字节序
void handler(int sig)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
int main(int argc, const char *argv[])
{
//捕获17) SIGCHLD信号
if(signal(SIGCHLD, handler) ==SIG_ERR)
{
ERR_MSG("signal");
return -1;
}
//创建套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("socket success __%d__",__LINE__);
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用成功 __%d__\n", __LINE__);
//填充地址结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
socklen_t addrlen = sizeof(sin);
//绑定
if(bind(sfd,(struct sockaddr*)&sin,addrlen)<0){
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n", __LINE__);
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n", __LINE__);
struct sockaddr_in cin;
socklen_t cadddrlen = sizeof(cin);
int pid = -1;
int newfd = -1;
while(1){
newfd = accept(sfd,(struct sockaddr *)&cin,&cadddrlen);
if(newfd<0){
ERR_MSG("accept");
return -1;
}
if((pid=fork())==0){
close(sfd);
del_cli_msg(newfd,cin);
exit(0);
}else if(pid>0){
close(newfd);
}else{
ERR_MSG("fork");
return -1;
}
}
return 0;
}
int del_cli_msg(int newfd, struct sockaddr_in cin){
char buf[128] = "";
ssize_t res = 0;
bzero(buf,sizeof(buf));
while(1){
//接收
res = recv(newfd,buf,sizeof(buf),0);
if(res<0){
ERR_MSG("recv");
}else if(res==0){
printf("[%s:%d] 客户端下线 newfd=%d __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd, __LINE__);
break;
}
printf("[%s:%d] newfd=%d :%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd, buf, __LINE__);
//发送
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success __%d__\n", __LINE__);
}
close(newfd);
return 0;
}
5.多线程并发服务器
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d: %s %s\n", __LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
struct climsg
{
int newfd;
struct sockaddr_in cin;
};
void* del_cli_msg(void *arg);
#define IP "192.168.2.155" //ifconfig出来的本机IP
#define PORT 6666 //1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("socket success __%d__",__LINE__);
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用成功 __%d__\n", __LINE__);
//填充地址结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
socklen_t addrlen = sizeof(sin);
//绑定
if(bind(sfd,(struct sockaddr*)&sin,addrlen)<0){
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n", __LINE__);
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n", __LINE__);
struct sockaddr_in cin;
socklen_t cadddrlen = sizeof(cin);
pthread_t tid = -1;
int newfd = -1;
struct climsg info;
while(1){
newfd = accept(sfd,(struct sockaddr *)&cin,&cadddrlen);
if(newfd<0){
ERR_MSG("accept");
return -1;
}
info.newfd = newfd;
info.cin = cin;
if(pthread_create(&tid,NULL,del_cli_msg,&info)!=0){
ERR_MSG("pthread_create");
return -1;
}
pthread_detach(tid);
}
close(sfd);
return 0;
}
void* del_cli_msg(void * arg){
char buf[128] = "";
ssize_t res = 0;
int newfd = ((struct climsg *)arg)->newfd;
struct sockaddr_in cin = ((struct climsg *)arg)->cin;
bzero(buf,sizeof(buf));
while(1){
//接收
res = recv(newfd,buf,sizeof(buf),0);
if(res<0){
ERR_MSG("recv");
}else if(res==0){
printf("[%s:%d] 客户端下线 newfd=%d __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd, __LINE__);
break;
}
printf("[%s:%d] newfd=%d :%s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd, buf, __LINE__);
//发送
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
}
printf("send success __%d__\n", __LINE__);
}
close(newfd);
}
作业:tftp上传
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d: %s %s \n",__LINE__,__FILE__,__func__);\
perror(msg);\
}while(0);
#define IP "192.168.2.30"//ifconfig出来的本机IP
#define PORT 69//1024~49151,网络字节序
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
printf("socket success__%d__\n",__LINE__);
//填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
char buf[516] = "";
int len = sprintf(buf,"%c%c%s%c%s%c",0,2,"1.png",0,"octet",0);
//向服务器发送请求
if(sendto(sfd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("sendto");
return -1;
}
printf("sendto success__%d__\n",__LINE__);
struct sockaddr_in rcvaddr;
socklen_t addrlen = sizeof(rcvaddr);
short *block ;
short *op;
//接收ack包得到临时端口
if(recvfrom(sfd,buf,4,0,(struct sockaddr*)&rcvaddr,&addrlen)<0){
ERR_MSG("recvfrom");
return -1;
}else {
op = (short*)buf;//记录ACK包中的操作码
block = (short*)(buf+2);//记录ACK中的块编号
}
//只读模式打开文件
int fd = open("./1.png",O_RDONLY);
if(fd<0){
ERR_MSG("open");
return -1;
}
printf("open success__%d__\n",__LINE__);
ssize_t datalen = 0;
short lastbock;
short blk = 0;
while(1){
//组数据包
sprintf(buf,"%c%c",0,3);
*block = htons(++blk);
lastbock = *block;
datalen = read(fd,buf+4,512);
if(datalen<0){
ERR_MSG("read");
return -1;
}
printf("read success__%d_read:%ld_\n",__LINE__,datalen);
while(1){
//发数据包
if(sendto(sfd,buf,datalen+4,0,(struct sockaddr*)&rcvaddr,sizeof(rcvaddr))<0){
ERR_MSG("sendto");
return -1;
}
printf("sendto success__%d__\n",__LINE__);
//阻塞方式接收ACK包
if(recvfrom(sfd,buf,4,0,(struct sockaddr*)&rcvaddr,&addrlen)>0){
if(*op==htons(4)||*block!=lastbock){
//发送完最后一个数据包并接到ACK包结束
if(datalen<512){
printf("上传完毕\n");
return 0;
}
break;
}
}
}
}
return 0;
}