469,955 Members | 1,441 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

connecting to a modem using winsock

Hi all,

There are two questions

1. I am trying to write a programme which can query to a modem get the modem details. i am using normal send() and recv() functions. But when i try to fetch a file from the modem. It shows me...

Error:400 Bad request.

2. The modem requires a username and password to get inside it. How can i set the username and password into it?

Here goes my code..

I hope my approach is correct. May be the way i am doing it is wrong.

#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include<conio.h>

#define DEFAULT_PORT 80

#define DEFAULT_PROTOCOL SOCK_STREAM // TCP
#define PRINTERROR(s) \
fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())

int main(void)
{
WSADATA wsaData;
SOCKET conn_socket;
struct sockaddr_in server;
struct hostent *hp;
char *server_name = "192.168.1.1";// modem IP address
unsigned short port = DEFAULT_PORT;
unsigned int addr = 0;
int socket_type = DEFAULT_PROTOCOL;
int retval = 0;
int loopflag = 0;
int loopcount = 0;
int maxloop = -1;
int i = 0;
char szBuffer[1024];
int nRet;


FILE *fp = fopen("read.txt","w");

if( WSAStartup(0x202,&wsaData) == SOCKET_ERROR )
{
fprintf(stderr,"WSAStartup failed with error %d\n",WSAGetLastError());
WSACleanup();
return -1;
}


if( isalpha(server_name[0]) )
{
hp = gethostbyname(server_name);
}
else
{
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr,4,AF_INET);
}


if( hp == NULL )
{
fprintf(stderr,"Client: Cannot resolve address [%s]: Error %d\n",
server_name,WSAGetLastError());
WSACleanup();
exit(1);
}

memset(&server,0,sizeof(server));
memcpy(&(server.sin_addr),hp->h_addr,hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);

conn_socket = socket(AF_INET,socket_type,0); /* Open a socket*/

if( conn_socket < 0 )
{
fprintf(stderr,"Client: Error Opening socket: Error %d\n",
WSAGetLastError());
WSACleanup();
return -1;
}


printf("Client connecting to: %s\n",hp->h_name);

if(connect(conn_socket,(struct sockaddr*)&server,sizeof(server)) == SOCKET_ERROR)
{
fprintf(stderr,"connect() failed: %d\n",WSAGetLastError());
fprintf(stderr,"Server not found...\n\n");
WSACleanup();
return -1;
}

/* Trying to get data from the modem*/

sprintf(szBuffer, "GET %s\n","/cgi-bin/webcm.html\n","\n");

nRet = send(conn_socket, szBuffer, strlen(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("send()");
closesocket(conn_socket);
}

while(1)
{
nRet = recv(conn_socket, szBuffer, sizeof(szBuffer), 0);
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recv()");
break;
}

if (nRet == 0)
break;
fwrite(szBuffer, nRet, 1,fp);

}
closesocket(conn_socket);
fclose(fp);
return 0;
}

Anybody has any idea how to proceed. Please help. Sorry if there are any spelling mistakes.

Thanks in advance
Feb 2 '07 #1
0 1820

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ashish | last post: by
3 posts views Thread by chethana | last post: by
5 posts views Thread by kc | last post: by
17 posts views Thread by Franc Zabkar | last post: by
1 post views Thread by Nicolas Ghesquiere | last post: by
1 post views Thread by ElvisRS | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.