Pepper
New Member
meep meep...!
Posts: 444
|
Post by Pepper on May 16, 2004 4:46:12 GMT -5
Anyone got a clue for this?
Like basic Socket listening :/
|
|
Tewl
xTeam
C# & VB Developer
Posts: 495
|
Post by Tewl on May 16, 2004 9:50:27 GMT -5
not in C++ but you need to use threads.
|
|
Pepper
New Member
meep meep...!
Posts: 444
|
Post by Pepper on May 16, 2004 15:24:04 GMT -5
/me hates threads
tried them in my ircbot
bah stupid things
|
|
Pepper
New Member
meep meep...!
Posts: 444
|
Post by Pepper on May 16, 2004 15:24:38 GMT -5
C/c++ btw..
|
|
|
Post by Armstrong on May 17, 2004 16:56:12 GMT -5
You know how to create sockets or only want to know how to put it on a listening state?
If you know how to create socket than use listen function.
int listen( SOCKET s, int backlog);
sck = your socket. backlog = Maximum length of the queue of pending connections. Set it to SOMAXCONN to let your system decide maximum reasonable value.
create the socket, bind it to addr, set it to listen, and process incomming requests with accept call.
|
|
Pepper
New Member
meep meep...!
Posts: 444
|
Post by Pepper on May 18, 2004 3:23:00 GMT -5
/me dosent know.
|
|
|
Post by Armstrong on May 18, 2004 14:25:09 GMT -5
// Description[img]http://proboards8.com/boardimages/sad.gif[/img]br]// This example illustrates a simple TCP server that accepts // incoming client connections. Once a client connection is // established, a thread is spawned to read data from the // client and echo it back (if the echo option is not // disabled). // // Command line options[img]http://proboards8.com/boardimages/sad.gif[/img]br]// server [-p:x] [-i:IP] [-o] // -p:x Port number to listen on // -i:str Interface to listen on // -o Receive only, don't echo the data back // #include <winsock2.h>
#include <stdio.h> #include <stdlib.h>
#define DEFAULT_PORT 7001 #define DEFAULT_BUFFER 4096
int iPort = DEFAULT_PORT; // Port to listen for clients on BOOL bInterface = FALSE, // Listen on the specified interface bRecvOnly = FALSE; // Receive data only; don't echo back char szAddress[128]; // Interface to listen for clients on
// // Function: usage // // Description[img]http://proboards8.com/boardimages/sad.gif[/img]br]// Print usage information and exit // void usage() { printf("usage: server [-p:x] [-i:IP] [-o]\n\n"); printf(" -p:x Port number to listen on\n"); printf(" -i:str Interface to listen on\n"); printf(" -o Don't echo the data back\n\n"); ExitProcess(1); } // // Function: ValidateArgs // // Description[img]http://proboards8.com/boardimages/sad.gif[/img]br]// Parse the command line arguments, and set some global flags // to indicate what actions to perform // void ValidateArgs(int argc, char **argv) { int i;
for(i = 1; i < argc; i++) { if ((argv[i][0] == '-') || (argv[i][0] == '/')) { switch (tolower(argv[i][1])) { case 'p'[img]http://proboards8.com/boardimages/sad.gif[/img]br] iPort = atoi(&argv[i][3]); break; case 'i'[img]http://proboards8.com/boardimages/sad.gif[/img]br] bInterface = TRUE; if (strlen(argv[i]) > 3) strcpy(szAddress, &argv[i][3]); break; case 'o'[img]http://proboards8.com/boardimages/sad.gif[/img]br] bRecvOnly = TRUE; break; default[img]http://proboards8.com/boardimages/sad.gif[/img]br] usage(); break; } } } }
// // Function: ClientThread // // Description[img]http://proboards8.com/boardimages/sad.gif[/img]br]// This function is called as a thread, and it handles a given // client connection. The parameter passed in is the socket // handle returned from an accept() call. This function reads // data from the client and writes it back. // DWORD WINAPI ClientThread(LPVOID lpParam) { SOCKET sock=(SOCKET)lpParam; char szBuff[DEFAULT_BUFFER]; int ret, nLeft, idx;
while(1) { // Perform a blocking recv() call // ret = recv(sock, szBuff, DEFAULT_BUFFER, 0); if (ret == 0) // Graceful close break; else if (ret == SOCKET_ERROR) { printf("recv() failed: %d\n", WSAGetLastError()); break; } szBuff[ret] = '\0'; printf("RECV: '%s'\n", szBuff); // // If we selected to echo the data back, do it // if (!bRecvOnly) { nLeft = ret; idx = 0; // // Make sure we write all the data // while(nLeft > 0) { ret = send(sock, &szBuff[idx], nLeft, 0); if (ret == 0) break; else if (ret == SOCKET_ERROR) { printf("send() failed: %d\n", WSAGetLastError()); break; } nLeft -= ret; idx += ret; } } } return 0; }
// // Function: main // // Description[img]http://proboards8.com/boardimages/sad.gif[/img]br]// Main thread of execution. Initialize Winsock, parse the // command line arguments, create the listening socket, bind // to the local address, and wait for client connections. // int main(int argc, char **argv) { WSADATA wsd; SOCKET sListen, sClient; int iAddrSize; HANDLE hThread; DWORD dwThreadId; struct sockaddr_in local, client;
ValidateArgs(argc, argv); if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { printf("Failed to load Winsock!\n"); return 1; } // Create our listening socket // sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (sListen == SOCKET_ERROR) { printf("socket() failed: %d\n", WSAGetLastError()); return 1; } // Select the local interface and bind to it // if (bInterface) { local.sin_addr.s_addr = inet_addr(szAddress); if (local.sin_addr.s_addr == INADDR_NONE) usage(); } else local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_family = AF_INET; local.sin_port = htons(iPort);
if (bind(sListen, (struct sockaddr *)&local, sizeof(local)) == SOCKET_ERROR) { printf("bind() failed: %d\n", WSAGetLastError()); return 1; } listen(sListen, 8); // // In a continous loop, wait for incoming clients. Once one // is detected, create a thread and pass the handle off to it. // while (1) { iAddrSize = sizeof(client); sClient = accept(sListen, (struct sockaddr *)&client, &iAddrSize); if (sClient == INVALID_SOCKET) { printf("accept() failed: %d\n", WSAGetLastError()); break; } printf("Accepted client: %s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)sClient, 0, &dwThreadId); if (hThread == NULL) { printf("CreateThread() failed: %d\n", GetLastError()); break; } CloseHandle(hThread); } closesocket(sListen); WSACleanup(); return 0; }
|
|
Tewl
xTeam
C# & VB Developer
Posts: 495
|
Post by Tewl on May 18, 2004 16:42:59 GMT -5
told ya, ya needed threads lol
|
|
|
Post by Armstrong on May 19, 2004 0:29:26 GMT -5
I never tried listening without threads, but theoritically it should work without major problems. However, with threads it runs smoothly
|
|
Pepper
New Member
meep meep...!
Posts: 444
|
Post by Pepper on May 19, 2004 2:06:44 GMT -5
hmm, i guess i need to fix threads in my ircbot <.>
|
|