linux網路程式設計 TCP檔案伺服器

2021-06-08 16:33:39 字數 3935 閱讀 2486

程式功能:

程式如下:

(1)、server.c

#include #include #include #include #include #include #include struct message

;void list_file(int , struct message *);

void get_file(int , struct message *);

void put_file(int , struct message *);

int main(int argc, const char *argv)

if ((listen_fd = socket(af_inet, sock_stream, 0)) < 0)

server_addr.sin_family = af_inet;

server_addr.sin_port = htons(atoi(argv[2]));

server_addr.sin_addr.s_addr = inet_addr(argv[1]);

client_addrlen = sizeof(struct sockaddr);

if (bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)

listen(listen_fd, 5);

while(1)

memset(&msg, 0, sizeof(msg));

recv(socket_fd, &msg, sizeof(msg), 0);

printf("cmd : %s\n", msg.cmd);

if (strcmp(msg.cmd, "list") == 0)

list_file(socket_fd, &msg);

if (strcmp(msg.cmd, "get") == 0)

get_file(socket_fd, &msg);

if (strcmp(msg.cmd, "put") == 0)

put_file(socket_fd, &msg);

}return 0;

}void list_file(int socket_fd, struct message *msg)

memset(buf, 0, sizeof(buf));

while ((dp = readdir(dir)) != null)

send(socket_fd, buf, strlen(buf) + 1, 0);

close(socket_fd);

return ;

}void get_file(int socket_fd, struct message *msg)

memset(buf, 0, sizeof(buf));

while((num = read(fd, buf, sizeof(buf))) > 0)

close(fd);

close(socket_fd);

return ;

}void put_file(int socket_fd, struct message *msg)

memset(buf, 0, sizeof(buf));

while((num = recv(socket_fd, buf, sizeof(buf), 0)) > 0)

write(fd, buf, num);

close(fd);

close(socket_fd);

return ;

}

(2)client.c

#include #include #include #include #include #include #include struct message

;void list_file(struct message , char *, struct sockaddr_in);

void get_file(struct message , char *, char *, struct sockaddr_in);

void put_file(struct message , char *, char *, struct sockaddr_in);

int main(int argc, const char *argv)

server_addr.sin_family = af_inet;

server_addr.sin_port = htons(atoi(argv[2]));

server_addr.sin_addr.s_addr = inet_addr(argv[1]);

while (1)

return 0;

}void list_file(struct message msg, char *cmd, struct sockaddr_in server_addr)

if (connect(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)

strcpy(msg.cmd, cmd);

send(server_fd, &msg, sizeof(msg), 0);

usleep(500);

memset(buf, 0, sizeof(buf));

recv(server_fd, buf, sizeof(buf), 0);

printf("%s\n", buf);

close(server_fd);

return ;

}void get_file(struct message msg, char *cmd, char *filename, struct sockaddr_in server_addr)

if (connect(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)

if ((fd = open(filename, o_wronly | o_creat | o_trunc, 0666)) < 0)

strcpy(msg.cmd, cmd);

strcpy(msg.filename, filename);

send(server_fd, &msg, sizeof(msg), 0);

memset(buf, 0, sizeof(buf));

while ((num = recv(server_fd, buf, sizeof(buf), 0)) > 0)

printf("download ok!\n");

close(fd);

close(server_fd);

return ;

}void put_file(struct message msg, char *cmd, char *filename, struct sockaddr_in server_addr)

if (connect(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)

strcpy(msg.cmd, cmd);

strcpy(msg.filename, filename);

send(server_fd, &msg, sizeof(msg), 0);

usleep(500);

if ((fd = open(filename, o_rdonly)) < 0)

memset(buf, 0, sizeof(buf));

while ((num = read(fd, buf, sizeof(buf))) > 0)

send(server_fd, buf, num, 0);

printf("upload ok!\n");

close(fd);

close(server_fd);

return ;

}

Linux 網路程式設計 TCP

一 tcp通訊步驟 tcp伺服器部分 1 呼叫函式socket 建立乙個socket 2 設定sockaddr in資訊,如要連線伺服器的ip和埠等屬性 3 呼叫函式bind 繫結ip位址 埠等資訊到socket上 4 呼叫函式listen 設定允許的最大連線數 5 呼叫函式accept 等待來自客...

Linux 網路程式設計(TCP)

include include include include include include include include int main int argc,char argv bzero server addr,sizeof struct sockaddr in server addr.si...

linux網路程式設計 初探TCP

伺服器端等待客戶端連線,連線成功後,列印客戶端的ip和port,然後迴圈接收資料,緩衝區無資料就阻塞待待。include include include include include include include include include include define maxbuf 10 ...