上传
int do_upload(int sfd,struct sockaddr_in sin)
{
int ret_val=0;
char buf[516]="";
char filename[20]="";
printf("请输入上传文件名>>>");
scanf("%s",filename);
while(getchar()!=10);
int fdd =open(filename,O_RDONLY);
if(fdd<0)
{
ERR_MSG("open");
return -1;
}
int size =sprintf(buf,"%c%c%s%c%s%c",0,2,filename,0,"octet",0);
if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -7;
}
ssize_t res=0;
unsigned short num=0;
socklen_t addrlen =sizeof(sin);
while(1)
{
bzero(buf,sizeof(buf));
res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
ret_val= -11;
break;
}
if(buf[1]==4)
{
if((htons(num))==*(unsigned short*)(buf+2))
{
res=read(fdd,buf+4,512);
if(res<0)
{
ERR_MSG("fread");
ret_val =-9;
break;
}
else if(res==0)
{
printf("文件上传完毕\n");
break;
}
// num++;
// buf[0]=0;
// buf[1]=3;
*(unsigned short*)buf=htons(3);
*(unsigned short*)(buf+2)=htons(++num);
if(sendto(sfd,buf,res+4,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
ret_val = -10;
break;
}
}
}
else if(buf[1]==5)
{
printf("MSG_ERR:code[%d] msg[%s] __%d__",\
htons(*(unsigned short*)(buf+2)),buf+4,__LINE__);
ret_val= -6;
break;
}
}
close(fdd);
close(sfd);
return ret_val;
}
多进程并发服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define PORT 2222 //1024~49151
#define IP "192.168.31.49" //IP地址,本机IP ifconfig
void callback(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int aaa(int newfd,struct sockaddr_in cin);
int main(int argc, const char *argv[])
{
__sighandler_t l=signal(17,callback);
//创建流式套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd = %d\n",sfd);
//设置允许端口快速被重用
int resue=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&resue,sizeof(resue))<0)
{
ERR_MSG("setsockopt");
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地址的网络字节序,ifconfig查询
//绑定——必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听
if(listen(sfd,128))
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//功能:阻塞函数,阻塞等待客户端连接成功;
//当客户端连接成功,会从已经完成连接的队列头中获取一个客户端信息;
//并生成一个新的文件描述符;新的文件描述符才是与客户端通信的文件描述符;
struct sockaddr_in cin; //存储连接成功的客户端的地址信息;
socklen_t addrlen= sizeof(cin);
int newfd=-1;
pid_t cpid=0;
while(1)
{
newfd= accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d]newfd=%d 客户连接成功\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
cpid=fork();
if(cpid>0)
{
close(newfd);
}
else if(0==cpid)
{
close(sfd);
aaa(newfd,sin);
close(newfd);
exit(0);
}
else
{
ERR_MSG("fork");
return -1;
}
}
//关闭所有套接字文件描述符
close(newfd);
close(sfd);
return 0;
}
int aaa(int newfd,struct sockaddr_in cin)
{
char buf[128]="";
ssize_t res=-1;
while(1)
{
bzero(buf,sizeof(buf));
//接受
res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
fprintf(stderr,"[%s:%d]newfd=%d 客户端下线\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s:%d]newfd=%d %s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
//发送
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
return 0;
}
多线程并发服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d ", __LINE__);\
perror(msg);\
}while(0)
#define PORT 6666 //1024~49151
#define IP "127.0.0.1" //IP地址,本机IP ifconfig
struct cli_msg
{
int newfd;
struct sockaddr_in cin;
};
void* deal_cli_msg(void* arg);
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 create success sfd = %d\n", sfd);
int resue = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 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 addrlen = sizeof(cin);
int newfd = -1;
pthread_t tid;
struct cli_msg info;
while(1)
{
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd=%d \n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
info.newfd = newfd;
info.cin = cin;
if(pthread_create(&tid, NULL, deal_cli_msg, &info) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_detach(tid);
}
close(sfd);
return 0;
}
void* deal_cli_msg(void* arg)
{
//y要另存为
int newfd = ((struct cli_msg*)arg)->newfd;
struct sockaddr_in cin = ((struct cli_msg*)arg)->cin;
char buf[128] = "";
ssize_t res = -1;
while(1)
{
bzero(buf, sizeof(buf));
res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
break;
}
else if(0 == res)
{
fprintf(stderr, "[%s : %d] newfd=%d 客户端下线\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
break;
}
printf("[%s : %d] newfd=%d : %s\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf);
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
break;
}
printf("send success\n");
}
close(newfd);
pthread_exit(NULL);
}