// WindowsSocketServer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include #pragma comment(lib,"Ws2_32.lib")using namespace std;
#define PORT 8081
#define IP_ADDRESS "172.16.20.181"
DWORD WINAPI ClientThread(LPVOID lpParameter){SOCKET ClientSocket = (SOCKET)lpParameter;int Ret = 0;char RecvBuffer[MAX_PATH];while ( true ){memset(RecvBuffer, 0x00, sizeof(RecvBuffer));Ret = recv(ClientSocket, RecvBuffer, MAX_PATH, 0);if ( Ret == 0 || Ret == SOCKET_ERROR ) {cout<<"客户端退出!"<<endl;break;}cout<<"接收到客户信息为:"<endl;// send msg to clientRet = send(ClientSocket, RecvBuffer, (int)strlen(RecvBuffer), 0);if ( Ret == SOCKET_ERROR ){cout<<"Send Info Error::"<endl;break;}cout<<"After Send Msg:"<endl;}return 0;}int main(int argc, char* argv[]){WSADATA Ws;SOCKET ServerSocket, ClientSocket;struct sockaddr_in LocalAddr, ClientAddr;int Ret = 0;int AddrLen = 0;HANDLE hThread = NULL;//Init Windows Socket//The WSAStartup function initiates use of WS2_32.DLL by a process.if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 ){cout<<"Init Windows Socket Failed::"<endl;return -1;}//Create SocketServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if ( ServerSocket == INVALID_SOCKET ){cout<<"Create Socket Failed::"<endl;return -1;}//the address of family specificationLocalAddr.sin_family = AF_INET;//The inet_addr function converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the IN_ADDR structure.LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS); //The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian).LocalAddr.sin_port = htons(PORT);//Sets buffers to a specified character.memset(LocalAddr.sin_zero, 0x00, 8);//Bind Socket,The bind function associates a local address with a socket.Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr));if ( Ret != 0 ){cout<<"Bind Socket Failed::"<endl;return -1;}//The listen function places a socket in a state in which it is listening for an incoming connection.Ret = listen(ServerSocket, 10);if ( Ret != 0 ){cout<<"listen ClientSocket Failed::"<endl;return -1;}cout<<"服务端已经启动"<<endl;while ( true ){AddrLen = sizeof(ClientAddr);//The accept function permits an incoming connection attempt on a socket.ClientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);if ( ClientSocket == INVALID_SOCKET ){cout<<"Accept Failed::"<endl;break;}//The inet_ntoa function converts an (Ipv4) Internet network address into a string in Internet standard dotted format.cout<<"客户端连接::"<":"<endl;//Call this function to create a thread that can use CRT functions.hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)ClientSocket, 0, NULL);if ( hThread == NULL ){cout<<"Create Thread Failed!"<<endl;break;}CloseHandle(hThread);}closesocket(ServerSocket);closesocket(ClientSocket);WSACleanup();return 0;}
// WindowsSocketClient.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include #pragma comment(lib,"Ws2_32.lib")using namespace std;#define PORT 8081
#define IP_ADDRESS "172.16.20.181"int _tmain(int argc, _TCHAR* argv[])
{WSADATA Ws;SOCKET ClientSocket;struct sockaddr_in ServerAddr;int Ret = 0;int AddrLen = 0;HANDLE hThread = NULL;char SendBuffer[MAX_PATH];//Init Windows Socket//The WSAStartup function initiates use of WS2_32.DLL by a process.if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 ){cout<<"Init Windows Socket Failed::"<endl;return -1;}//Create SocketClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if ( ClientSocket == INVALID_SOCKET ){cout<<"Create Socket Failed::"<endl;return -1;}ServerAddr.sin_family = AF_INET;ServerAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);ServerAddr.sin_port = htons(PORT);memset(ServerAddr.sin_zero, 0x00, 8);//Bind Socket,The bind function associates a local address with a socket.//Ret = bind(ClientSocket, (struct sockaddr*)&ServerAddr, sizeof(ServerAddr));//if ( Ret != 0 )//{// cout<<"Bind Client Socket Failed::"<// //return -1;//}
Ret = connect(ClientSocket,(struct sockaddr*)&ServerAddr, sizeof(ServerAddr));if ( Ret == SOCKET_ERROR ){cout<<"Connect Error::"<endl;// return -1;
}else{cout<<"连接成功!"<<endl;}while ( true ){cin.getline(SendBuffer, sizeof(SendBuffer));Ret = send(ClientSocket, SendBuffer, (int)strlen(SendBuffer), 0);if ( Ret == SOCKET_ERROR ){cout<<"Send Info Error::"<endl;break;}char recvBuf[500];int byteRecv = recv(ClientSocket, recvBuf, MAX_PATH, 0);if (byteRecv != 0){recvBuf[byteRecv] = 0;cout << "From server: " << recvBuf << endl;}}closesocket(ClientSocket);WSACleanup();system("pause");return 0;}