📄 client.cpp
字号:
#include <stdlib.h>#include <iostream>#include <string.h>#include <string>#include <errno.h>#include <netdb.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#include <unistd.h>#include <pthread.h>#include <malloc.h>using namespace std;const int MAXSIZE = 4096;const int PORT = 5000;int quit = 0;char *srcfile = "../test.c";void *get_server(void *);//void itoa(int n, char s[]);int sendfile(int, char *);int main() { int connfd, snd, slenth; int send_len; struct sockaddr_in server; struct hostent *hp;// char honame[20];// char msg1[MAXCHAR], msg2[MAXCHAR], cln[MAXCHAR], qstr[] ={ "Quit"}; string honame; string msg1; string msg2; string cln;// char temp[MAXCHAR]; string qstr="Quit"; pthread_t tid; cout<<"请输入服务器ip地址:"<<endl;// scanf("%s*",honame); cin>>honame; cout<<"正在建立套接口"<<endl; if((connfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { cout<<"建立套接口失败"<<endl; exit(-1); } if((hp = gethostbyname(honame.data())) == NULL) { cout<<"获取服务器ip地址失败"<<endl; exit(1); } else { cout<<"套接字接口建立成功,连接服务器中。。。。"<<endl; } memcpy(&server.sin_addr, hp->h_addr, hp->h_length); server.sin_family = AF_INET; server.sin_port = htons(PORT); if(connect(connfd, (struct sockaddr*)&server, sizeof(server)) < 0) { cout<<"连接服务器失败"<<endl; exit(1); } cout<<"连接服务器成功"<<endl; cout<<"请输入你的名称:"<<endl; cin>>msg1; slenth = msg1.size();/* scanf("%s",msg1); slenth = strlen(msg1); msg1[slenth] = ':'; msg1[slenth+1] = '\0'; strcpy(cln, msg1); cln = msg1;*/ msg1 += ": "; cln = msg1;// pthread_create(&tid, NULL, &get_server, (void *)connfd); cout<<"请输入您的信息(\"Quit\")断开连接"<<endl;// while(true) // {/* scanf("%s",msg2); cin>>msg2; int len = strlen(msg2); if(len > MAXCHAR) { char[] newMsg2 = mew char[len]; char[] newMsg1 = new char[2*len+1]; strcpy(newMsg1, msg1); strcpy(newMsg2, msg2); } if(msg2.compare(qstr) == 0) { close(connfd); quit = 1; break; } else { msg1 += msg2; strcat(msg1, msg2); itoa(msg1.size()+1, temp); send_len = send (connfd, temp, sizeof(int), 0); if(send_len < 0) { cout<<"传输文件大小错误"<<endl; exit(-1); } */ sendfile(connfd,srcfile); /* snd = send(connfd, msg1.data(),msg1.size()+1, 0); { cout<<"传输客户端信息失败"<<endl; exit(-1); }*/// msg1 = cln;// }// }}void *get_server(void *sockfd) { char buf[1024]; int rev; if((int)sockfd <0) { cout<<"接收服务器失败"<<endl; } else { while(true) { if(!quit) { if(rev = recv((int)sockfd, buf, 1024, 0) > 0) { cout<<buf<<endl; } if(rev == 0) { cout<<"服务器终止连接"<<endl; quit = 1; } } else { close((int)sockfd); break; } } return NULL; }}void itoa(int n, char s[]){ int i,j,sign; if((sign = n) < 0) { n = -n; } i = 0; do { s[i++] = n%10 + '0'; } while ((n = n/10)>0); if(sign < 0) s[i++] = '-'; s[i] = '\0'; int len=strlen(s); for( ; i>0; j--) s[i] = s[len-i-1];}/*给服务端发送文件r*/int sendfile(int fd,char *srcfile) { int num,i,sum; FILE *srcfp; char buf[MAXSIZE - sizeof(int)]; int buflen; char sendbuf[MAXSIZE]; char *p; //定义一个指向sendbuf的指针 if((srcfp = fopen(srcfile,"rb")) == NULL) { perror("can not open comfile"); exit(1); } i = sizeof(buflen); while(!feof(srcfp)) { buf[0] = '\0'; sendbuf[0] = '\0'; p = sendbuf; buflen = fread(buf, 1, sizeof(buf),srcfp); memcpy(p, &buflen, i); p = p + i; memcpy(p, buf, buflen); sum = i + buflen; if((num = send(fd,sendbuf,sum,0)) == -1) { perror("send file is error"); exit(1); } printf("num:%d\tbuflen:%d\n", num, buflen); } buflen = 0; memcpy(sendbuf, &buflen, i); if(send(fd, sendbuf, i, 0) == -1) { perror("send file is error"); exit(1); } printf("copy end\n"); // fclose(srcfp); buf[0] = '\0'; if((num = recv(fd, buf, sizeof(buf), 0)) == -1) { perror("recv data error"); exit(1); } buf[num]='\0'; cout<<"服务器发送的信息如下:"<<endl; printf("buf:%s\n",buf); fclose(srcfp); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -