#include
#include
#pragma comment(lib,"ws2_32.lib")
#define PORT 15001
#define ERROR 0
#define BUFFER_SIZE 1024
typedef struct Info
{
char client_name[30];
char buffer[1024];
}message;
int main()
{
WSADATA WSAData;
WSAStartup(MAKEWORD(2, 0), &WSAData);
SOCKET sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in ServerAddr;
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(PORT);
ServerAddr.sin_addr.s_addr = INADDR_ANY;
bind(sock, (LPSOCKADDR)&ServerAddr, sizeof(ServerAddr));
listen(sock, 10);
SOCKET msgsock;
// 用于接收结构
char buf[1024] = { 0 };
msgsock = accept(sock, (LPSOCKADDR)0, (int *)0);
recv(msgsock, buf, sizeof(buf), 0);
// 接收到结构,强制类型转换
message *msg = (message*)buf;
printf("%s \n", msg->client_name);
printf("%s \n", msg->buffer);
closesocket(sock);
WSACleanup();
system("pause");
return 0;
}