TCP实现点对点聊天(多线程)-服务器端
//**********操作步骤********************//
//1、编译:gcc TCPM.c -lws2_32 -o TCPM.exe
//2、运行:TCPM
#include <stdio.h>
#include <winsock2.h>
#include <pthread.h>
int nclients = 0;
void *hanld_client(void *arg);
int main(int argc, char *argv[])
{
//初始化动态链接库ws2_32.dll
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if(WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}
//创建socket套接字,指定使用TCP协议
SOCKET listensocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listensocket == INVALID_SOCKET)
{
printf("创建socket失败!!!");
return 0;
}
//填充服务器相关地址信息,绑定IP地址、协议及端口
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(8888);
server.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(listensocket, (struct sockaddr *)&server, sizeof(server