📄 peer.c
字号:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>#include <malloc.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <setjmp.h>#include <sys/stat.h>#include <sys/ipc.h>#include <sys/shm.h>
#define MSGMAX 100 #define BUFMAX 104 //buffer=head+msg
#define LGIN 1
#define ERROR 2
#define LRES 3 #define SHRE 4#define SHOK 5#define SRCH 6#define SRES 7#define QUIT 8#define PING 9#define PRES 10#define GETF 11#define FILES 12#define TERROR 13jmp_buf env_alrm;char port[10]; //for listen ping service!char nowusername[20];//current user!//for time out alarm!void alarm_handler(int signo)
{
longjmp(env_alrm, 1);
}//basic message format!
struct Message
{
int head;
unsigned char content[MSGMAX];
};//get filedoGETF(struct Message msgin){ printf("ENTET doGETF:\n"); int sockfd;
struct sockaddr_in target_addr; unsigned char buf2[BUFMAX];// pid_t pid;//for one ping// pid_t pid2;//for ping pong listen service!// pid_t pid3;//for file service!
char *temp;//get the ip and port from msg char *filename;// char preamble[10]; filename=strtok(msgin.content,"#");//filename strtok(NULL,"#");//username,useless! temp=strtok(NULL,"#"); //now temp=ip
struct Message msg;//the package rcv & snd int flag=0;//log on success flag;// char input[100];// char choice[10];// weather to download file!
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < -1) {
perror("create socket error");
}
//set the server addr
bzero(&target_addr, sizeof(target_addr));
target_addr.sin_family = AF_INET;
target_addr.sin_addr.s_addr = inet_addr(temp); temp=strtok(NULL,"#");//now temp=port
target_addr.sin_port = htons(atoi(temp)+1);
// printf("Welcome, hello!\n");
if (connect(sockfd, (struct sockaddr *)&target_addr, sizeof(target_addr)) < -1) {
perror("connect error");
} //begin lgin! struct Message snd;//the package rcv & snd unsigned char *buf; //snd buffer buf=(unsigned char *)malloc(BUFMAX); //dynamic alloc mem!!!!so fucking! //make lgin msg snd.head=LGIN; strcpy(snd.content,nowusername); buf=(unsigned char *)&snd; printf("send %d bytes\n",write(sockfd, buf, BUFMAX));
// read
for (;;) { memset(&msg,'\0', sizeof(&msg)); memset(buf2,'\0', sizeof(buf2)); printf("read %d bytes:",read(sockfd, buf2, BUFMAX)); msg=*(struct Message *)buf2; printf("%d\n",msg.head);///////////////////////////////////////// if(msg.head==ERROR){ printf("%d,%s\n",msg.head,msg.content); break; }else if(msg.head==LRES){ printf("%d,%s\n",msg.head,msg.content); flag=1; break; }
} //if lgin sucuss if(flag){ //make getf msg snd.head=GETF; strcpy(snd.content,filename); buf=(unsigned char *)&snd; printf("send %d bytes GETF!\n",write(sockfd, buf, BUFMAX)); //build file FILE *file; if((file=fopen(filename,"w+"))==NULL){ printf("build file fale\n"); } char *fbuf; fbuf=(unsigned char *)malloc(BUFMAX); //read respose while(1){ memset(&msg,'\0', sizeof(&msg)); memset(buf2,'\0', sizeof(buf2)); printf("read %d bytes\n",read(sockfd, buf2, BUFMAX)); msg=*(struct Message *)buf2; if(msg.head==ERROR){ printf("%d,%s\n",msg.head,msg.content); break; }else if(msg.head==FILES){ //printf("1\n"); //strncpy(preamble,msg.content,7); //printf("2\n"); //strtok(msg.content,preamble);//shift 7 bytes! //printf("3\n"); //fbuf=strtok(NULL,preamble); //printf("4\n"); //fwrite(fbuf,1,strlen(fbuf),file); //printf("write=%s\nlenis:\n",fbuf,strlen(fbuf)); strtok(msg.content,"1111000"); fbuf=strtok(msg.content,"1111000"); printf("len:%d\n",strlen(fbuf)); fwrite(fbuf,1,strlen(fbuf),file); if(strlen(fbuf)!=86) break; } }//end while read fclose(file); }//end if flag};//do share one filedosharefile(struct Message msg,int client_sockfd){ printf("ENTER share one file!\n"); struct Message snd;//the package rcv & snd unsigned char *buf; //snd buffer buf=(unsigned char *)malloc(BUFMAX); //dynamic alloc mem!!!!so fucking! //check permission! char *temp=strtok(msg.content,"\n"); int i; ///open file char fbuf[100]; FILE *file; char nowfile[20]; int find=0;
if((file=fopen(nowusername,"r+"))==NULL){
printf("can not open file!\n");
return;
} while(fscanf(file,"%s",nowfile)>0){ printf("now:%s\n",nowfile); if(strncmp(nowfile,temp,strlen(temp))==0){ find=1; break; } } fclose(file); printf("out\n"); int seperate=0; if(find==0){ printf("file deny \n"); snd.head=ERROR; strcpy(snd.content,nowusername); //send buf=(unsigned char *)&snd; printf("send %d bytes\n",write(client_sockfd, buf, BUFMAX)); }else{ //file send! printf("file access\n"); if((file=fopen(temp,"r+"))==NULL){ printf("can not open file!\n");
return; } //100-7-7=86bytes at most! int readed=0; while(1){ strcpy(snd.content,"1111000");//preamble //change statu and read! readed=fread(fbuf,1,86,file); snd.head=FILES; //content strncat(snd.content,fbuf,readed); //endl strcat(snd.content,"1111000\n");//complete //snd buf=(unsigned char *)&snd; printf("send %d bytes\n",write(client_sockfd, buf, BUFMAX)); //printf("$content:\n%s\n",snd.content); if(readed<86) break; } } };//do file service!void doFILE(){ int server_sockfd, client_sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len;//size of client_addr
unsigned char buf[BUFMAX];//rcv buffer
struct Message msg;//the package rcv pid_t pid; char temp[10]; char username[20];//log in sucuss username struct Message snd; unsigned char *buf2; //snd buffer! buf2=(unsigned char *)malloc(BUFMAX); //dynamic alloc mem!!!!so fucking! client_len = sizeof(client_addr); //set the server addr
bzero((char *)&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//get my ip add
server_addr.sin_port = htons(atoi(port)+1); // file service port=ping listen port+1
//create socket SOCK_STREAM for TCP
if ((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("create server socket error");
}
// bind socket with my ip
if (bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind server socket error");
}
//listen and create connection,at most 5 connections
if (listen(server_sockfd, 5) < 0) {
perror("listen server socket error");
}
printf("<BEGIN FILE Service>\n");
for (;;) { memset(&username,'\0',20);
printf("Waiting for file connect...\n");
//get client sock client_len=sizeof(client_addr);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &client_len); ////////////////fork//////////////////// //pid=fork(); //if(pid>0){} //else if(pid==0){ /////////////end fork////////////////////// //printf("client ip:%s,sockfd:%d\n",inet_ntoa(client_addr.sin_addr.s_addr),client_sockfd); printf("one peer connected\n"); while(1){ //read & write
memset(&msg,'\0', sizeof(&msg)); memset(buf,'\0', sizeof(buf)); //so important!!!!! must use the unsigned buffer read(client_sockfd, buf, sizeof(buf)); msg=*(struct Message *)buf;
// printf("recive request %d,%s\n", msg.head,msg.content); //test
if(msg.head==LGIN){ memset(&username,'\0',20); /*if(strlen(msg.content)==0) { snd.head=ERROR; strcpy(snd.content,"some error happen\n"); memset(buf2,'\0', BUFMAX); buf2=(unsigned char *)&snd; printf("send %d bytes fufff\n",write(client_sockfd, buf2, BUFMAX)); break; }*/ printf("do you want so share file with:%s?(y/n)\n",msg.content);
//gets(temp); temp[0]='y'; if(temp[0]=='y'){ memset(&snd,'\0', BUFMAX); memset(buf2,'\0', BUFMAX); snd.head=LRES; strcpy(snd.content,"login succuss\n"); buf2=(unsigned char *)&snd; printf("%d^",snd.head); printf("send %d bytes yy\n",write(client_sockfd, buf2, BUFMAX)); strcpy(username,msg.content); }else{ snd.head=ERROR; strcpy(snd.content,"file share deny\n"); memset(buf2,'\0', BUFMAX); buf2=(unsigned char *)&snd; printf("send %d bytes \n",write(client_sockfd, buf2, BUFMAX)); } break; } }//end while if(username[0]!='\0'){//log in sucuss while(1){ printf("log in \n"); //read & write
memset(&msg,'\0', sizeof(&msg)); memset(buf,'\0', sizeof(buf)); //so important!!!!! must use the unsigned buffer read(client_sockfd, buf, sizeof(buf)); msg=*(struct Message *)buf; if(msg.head==GETF){//deal with share file //printf("begin share\n"); dosharefile(msg,client_sockfd); break;
} }//end while } close(client_sockfd); /////////////////////////fork////////////// //}else{ perror("fork error");} //return 0;
}
return 0;};//listen ping service!void doPRES()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -