step5. day3 Network Programming Programming Demo TPC-based network protocol, FTP function class

1. Client

//cilent code

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

#define SIZE 128

void client_list(int socket_fd);

void client_get(int socket_fd,char *bufsend);
void client_put(int socket_fd,char *bufsend);

main int (int argc, char const * the argv [])
{
IF (argc <. 3) {
the printf ( "named USRMSG is:% S <the IP> <Port> \ n-", the argv [0]);
return -1;
}
/ / create links and communication socket
int socket_fd;
socket_fd = socket (AF_INET, SOCK_STREAM, 0);
IF (socket_fd == -1) {
perror ( "socket");
return -1;
}
the printf ( "socket OK \ n- ");

// fill structure sockadd_in, parameter passing
int connect_fd;
struct the sockaddr_in ServerAddr;
bzero (& ServerAddr, the sizeof (ServerAddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = the htons (atoi (the argv [2]));
serveraddr.sin_addr = the inet_addr .s_addr (the argv [. 1]);
// Connect link
connect_fd = Connect (socket_fd, (struct the sockaddr *) & ServerAddr, the sizeof (ServerAddr));
IF (connect_fd == -1) {
perror ( "Connect");
-1 return;
}
the printf ( "Connect OK \ n-");
// send an accept buffer buf
char bufsend [SIZE];
char bufrecv [SIZE] = {0};

// transmit and receive cycles transmit successfully verified
while (1 ) {
the printf ( "*************** List ************** \ n-");
the printf ( "***********put filename**********\n");
printf("***********get filename**********\n");
printf("***************quit**************\n");
printf("choose msg >>");

fgets(bufsend,SIZE,stdin);
bufsend[strlen(bufsend)-1] = '\0';

switch(bufsend[0]){
case 'l':
client_list(socket_fd);
break;
case 'p':
printf("send put\n");
client_put(socket_fd,bufsend+4);
break;
case 'g':
printf("send get\n");
client_get(socket_fd,bufsend+4);
break;
case 'q':
printf("quit\n");
close(socket_fd);
return 0;
default:
printf("error choose\n");
break;
}

}
close(socket_fd);
return 0;
}

void client_list(int socket_fd)
{
int recvbyte,i=1;
char buf[SIZE] = {0};
sprintf(buf,"L");
send(socket_fd, buf, sizeof(buf),0); //发送L给服务器

while(1){
memset(buf,0,SIZE);
recvbyte = recv(socket_fd,buf,sizeof(buf),0);

if(strncmp(buf,"qqq!",4) == 0)
break;

printf("%d.%s\n",i++,buf);
}
printf("client list is ok\n");
}

 

void client_get(int socket_fd,char *bufsend)
{
char buf[SIZE];
sprintf(buf,"G %s",bufsend);
send(socket_fd, (void *)buf, sizeof(buf),0);
int fdd,n;
if((fdd = open(buf+2,O_WRONLY | O_CREAT | O_TRUNC,0666))== -1 ){
perror("opendes");
return;
}

int recvbyte;
while(1){
memset(buf,0,SIZE);
recvbyte = recv(socket_fd, buf,sizeof(buf),0);

if(strncmp(buf,"qqq!",4)==0)
break;

write(fdd,buf,recvbyte);

}
printf("client get ok\n");
close(fdd);

}

void client_put(int socket_fd,char *bufsend)
{
char buf[SIZE];
sprintf(buf,"P %s",bufsend);
send(socket_fd, (void *)buf, sizeof(buf),0);

int fds,n;
if((fds = open(bufsend,O_RDONLY))== -1 ){
perror("opensrc");
return;
}

while((n = read(fds,buf,64))>0){
send(socket_fd, buf, n,0);
usleep(10000);
}
strcpy(buf,"qqq!");
send(socket_fd, buf,sizeof(buf),0);
printf("client put ok\n");
close(fds);

}

2. server code


//server code

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>

#define SIZE 128

void server_get(int accept_fd,char *bufrecv);
void server_put(int accept_fd,char *bufrecv);
void server_list(int accept_fd);

main int (int argc, char const * the argv [])
{
IF (argc <2) {
the printf ( "named USRMSG is: S% <Port> \ n-", the argv [0]);
return -1;
}
//. 1. create a socket file that is used to link
int socket_fd;
socket_fd = socket (AF_INET, SOCK_STREAM, 0);
IF (socket_fd == -1) {
perror ( "socket");
return -1;
}
the printf ( "OK socket the created \ n ");

// fill structure sockaddr_in, for parameter passing
int bind_fd;
struct the sockaddr_in ServerAddr; // packed structure defined
bzero ((void *) & ServerAddr, the sizeof (ServerAddr));
serveraddr.sin_family = AF_INET; // the IPV4 protocol
ServerAddr .sin_port = htons (atoi (argv [ 1])); // the local network port number conversion bit bytecode
serveraddr.sin_addr.s_addr = htonl (INADDR_ANY); // local IP network byte bit conversion code, any IP
/ . / 2 bound socket descriptor, the port, the IP
bind_fd = the bind (socket_fd, (struct the sockaddr *) & ServerAddr, the sizeof (ServerAddr));
IF (bind_fd == -1) {
perror ( "the bind");
return - . 1;
}
the printf ( "the bind OK \ n-");

// monitor 3, the active socket file attribute of the passive mode
int listen_fd;
listen_fd = the listen (socket_fd, 10);

if(listen_fd == -1){
perror("listen");
return -1;
}
printf("listen ok\n");

int accept_fd;
while(1){

// 4 blocking function, waiting for a client request for a link, the link is not concerned with the client, fill NULL
accept_fd = Accept (socket_fd, NULL, NULL);
IF (accept_fd == -1) {
perror ( "Accept");
-1 return;
}
the printf ( "Accept OK \ n-");

//循环接收信息
char bufrecv[SIZE] = {0};
int recvbyte;
while(1){
memset(bufrecv,0,SIZE);
printf("recv wait:\n");
recvbyte = recv(accept_fd, (void *)bufrecv, SIZE, 0);

if(recvbyte>0){
switch(bufrecv[0]){
case 'L':
server_list(accept_fd);
break;
case 'P':
printf("recv put\n");
server_put(accept_fd,bufrecv+2);
break;
case 'G':
printf("recv get\n");
server_get(accept_fd,bufrecv+2);
break;
}
}

else if(recvbyte<0){
printf("recv fail,waiting for next connect.\n");
break;
}
else{
printf("connect break, waiting for next connect.\n");
close(accept_fd);
break;
}

}
}
close(accept_fd);
close(socket_fd);

return 0;
}


void server_list(int accept_fd)
{
DIR *dirp;
struct dirent *dent;
struct stat temp;

dirp = opendir(".");
char buf[SIZE] = {0};
while((dent = readdir(dirp)) != NULL)
{
if(strncmp(dent->d_name,".",1) == 0)
continue;

stat(dent->d_name,&temp);
if(S_ISDIR(temp.st_mode) !=0)
continue;
memset(buf,0,SIZE);
strcpy(buf,dent->d_name);
send(accept_fd, buf, sizeof(buf),0);
usleep(10000); //防止栈包
}

memset(buf,0,SIZE);
strcpy(buf,"qqq!");
send(accept_fd, buf, sizeof(buf),0);
printf("server list is ok\n");
closedir(dirp);
return;

}

void server_get(int accept_fd,char *bufrecv)
{
int fds,n;
char buf[SIZE];
if((fds = open(bufrecv,O_RDONLY))== -1 ){
perror("opensrc");
return;
}

while((n = read(fds,buf,SIZE))>0){
send(accept_fd, buf, n,0);
usleep(10000);
}
strcpy(buf,"qqq!");
send(accept_fd, buf, sizeof(buf),0);
printf("server get ok\n");
close(fds);
}


void server_put(int accept_fd,char *bufrecv)
{
int fdd,n;
if((fdd = open(bufrecv,O_WRONLY | O_CREAT | O_TRUNC,0666))== -1 ){
perror("opendes");
return;
}

char buf[SIZE] = {0};
int recvbyte;
while(1){
memset(buf,0,SIZE);
recvbyte = recv(accept_fd, (void *)buf,sizeof(buf) ,0);
if(strncmp(buf,"qqq!",4)==0)
break;

write(fdd,buf,recvbyte);
}
printf("server put ok\n");
close(fdd);

}

 

Guess you like

Origin www.cnblogs.com/huiji12321/p/11371398.html