📄 client.c
字号:
#include "mysocket.h"#include "tcp_connect.h"#include "des.h"/*loacl variables*/static des_context ctx;//des加密算法函数使用到的上下文static char des_key[8] = {0x20,0x05,0xAF,0x45,0xA3,0xF1,0x80,0x78}; //加密密码./*loacl functions*/static int encrypt_data(const int count,char *input,char *output);//加密数据函数static int send_data(int sockfd);//传送数据函数int main(int argc,char **argv){ //127.0.0.1为本机ip地址,也就是说如果server和client运行在同一台机器上,则使用这个地址 //如果他们分别运行在两台机器上,则这个ip地址必须设置为server所在机器的ip. char *host = "127.0.0.1"; int sockfd; if(argc > 2) { printf("Usage:%ss[hostname or IP address]\n",argv[0]); exit(1); } if(argc == 2) { host = argv[1]; } des_set_key(&ctx,des_key);//设置des加密密码 sockfd = tcp_connect(host,"8888");//使用tcp协议连接服务器的8888端口 if(sockfd < 0) { printf("\tERROR:Make sure that the server have been in runing.\n"); exit(1); } send_data(sockfd);//如果连接成功则开始传送数据 close(sockfd); exit(0);}/*加密数据,des_encrypt()函数每次只能加密8个字节的内容,如果数据count不能被8整除,则最后几个字节的数据不进行加密*/static intencrypt_data(const int count,char *input,char *output){ int n;//,m; char *iptr = input; char *optr = output; if(!count || input == NULL || output == NULL) return (-1); for(n = count / 8; n > 0;n--) { des_encrypt(&ctx, iptr, optr); iptr += 8; optr += 8; }#if 0 //How to deal with last bytes less than 8? I have no idea about it for now. //Do nothing may be a good idea. m = n % 8; bzero(optr + m,8 - m); des_encrypt(&ctx, iptr, optr);#endif return (0);}/*传送数据,这里是用从一个文件读取数据传送给服务器来模拟流媒体数据。*/static intsend_data(int sockfd){ FILE *fd = NULL; unsigned int count = 0; char buf4send[MAXLINE + 1] = {0}; fd = fopen("./a","r");//打开当前目录下名为a的文件 if(fd == NULL) { printf("ERROR:Open file 'a' failed.\n"); close(sockfd); exit(1); } while((count = fread(buf4send,sizeof(char),MAXLINE,fd)))//从文件中读取数据 { encrypt_data(count,buf4send,buf4send);//加密从文件中读到的数据 writen(sockfd,buf4send,count);//穿送给服务器 } fclose(fd); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -