Created
December 24, 2018 07:28
-
-
Save jklm23/3f238d325389c289cce8700fde1bc461 to your computer and use it in GitHub Desktop.
实验一客户端
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "head.h" | |
#define max_len 40 | |
SOCKET s; | |
char buff[max_len]; | |
int iResult; | |
static int read_cnt; | |
char c; | |
char *read_ptr; | |
typedef struct sendtext{//发送和接收定长数据格式 | |
unsigned int len; | |
char buff[max_len]; | |
}sendtext; | |
int recvline(SOCKET fd,char *ptr,u_int maxsize){ | |
if(read_cnt<=0){//原有的缓冲区数据已全部读完,开始从内核中读取新的数据 | |
read_ptr=ptr;//指针指向缓冲区首地址 | |
read_cnt = recv(fd, ptr, maxsize,0); | |
if ( read_cnt < 0) { | |
if(read_cnt==-1){ | |
printf("recv error:%d\n",WSAGetLastError()); | |
Closesocket(fd); | |
return -1; | |
} | |
else if(read_cnt==0){ | |
return 0; | |
} | |
} | |
} | |
c=*read_ptr; | |
read_ptr++; | |
read_cnt--; | |
return 1; | |
} | |
int getline(char *s, int max) { | |
//memset(s,0, sizeof(s)); | |
int i = 0; | |
char cc; | |
while (--max > 0 && (cc = getchar()) != EOF && cc != '\n') | |
s[i++] = cc; | |
if (cc = '\n') | |
s[i] = '\n'; | |
//s[i+1]='\0'; | |
return i; | |
} | |
int Time(){ | |
WSADATA ws; | |
if(WSAStartup(MAKEWORD(2,2),&ws)!=0){ | |
printf("Error StartUp:%d\n",WSAGetLastError()); | |
return -1; | |
} | |
s=socket(PF_INET,SOCK_STREAM,0); | |
if(s==INVALID_SOCKET){ | |
printf("socket error:%d\n",WSAGetLastError()); | |
return -1; | |
} | |
SOCKADDR_IN si; | |
memset(&si,0, sizeof(SOCKADDR_IN)); | |
si.sin_family=PF_INET; | |
//时间服务器端口6677 | |
si.sin_port=htons(6677); | |
si.sin_addr.s_addr=inet_addr("127.0.0.1"); | |
if(connect(s,(LPSOCKADDR)&si,sizeof(SOCKADDR))==-1){ | |
printf("Error connect:%d\n",WSAGetLastError()); | |
return -1; | |
} | |
//循环接收 | |
do { | |
iResult = recv(s, buff, max_len, 0); | |
if (iResult < 0) { | |
printf("receive error:%d\n"); | |
return -1; | |
} else if (iResult == 0) { | |
printf("close\n"); | |
return 0; | |
} else { | |
buff[iResult] = '\0'; | |
printf("receive:%s\n", buff); | |
} | |
}while(iResult>0); | |
// | |
if(closesocket(s)==-1){ | |
printf("Error close:%d\n",WSAGetLastError()); | |
return -1; | |
} | |
WSACleanup(); | |
} | |
//int readfile(){ | |
// printf("input file:"); | |
// char file[100];//文件路径 | |
// scanf("%s",file); | |
// FILE *fp=fopen(file,"r"); | |
// if(fp==NULL){ | |
// printf("打开文件失败\n"); | |
// return -1; | |
// } | |
// char *p=buff; | |
// | |
// while(!feof(fp)){ | |
// *p=fgetc(fp); | |
// p++; | |
// } | |
// *p='\0'; | |
// printf("读入文件:"); | |
// printf("%s\n",buff); | |
// fclose(fp); | |
//} | |
void readmultiline(){//输入多行数据,以#表示输入结束 | |
char *p=buff; | |
char ch; | |
while(scanf("%c",&ch)==1 && ch!=EOF && ch!='#'){ | |
if(p==buff+max_len) | |
return; | |
*p++=ch; | |
} | |
} | |
//回射程序 | |
int reflect(u_short port){ | |
getchar(); | |
// readmultiline();//输入多行数据,以#结束 | |
WSADATA ws; | |
if (WSAStartup(MAKEWORD(2, 2), &ws) != 0) { | |
printf("Error StartUp:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
s = socket(PF_INET, SOCK_STREAM, 0); | |
if (s == INVALID_SOCKET) { | |
printf("socket error:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
SOCKADDR_IN si; | |
memset(&si, 0, sizeof(SOCKADDR_IN)); | |
si.sin_family = AF_INET; | |
si.sin_port = htons(port); | |
si.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
if (connect(s, (LPSOCKADDR) &si, sizeof(SOCKADDR)) == -1) { | |
printf("Error connect:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
//char *p = buff; | |
while(true) { | |
getline(buff, max_len); | |
if (strcmp(buff, "q\n") == 0) {//输入的字符为q | |
//recv(s,buff,sizeof(buff),0); | |
//buff[max_len-1]='\0'; | |
// printf("%s\n",buff); | |
shutdown(s,1);//向服务器发FIN表示要关闭连接 | |
break; | |
} | |
if (send(s, buff, max_len, 0) <= 0) { | |
printf("Send error:%d\n",WSAGetLastError()); | |
return -1; | |
} | |
// | |
while (recvline(s, buff, sizeof(buff)) > 0) { | |
if(c==0) | |
continue;//过滤旧消息 | |
printf("%c", c); | |
if (c == '\n') {//本行数据接收结束,应初始化指针 | |
read_ptr=buff; | |
read_cnt=0; | |
memset(buff,0,sizeof(buff)); | |
break; | |
} | |
} | |
} | |
//shutdown(s,2); | |
Closesocket(s); | |
printf("connect close\n"); | |
WSACleanup(); | |
} | |
//发送定长数据 | |
int sendn(u_short port){ | |
WSADATA ws; | |
if (WSAStartup(MAKEWORD(2, 2), &ws) != 0) { | |
printf("Error StartUp:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
s = socket(PF_INET, SOCK_STREAM, 0); | |
if (s == INVALID_SOCKET) { | |
printf("socket error:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
SOCKADDR_IN si; | |
memset(&si, 0, sizeof(SOCKADDR_IN)); | |
si.sin_family = AF_INET; | |
si.sin_port = htons(port);//接收定长服务器 | |
si.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
if (connect(s, (LPSOCKADDR) &si, sizeof(SOCKADDR)) == -1) { | |
printf("Error connect:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
int temp; | |
printf("输入要发送的长度(换行符计入长度):"); | |
scanf("%d",&temp); | |
char *buffn=(char *)malloc(temp); | |
//printf("sizeof buff is %d\n", sizeof(*buffn)); | |
unsigned int ttt=htonl(temp); | |
if(-1==send(s,(char *)&ttt,sizeof(unsigned int),0)){ | |
printf("send error:%d\n",WSAGetLastError()); | |
return -1; | |
} | |
// | |
while(true) { | |
memset(buffn,0, temp); | |
scanf("%s",buffn); | |
if (strcmp(buffn, "q") == 0) {//输入的字符为q | |
break; | |
} | |
//printf("buffn is %s\n",buffn); | |
if(-1==send(s,buffn,temp,0)){ | |
printf("send error:%d\n",WSAGetLastError()); | |
break; | |
} | |
while (recvline(s, buffn, temp) > 0) { | |
printf("%c", c); | |
if (c == '\n') {//本行数据接收结束,应初始化指针 | |
read_ptr=buffn; | |
read_cnt=0; | |
memset(buffn,0,temp); | |
break; | |
} | |
} | |
} | |
shutdown(s,2); | |
if(closesocket(s)==-1) { | |
printf("close error:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
} | |
//发送变长数据 | |
int sendv(u_short port){ | |
char buff[max_len]; | |
unsigned int len; | |
WSADATA ws; | |
if (WSAStartup(MAKEWORD(2, 2), &ws) != 0) { | |
printf("Error StartUp:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
s = socket(PF_INET, SOCK_STREAM, 0); | |
if (s == INVALID_SOCKET) { | |
printf("socket error:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
SOCKADDR_IN si; | |
memset(&si, 0, sizeof(SOCKADDR_IN)); | |
si.sin_family = AF_INET; | |
si.sin_port = htons(port);//接收定长服务器 | |
si.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
if (connect(s, (LPSOCKADDR) &si, sizeof(SOCKADDR)) == -1) { | |
printf("Error connect:%d\n", WSAGetLastError()); | |
return -1; | |
} | |
while (true){ | |
sendtext st; | |
printf("输入要发送的数据:"); | |
scanf("%s",st.buff); | |
if(strcmp(st.buff,"q")==0){ | |
break; | |
} | |
st.len=strlen(buff); | |
} | |
shutdown(s,2); | |
Closesocket(s); | |
} | |
int main() { | |
while(true) { | |
printf("选择:\n1、时间服务器\n2、回射服务器(循环)\n3、回射服务器(并发)\n4、定长接收数据(循环)\n5、定长接收数据(并发)\n6、变长接收数据(循环)\n7、变长接收数据(并发)\n0、退出\n"); | |
int n; | |
scanf("%d",&n); | |
switch (n){ | |
case 1: | |
Time(); | |
break; | |
case 2: | |
reflect(6678);//循环回射服务器端口为6678 | |
//printf("%s\n",buff); | |
break; | |
case 3: | |
reflect(6679);//循环回射服务器端口为6679 | |
break; | |
case 4: | |
sendn(6680);//变长发送和接收数据 | |
break; | |
case 5: | |
sendn(6681); | |
break; | |
case 0: | |
return 0; | |
default: | |
printf("输入错误\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment