socket management
Page 1 of 13 Replies - 1393 Views - Last Post: 11 September 2007 - 11:37 PM
#1
socket management
Posted 08 September 2007 - 11:50 PM
i'm currently writing a pseudo-chat program, and it's my first real project using sockets. my operating system / language of choice is windows/c, and i would like to be able to manage ( send() / recv() ) multiple connections at the same time. how can i do something like this? help is always appreciated. thanks
Replies To: socket management
#3
Re: socket management
Posted 11 September 2007 - 06:16 PM
alright, i think my server is able to handle multiple connections now. however, when i Ctrl+C'd out of the client, the server went bonkers with messages. can anyone tell me if what i'm doing is right?
i'll post the code for the client if i have to as well.
#include <stdio.h>
#include <winsock.h>
#include <windows.h>
#define MAX_CONNECTIONS 7
typedef struct _connectionData {
SOCKET socket;
char charCode[2];
char user[8],pass[8];
int intCode;
char message[100];
} CONNECTIONDATA, *PCONNECTIONDATA;
SOCKET establish(unsigned short portnum);
DWORD WINAPI handleConnection(LPVOID lpParam);
void checkForError(int result, char *function);
SOCKET connections[MAX_CONNECTIONS];
int sockets[MAX_CONNECTIONS];
DWORD dwThreadId[MAX_CONNECTIONS];
HANDLE hThread[MAX_CONNECTIONS];
short i = 0;
SOCKET hoster;
int main()
{
WSADATA info;
if (WSAStartup(MAKEWORD(1,1), &info) != 0)
{
printf("Cannot initialize WinSock!");
exit(1);
}
if((hoster = establish(5697)) == INVALID_SOCKET)
{
printf("Cannot establish socket on port 5697!");
exit(1);
}
int len = sizeof(struct sockaddr_in);
PCONNECTIONDATA pConData;
// Create MAX_CONNECTIONS worker threads.
for( i=0; i<MAX_CONNECTIONS; i++ )
{
// Allocate memory for thread data.
pConData = (PCONNECTIONDATA) HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, sizeof(CONNECTIONDATA));
if( pConData == NULL )
ExitProcess(2);
// Generate unique data for each thread.
pConData->socket = accept(hoster,NULL,NULL);;
hThread[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
handleConnection, // thread function
pConData, // argument to thread function
0, // use default creation flags
&dwThreadId[i]); // returns the thread identifier
// Check the return value for success.
if (hThread[i] == NULL)
ExitProcess(i);
}
// Wait until all threads have terminated.
WaitForMultipleObjects(MAX_CONNECTIONS, hThread, TRUE, INFINITE);
// Close all thread handles upon completion.
for(i=0; i<MAX_CONNECTIONS; i++)
{
CloseHandle(hThread[i]);
}
WSACleanup();
return 0;
}
SOCKET establish(unsigned short portnum)
{
char myname[256];
SOCKET s;
struct sockaddr_in sa;
struct hostent *hp;
memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
gethostname(myname, sizeof(myname)); /* who are we? */
hp = gethostbyname(myname); /* get our address info */
if (hp == NULL) /* we don't exist !? */
return(INVALID_SOCKET);
sa.sin_family = hp->h_addrtype; /* this is our host address */
sa.sin_port = htons(portnum); /* this is our port number */
s = socket(AF_INET, SOCK_STREAM, 0); /* create the socket */
if (s == INVALID_SOCKET)
return INVALID_SOCKET;
/* bind the socket to the internet address */
if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
{
closesocket(s);
return(INVALID_SOCKET);
}
listen(s, MAX_CONNECTIONS); /* max # of queued connects */
return(s);
}
DWORD WINAPI handleConnection(LPVOID lpParameter)
{
PCONNECTIONDATA pConData = (PCONNECTIONDATA)lpParameter;
int result;
while(1)
{
result = recv(pConData->socket,pConData->charCode,2,0);
checkForError(result,"recv()");
int pConData->intCode = atoi(pConData->charCode);
if(pConData->intCode == 0)
printf("Error. Bad packet sent.");
switch(pConData->intCode)
{
case 10:
printf("Successful connection.\n");
break;
case 11:
printf("Login requested.\n");
result = recv(pConData->socket,pConData->user,8,0);
checkForError(result,"recv()");
result = recv(pConData->socket,pConData->pass,8,0);
checkForError(result,"recv()");
printf("user:%s\npass:%s\n",pConData->user,pConData->pass);
result = send(pConData->socket,"12",2,0);
checkForError(result,"send()");
break;
case 14:
printf("Account creation requested.\n");
break;
case 17:
printf("User list requested.\n");
break;
case 19:
printf("Text being received.\n");
break;
}
}
HeapFree(GetProcessHeap(), 0, pConData);
return 0;
}
void checkForError(int result, char *function)
{
if(result == SOCKET_ERROR)
{
printf("%s failed with error: %d\n",function,WSAGetLastError());
printf("Please contact apples.\n");
}
}
i'll post the code for the client if i have to as well.
#4
Re: socket management
Posted 11 September 2007 - 11:37 PM
result = recv(pConData->socket,pConData->charCode,2,0);
When the client disconnecteds, Winsock lets you know by returning something from this function;
you want something like that
When the client disconnecteds, Winsock lets you know by returning something from this function;
result = recv(pConData->socket,pConData->charCode,2,0);
if(result <= 0)
{
//disconnected
//cleanup
return 0;
}
you want something like that
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|