一、Socket
#pragma once
#include<iostream>
#include<cstdlib>
#include<WinSock2.h>
#include<WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#define SERVER_IP "192.168.1.1"
#define SERVER_PORT 7777
#define BUFSIZE 1024
using namespace std;
class MySocket {
public:
WORD version = MAKEWORD(2, 2);
WSADATA lpData;
sockaddr_in hostAddr;
sockaddr_in clientAddr;
struct in_addr addr;
SOCKET listenSock;
SOCKET clientSock;
int count = 1;
public:
MySocket() {}
~MySocket() {}
bool SockInit();
bool SockClose();
bool SockExit();
};
bool MySocket::SockInit() {
this->hostAddr.sin_family = AF_INET;
this->hostAddr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_IP, (void*)&this->addr);
this->hostAddr.sin_addr = this->addr;
cout << "net IP:" << this->addr.S_un.S_addr << endl;
bool ret = WSAStartup(this->version, &this->lpData);
if (!ret) {
cout << "Init success." << endl;
}
else {
cout << "Init failed." << endl;
return false;
}
this->listenSock = socket(AF_INET, SOCK_STREAM, 0);
if (listenSock != INVALID_SOCKET) {
cout << "Create socket success." << endl;
}
else {
cout << "Create socket failed." << endl;
return false;
}
return true;
}
bool MySocket::SockClose() {
int ret = closesocket(this->listenSock);
if (ret != SOCKET_ERROR) {
cout << "Close success." << endl;
}
else {
cout << "Close failed." << endl;
return false;
}
return true;
}
bool MySocket::SockExit() {
int ret = WSACleanup();
if (!ret) {
cout << "WSACleanup success." << endl;
}
else {
cout << "WSACleanup failed." << endl;
return false;
}
return true;
}
二、Server
Server.h
#pragma once
#include"Socket.h"
class Server : public MySocket {
private:
public:
Server() {}
~Server() {}
bool SockBind();
bool SockListen();
bool SockAccept();
bool SockRecv();
};
bool Server::SockBind() {
bool ret = bind(this->listenSock, (struct sockaddr*)&this->hostAddr, sizeof(sockaddr));
if (!ret) {
cout << "Bind success." << endl;
}
else {
cout << "Bind failed." << ret << endl;
return false;
}
return true;
}
bool Server::SockListen() {
bool ret = listen(this->listenSock, 3);
if (!ret) {
cout << "Listen ..." << endl;
}
else {
cout << "Listen faided." << endl;
return false;
}
return true;
}
bool Server::SockAccept() {
int len = sizeof(struct sockaddr);
this->clientSock = accept(this->listenSock, (struct sockaddr*)&this->clientAddr, &len);
if (this->clientSock == INVALID_SOCKET) {
cout << "Accept failed." << endl;
cout << WSAGetLastError() << endl;
system("pause");
return false;
}
cout << "Accept client:" << (struct sockaddr*)&this->clientAddr << endl;
return true;
}
bool Server::SockRecv() {
char buf[BUFSIZE] = "\0";
int bufLen = recv(this->clientSock, buf, BUFSIZE, 0);
if (bufLen == SOCKET_ERROR) {
cout << "Recv failed." << endl;
return false;
}
cout << "Recieve data " << this->count++ << ": "<< buf << endl;
return true;
}
Server.cpp
#include"Server.h"
int main() {
Server myServer;
myServer.SockInit();
myServer.SockBind();
myServer.SockListen();
myServer.SockAccept();
while (myServer.SockRecv()) {
}
myServer.SockClose();
myServer.SockExit();
system("pause");
return 0;
}
三、Client
Client.h
#pragma once
#include"Socket.h"
class Client : public MySocket {
private:
public:
Client() {}
~Client() {}
bool SockConnect();
bool SockSend();
};
bool Client::SockConnect() {
int ret = connect(this->listenSock, (sockaddr*)&hostAddr, sizeof(sockaddr));
if (ret != INVALID_SOCKET) {
cout << "Connect success." << endl;
}
else {
cout << "Connect failed." << endl;
system("pause");
return false;
}
return true;
}
bool Client::SockSend() {
char buf[BUFSIZE] = "\0";
cout << "Please input data: ";
cin >> buf;
int ret = send(this->listenSock, buf, strlen(buf), 0);
if (ret == SOCKET_ERROR) {
cout << "Send failed." << endl;
system("pause");
return false;
}
else {
cout << "Send success." << endl;
}
return true;
}
Client.cpp
#include"Client.h"
int main() {
Client myClient;
myClient.SockInit();
myClient.SockConnect();
while (myClient.SockSend()) {
}
myClient.SockClose();
myClient.SockExit();
system("pause");
return 0;
}