//Tcp.cpp 对TCP使用类的方式进行封装
#include<winsock2.h>
#include<iostream>
#include<string>
using namespace std;
#pragma comment(lib,"ws2_32.lib");
#define MY_SERVER 1 //启动服务器
#define MY_CLient 2 //启动客户端
class MY_TCP{
public:
typedef SOCKET MY_SOCKET;
typedef struct sockaddr_in sockaddr_in4;
public:
MY_TCP(char * ip, unsigned int port)
{
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
cout<<"Failed to load Winsock."<<endl;
return;
}
ser.sin_family = AF_INET;
ser.sin_addr.s_addr = inet_addr(ip);
ser.sin_port = htons(port);
}
MY_TCP(unsigned int ip, unsigned int port)
{
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
cout << "Failed to load Winsock." << endl;
return;
}
ser.sin_family = AF_INET;
ser.sin_addr.s_addr = htonl(ip);
ser.sin_port = htons(port);
}
void start( int type = MY_SERVER ){
tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
if ( tcp_socket == INVALID_SOCKET )
{
cout << "socket创建失败: " << WSAGetLastError() << endl;
return;
}
if ( type == MY_SERVER ) //服务器
{
if ( bind(tcp_socket,(LPSOCKADDR)&ser,sizeof(ser) ) == SOCKET_ERROR)
{
cout << "绑定失败: " << WSAGetLastError()<<endl;
return;
}
if (listen(tcp_socket, 5) == SOCKET_ERROR)
{
cout << "监听失败: " << WSAGetLastError() << endl;
return;
}
}
else if ( type == MY_CLient ) //客户端
{
if (connect(tcp_socket, (struct sockaddr*)&ser, sizeof(ser)) == INVALID_SOCKET ) //tcp_socket 本地 ser远程
{
cout << "连接失败:" << WSAGetLastError() << endl;
return;
}
}
else{
cout << "输入错误的服务类型,启动失败" << endl;
return;
}
}
SOCKET my_accept(){ //返回一个新的socket与客户端进行通信 返回的socket为已建立了链接的socket
int iLen = sizeof(client_addr); //客户端地址结构长度
SOCKET s;
s = accept(tcp_socket, (struct sockaddr*) &client_addr, &iLen);
return s;
}
int my_send(SOCKET s,char* sendbuf ){
int sleng;
int send_len = 0;
while (sendbuf[send_len] != '\0')send_len++;
sleng = send(s, sendbuf, send_len, 0);
if (sleng == SOCKET_ERROR)
{
cout <<"发送失败:" << WSAGetLastError() << endl;
}
return sleng;
}
int my_recv(SOCKET s,char* recvbuf){
int leng;
leng = recv(s, recvbuf, sizeof(recvbuf), 0); // tcp_socket 本地
return leng;
}
~MY_TCP(){
closesocket(tcp_socket);
WSACleanup();
}
public:
MY_SOCKET tcp_socket; //本地socket
private:
sockaddr_in4 ser,client_addr; //ipv4地址结构
WSADATA wsaData;
};
//主函数,每当接收到客户端的一个请求,就创建一个新的线程,然后去与相应的客户端进行通信
#include"TCP.cpp"
void stocp(string str,char temp[]){
for (int i = 0; i < str.size(); i++) temp[i] = str[i];
temp[str.size()] = '\0';
}
DWORD dwThreadId = 0;
DWORD WINAPI Thread1Proc(LPVOID lpParameter); //线程的执行函数
int main(){
char recvbuf[100] = {0};
MY_TCP mytcp("127.0.0.1", 5050);
mytcp.start(MY_SERVER);
cout << "服务器启动:"<<endl;
MY_TCP::MY_SOCKET new_socket;
while (1){
new_socket = mytcp.my_accept();
HANDLE hThread1 = CreateThread(NULL, 0, Thread1Proc, (LPVOID)&new_socket, 0, &dwThreadId); //创建线程1
cout << "ThreadID:" << dwThreadId<<endl;
CloseHandle(hThread1);
}
mytcp.~MY_TCP();
return 0;
}
DWORD WINAPI Thread1Proc(LPVOID lpParameter){
SOCKET s;
int iSent;
char buf1[] = "I am a server;\n";
s = *((SOCKET *)lpParameter);
for (int n = 0; n<10; n++)
{
iSent = send(s, buf1, sizeof(buf1), 0);
Sleep(1000);
}
closesocket(s);
return 0;
}
TCP并发服务器
最新推荐文章于 2023-03-05 17:38:36 发布