Host:192.168.100.218:5000
欢迎访问!!!
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
#pragma comment (lib, "ws2_32.lib")
using namespace std;
unsigned int i = 0;
const unsigned int MAXCNT = 32;
const unsigned int BUFLEN = 65536;
char bufhead[BUFLEN];
char bufbody[BUFLEN];
char sendbuf[BUFLEN];
char recvbuf[BUFLEN];
void init() {
char ch;
FILE* fp;
unsigned int it;
it = 0;
fp = fopen("head.txt", "r");
ch = fgetc(fp);
while (ch != EOF) {
if (ch != '\n')
bufhead[it++] = ch;
else {
bufhead[it++] = '\r';
bufhead[it++] = '\n';
}
ch = fgetc(fp);
}
bufhead[it++] = '\0';
fclose(fp);
it = 0;
fp = fopen("body.txt", "r");
ch = fgetc(fp);
while (ch != EOF) {
if (ch != '\n')
bufbody[it++] = ch;
else {
bufbody[it++] = '\r';
bufbody[it++] = '\n';
}
ch = fgetc(fp);
}
bufbody[it++] = '\0';
fclose(fp);
sprintf(sendbuf, bufhead, strlen(bufbody), bufbody);
printf("%s\n", sendbuf);
return;
}
void threadpro(LPVOID lpsock) {
SOCKET sock = (SOCKET)lpsock;
recv(sock, recvbuf, sizeof(recvbuf), 0);
printf("recv:\n%s\n", recvbuf);
send(sock, sendbuf, strlen(sendbuf) + 1, 0);
printf("send:\n%s\n", sendbuf);
closesocket(sock);
return;
}
int main() {
init();
SOCKET rcv;
SOCKET sock[MAXCNT];
WSADATA wsd;
sockaddr_in ser;
sockaddr_in cli;
WSAStartup(MAKEWORD(2, 2), &wsd);
int csize = sizeof(sockaddr);
ser.sin_family = AF_INET;
ser.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
ser.sin_port = htons(5000);
rcv = socket(AF_INET, SOCK_STREAM, 0);
bind(rcv, (sockaddr*)&ser, sizeof(sockaddr));
while (true) {
listen(rcv, 0);
sock[i++] = accept(rcv, (sockaddr*)&cli, &csize);
if (i < MAXCNT) {
DWORD id;
HANDLE hand = (HANDLE)::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadpro, (LPVOID)sock[--i], 0, &id);
}
else {
i--;
}
}
}