partitionist
Erfahrenes Mitglied
Damit ich eine Verbindung mit mehreren clients an ein server aufbauen kann, muss der server mit threads laufen, könnt ihr mir an diesem beispiel zeigen was ich machen soll:
Code:
SOCKET theSocket;
theSocket = socket(AF_INET, // Address family
SOCK_DGRAM, // Socket type
IPPROTO_UDP);// Protocol
if (theSocket == INVALID_SOCKET)
{
PRINTERROR("socket()");
return;
}
//
// Fill in the address structure
//
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; // Let WinSock assign address
saServer.sin_port = htons(nPort); // Use port passed from user
//
// bind the name to the socket
//
int nRet;
nRet = bind(theSocket, // Socket descriptor
(LPSOCKADDR)&saServer, // Address to bind to
sizeof(struct sockaddr) // Size of address
);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("bind()");
closesocket(theSocket);
return;
}
//
// This isn't normally done or required, but in this
// example we're printing out where the server is waiting
// so that you can connect the example client.
//
int nLen;
nLen = sizeof(SOCKADDR);
char szBuf[256];
nRet = gethostname(szBuf, sizeof(szBuf));
if (nRet == SOCKET_ERROR)
{
PRINTERROR("gethostname()");
closesocket(theSocket);
return;
}
//
// Show the server name and port number
//
printf("\nServer named %s waiting on port %d\n",
szBuf, nPort);
//
// Wait for data from the client
//
SOCKADDR_IN saClient;
memset(szBuf, 0, sizeof(szBuf));
nRet = recvfrom(theSocket, // Bound socket
szBuf, // Receive buffer
sizeof(szBuf), // Size of buffer in bytes
0, // Flags
(LPSOCKADDR)&saClient, // Buffer to receive client address
&nLen); // Length of client address buffer