服务端:
#include <winsock2.h>#include <stdio.h>#include <string.h>#pragma comment(lib,"ws2_32.lib")typedef struct sockaddr_in sockaddr_in;typedef struct WSAData WSAData;typedef struct sockaddr sockaddr;int main(int argc, char* argv[]){//初始化WSAWORD sockVersion = MAKEWORD(2,2);WSADATA wsaData;if(WSAStartup(sockVersion, &wsaData)!=0){return 0;}//创建套接字SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if(slisten == INVALID_SOCKET){printf("socket error !");return 0;}//绑定IP和端口sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(8080);sin.sin_addr.S_un.S_addr = INADDR_ANY;if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)//if(bind(slisten, (sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR){printf("bind error !");}//开始监听if(listen(slisten, 5) == SOCKET_ERROR){printf("listen error !");return 0;}SOCKET sClient;sockaddr_in remoteAddr;int nAddrlen = sizeof(remoteAddr);printf("等待连接...\n");sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);if(sClient == INVALID_SOCKET){printf("accept error !");closesocket(sClient);closesocket(slisten);WSACleanup();return 0;}printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));int i=0,j,tmp;char buf[255]={0};char revData[255];printf("\n实现字符数组的翻转与加解密操作\n");while(1){printf("\n等待客户端发送数据");int ret = recv(sClient,revData,255,0);if(ret >0){revData[ret] = 0x00;printf("\n接收到的数据");printf(revData);printf("\n");if(strcmp(revData,"next")==0)//判断{break;}while(revData[i]!='\0'){i++;}tmp = i;for(j=0;j<tmp;j++,i--){buf[j] = revData[i-1];}printf("\n翻转后的数据:");printf(buf);printf("\n");for(i=0;i<strlen(revData);i++){if(revData[i]>='A'&&revData[i]<='Z'){revData[i]=((revData[i]-'A')+26-4)%26+'A';}else if(revData[i]>'a'&&revData[i]<'z'){revData[i]=((revData[i]-'a')+26-4)%26+'a';}}printf("\n解密后的数据:");printf(revData);printf("\n");}for(i=0;i<strlen(buf);i++)//加密{if(buf[i]>='A'&&buf[i]<='Z'){buf[i]=((buf[i]-'A')+26-4)%26+'A';}else if(buf[i]>'a'&&buf[i]<'z'){buf[i]=((buf[i]-'a')+26-4)%26+'a';}}printf("\翻转后后的加密数据:");printf(buf);printf("\n");send(sClient,buf,strlen(buf),0);}/*printf("\n**********聊天**********\n");while(1){char str1[50];char str2[50];int ret = recv(sClient, str2, 255, 0);if(strcmp(str2,"byebye")==0)//判断{break;}if(ret > 0){str2[ret] = 0x00;printf("\n来自client: ");printf(str2);printf("\n");}printf("sever: ");if(scanf("%s", str1)){printf("\n");send(sClient, str1, strlen(str1), 0);if(strcmp(str1,"byebye")==0)//判断{break;}}}*/printf("\n开始退出\n");closesocket(slisten);closesocket(sClient);WSACleanup();return 0;}
客户端:
#include <winsock2.h>#include <stdio.h>#include <string.h>#pragma comment(lib,"ws2_32.lib")typedef struct sockaddr_in sockaddr_in;typedef struct WSAData WSAData;typedef struct sockaddr sockaddr;int main(int argc, char* argv[]){WORD sockVersion = MAKEWORD(2,2);WSADATA data;if(WSAStartup(sockVersion, &data) != 0){return 0;}SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if(sclient == INVALID_SOCKET){printf("invalid socket !");return 0;}sockaddr_in serAddr;serAddr.sin_family = AF_INET;serAddr.sin_port = htons(8080);serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) ==SOCKET_ERROR){printf("connect error !");closesocket(sclient);return 0;}int i;int len = sizeof(serAddr);char sentData[255];char recData[255];printf("\n实现字符数组的翻转与加解密操作\n");while(1)//实现字符数组的翻转与加解密操作{printf("\n发送数据\n");gets(sentData);if(strcmp(sentData,"next")!=0){for(i=0; i<strlen(sentData); i++)//加密{if(sentData[i] >= 'A' && sentData[i] <= 'Z'){sentData[i] = ((sentData[i]-'A')+4)%26+'A';}else if(sentData[i] >= 'a' && sentData[i] <= 'z'){sentData[i] = ((sentData[i]-'a')+4)%26+'a';}}printf("\n加密后: ");printf(sentData);printf("\n");send(sclient, sentData, strlen(sentData), 0);int ret = recv(sclient, recData, 255, 0);if(ret > 0){recData[ret] = 0x00;for(i=0; i<strlen(recData); i++)//原字符解密{if(recData[i] >= 'A' && recData[i] <= 'Z'){recData[i] = ((recData[i]-'A')+26-4)%26+'A';}else if(recData[i] >= 'a' && recData[i] <= 'z'){recData[i] = ((recData[i]-'a')+26-4)%26+'a';}}printf("\n解密翻转后的数据: ");printf(recData);printf("\n");}}else{strcpy(sentData,"next");sendto(sclient, sentData, strlen(sentData), 0, (sockaddr *)&serAddr, len);break;}}/*printf("\n**********聊天**********\n");while(1)//实现基于 TCP 的聊天软件设计{char str1[50];char str2[50];printf("\nclient: ");if(scanf("%s", str1)){send(sclient, str1, strlen(str1), 0);}if(strcmp(str1,"byebye")==0)//判断{break;}int ret = recv(sclient, str2, 255, 0);if(ret > 0){str2[ret] = 0x00;printf("来自sever: ");printf(str2);printf("\n");if(strcmp(str2,"byebye")==0)//判断{break;}}}*/closesocket(sclient);WSACleanup();return 0;}
聊天 实现效果: