|
-
July 15th, 2005, 09:45 AM
#1
adhoc networking programming
Hi I am actually programming an adhoc network in Visual C++ without success. please can anyone suggest what I can do or what I am doing wrong. this is my code.
Code:
#include <stdio.h>
#include <winsock.h>
#include <iostream.h>
#define ATTACH 100
#define DETACH 200
#define ECHO 300
#define TIME 400
const UINT SRV_UDP_PORT = 5050;
CSrvrView::CSrvrView()
{
m_hListenerThread = NULL;
int status;
WSADATA wsaData;
m_data = "Initiallizing Windows Sockets DLL..."
if((status = WSAStartup(MAKEWORD(2,2), &wsaData)) == 0)
{
m_data += "Succeeded\n";
m_bInitialized = TRUE;
}
else
{
m_bInitialized = FALSE;
}
m_data += "Creating socket...";
//create socket
m_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (m_sock == INVALID_SOCKET)
{
m_data += "Failed\n";
}
//Bind socket to address
m_data =+ "Succeeded\nBinding socket..."
// set up address structure
SOCKADDR_IN sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(SRV_UDP_PORT);
if(bind(m_sock,(PSOCKADDR)&sa, sizeof(sa)) == SOCKET_ERROR)
{
m_data += "Failed\n";
closesocket(m_sock);
}
m_data += "Succeeded\nCreating listener thread...";
unsigned long idThread;
m_hListenerThread = CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)Listener,
(void *)this, 0, &idThread);
if(m_hListenerThread)
m_data += "Succeeded\nListening...\n";
else
m_data += "Failed\n";
}
//cleanup
CSrvrView::~CSrvrView()
{
if(m_bInitialized)
WSACleanup();
closesocket(m_sock);
if(m_hListenerThread)
::TerminateThread(m_hListenerThread, 0);
// define threads that recienves and process messages
long WINAPI Listener(CSrvrView* pView)
{
SOCKADDR_IN saClnt;
char msg[MSGSIZE];
int saClntLen, nchar, msglen;
while(1)
{
saClntLen = sizeof(saClnt);
nchar = recvfrom(pView->m_sock, msg, MSGSIZE, 0
(PSOCKADDR)&saClnt, &saClntLen);
if(nchar < 0)
{
pView->m_data += "Error in recvfrom\n";
pView->InvalidateRect(NULL);
}
else
{
msglen = nchar;
short *p_code = (short*)msg;
short code = ntohs(*p_code);
switch(code)
{
case ATTACH:
wsprintf(msg, "Client from %s attached\n",
inet_ntoa(saClnt.sin_addr));
msglen = strlen(msg);
pView->m_data += msg;
pView->InvalidateRect(NULL);
break;
case DETACH:
wsprintf(msg, "Client from %s detached\n",
inet_ntoa(saClnt.sin_addr));
msglen = strlen(msg);
pView->m_data += msg;
pView->InvalidateRect(NULL);
break;
case TIME:
time_t tnow;
time(&tnow);
sprintf(msg, "Time now: %s\n", ctime(&tnow));
msglen = strlen(msg);
break;
case ECHO:
saClntLen = sizeof(saClnt);
msglen = recvfrom(pView->m_sock, msg, MSGSIZE,
0, (PSOCKADDR)&saClnt, &saClntLen);
break;
default:
wsprintf(msg,
"Don't understand message: %d\n",
code);
msglen = strlen(msg);
}
}
//send result to client
sendto(pView->m_sock, msg, msglen, 0, (PSOCKADDR)&saClnt, saClntLen);
}
return(0);
}
it is not even working.
Last edited by Andreas Masur; July 19th, 2005 at 12:31 PM.
Reason: Added code tags...
-
July 19th, 2005, 12:30 PM
#2
Re: adhoc networking programming
-
July 19th, 2005, 10:55 PM
#3
Re: adhoc networking programming
 Originally Posted by charllychilly
... it is not even working.
Please elaborate. What "is not working"? Does it compile, for example? Does it run, but only badly? What does m_data look like (use the debugger to find out).
Incidentally, you are over-writing anything that might be received in the Listener thread's msg buffer. You receive data using
Code:
nchar = recvfrom(pView->m_sock, msg, MSGSIZE, 0
(PSOCKADDR)&saClnt, &saClntLen);
and then you immediately over-write it using
Code:
wsprintf(msg, "Client from %s attached\n",
inet_ntoa(saClnt.sin_addr));
You probably intended to use different buffers, so modify your code appropriately.
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|