网络编程之路---5

发布时间:2016-12-6 16:07:46 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"网络编程之路---5",主要涉及到网络编程之路---5方面的内容,对于网络编程之路---5感兴趣的同学可以参考一下。

介个是接着上一篇的小应用。 服务端: #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") #include <stdio.h> #define BUF_SIZE 64 //定义缓冲区的大小 int main(int argc, char *argv[]) { WSADATA wsaData; //WSA windows socket api char buf[BUF_SIZE]; //缓冲区 int resValue; //用来接收函数的返回值 if ((WSAStartup(MAKEWORD(2,2), &wsaData)) != 0) //用于初始化windows sockets并返回WSADATA结构体 { //MessageBox(NULL, L"无法初始化", L"WindowsApi", 0); printf("无法初始化!"); return 0; } //... SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创建tcp套接字(服务器) //地址家族, 套接字类型,套接字使用的协议 if (s == INVALID_SOCKET) { printf("socket error!"); } SOCKADDR_IN addr; //命名socket,服务器地址 //struct sockaddr_in addr; //命名socket,地址 addr.sin_family = AF_INET; //因特网地址家族或协议系列 addr.sin_port = htons(9990); //主机->网络 addr.sin_addr.S_un.S_addr =/*inet_addr("222.18.167.206")*//*htonl(INADDR_ANY)*/inet_addr("222.18.167.206"); //在任意本地地址上进行监听 int errorCode; //错误代码 int addr_len = sizeof(struct sockaddr_in); errorCode = bind(s, (const SOCKADDR*)&addr, addr_len); //未绑定的套接字描述符 命名socket的地址 地址长度 if (errorCode == SOCKET_ERROR) { printf("bind error!"); closesocket(s); exit(1); } //截止到现在socket的全部属性已经被填充,客户端可以定位到服务器socket errorCode = listen(s, 3); if (errorCode == SOCKET_ERROR) { printf("listen error"); closesocket(s); //关闭套接字 exit(2); } //服务端的socket已经准备好... //现在开始客户端的socket printf("TCP SERVER start...\n"); SOCKET sockAccept; //执行accept函数后用于实际通信的套接字 SOCKADDR_IN ClientAddr; //客户端地址 int len = sizeof(ClientAddr); sockAccept = accept(s, (SOCKADDR FAR*)&ClientAddr, &len); //阻塞式的 if (sockAccept == INVALID_SOCKET) { printf("accept %d", WSAGetLastError()); closesocket(s); closesocket(sockAccept); WSACleanup(); return -1; } else { printf("%s", inet_ntoa(ClientAddr.sin_addr)); //打印接入地址 } //循环接收来自客户端的数据 while (true) { ZeroMemory(buf, BUF_SIZE); //每次清空缓冲 resValue = recv(sockAccept, buf, BUF_SIZE, 0); //接收来自客户端的数据 if (resValue == SOCKET_ERROR) { printf("recv failed!"); closesocket(s); closesocket(sockAccept); WSACleanup(); return -1; } SYSTEMTIME st; GetLocalTime(&st); char sDataTime[30]; sprintf(sDataTime, "%4d-%2d-%2d:%2d:%2d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute,st.wMinute, st.wSecond); printf("%s, Recv From Client [%s:%d] :%s\n", sDataTime, inet_ntoa(ClientAddr.sin_addr), ClientAddr.sin_port, buf); if (strcmp(buf, "quit") == 0) { resValue = send(sockAccept, "quit", strlen("quit"), 0); //发往客户端的数据 break; } //向客户端发送回显字符 else { char msg[BUF_SIZE]; sprintf(msg, "Message Received - %s", buf); resValue = send(sockAccept, buf, BUF_SIZE, 0); if (resValue == SOCKET_ERROR) { printf("Socket send Failed\n"); closesocket(s); closesocket(sockAccept); WSACleanup(); return -1; } } } closesocket(s); closesocket(sockAccept); if ((WSACleanup() == SOCKET_ERROR)) { MessageBox(NULL, L"清除失败", L"WindowsApi", 0); return 0; } system("pause"); return 0; } 客户端: #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") #include <stdio.h> #include <iostream> #include <string> #define BUF_SIZE 64 //定义缓冲区的大小 int main(int argc, char *argv[]) { WSADATA wsaData; SOCKET sHost; //创建客户端的socket int resValue; //函数返回值,用于查看函数执行情况 char buf[BUF_SIZE]; //缓冲区 if ((WSAStartup(MAKEWORD(2,2), &wsaData)) != 0) //用于初始化windows sockets并返回WSADATA结构体 { //MessageBox(NULL, L"无法初始化", L"WindowsApi", 0); printf("无法初始化!"); return 0; } sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sHost == INVALID_SOCKET) { printf("socket failed! \n"); WSACleanup(); return -1; } SOCKADDR_IN servAddr; //服务器的地址,端口号 servAddr.sin_family = AF_INET; servAddr.sin_addr.S_un.S_addr = inet_addr("222.18.167.206"); servAddr.sin_port = htons(9990); int sServerAddlen = sizeof(servAddr); //服务器地址长度 resValue = connect(sHost, (LPSOCKADDR)&servAddr, sizeof(servAddr)); //连接到服务器 //未连接的socket 指定要建立连接的socket的名称 if (resValue == SOCKET_ERROR) { printf("%ld", WSAGetLastError()); printf("connect failed!\n"); closesocket(sHost); WSACleanup(); return -1; } while (true) { printf("\nplease input a string to send\n"); std::string str; std::getline(std::cin, str); //接收输入的数据 ZeroMemory(buf,BUF_SIZE); strcpy(buf, str.c_str()); //将用户的输入复制到buf里面 resValue = send(sHost, buf, strlen(buf), 0); // if (resValue == SOCKET_ERROR) { printf("socket send failed!\n"); closesocket(sHost); WSACleanup(); return -1; } resValue = recv(sHost, buf, sizeof(buf) + 1, 0); //要接收数据的socket 要接收数据buf的地址 要接收数据buf的长度 0 if (resValue == SOCKET_ERROR) { printf("socket receive failed!\n"); closesocket(sHost); WSACleanup(); return -1; } printf("Receive from server: %s", buf); if (strcmp(buf, "quit") == 0) { printf("quit"); break; } } closesocket(sHost); WSACleanup(); system("pause"); return 0; }

上一篇:Centos5.5 下安装python-pip的坑
下一篇:cSplit和cvMerge代码示例

相关文章

相关评论