Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 1st, 2005, 7:06 PM   #1
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
hi all,

i've been programming in C for a while now, so i decided to learn socketprogramming...

well, i have written the following program, but it doesn't receive anything from the server...

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

#define PORT 80
#define IP "192.168.1.1"
int main(int argc, char *argv[])
{
 WSADATA wsa;
 SOCKET ConnectSocket;
 
 if(WSAStartup(MAKEWORD(1,1),&wsa)!=0) //windows socket startup
 {
   printf("Error: WSAStartup failed..");
   return EXIT_FAILURE;
 }
 ConnectSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); //create a socket
 if(ConnectSocket==INVALID_SOCKET)
 {
   printf("Error: creating socket..");
   WSACleanup();
   return EXIT_FAILURE;
 }
 
 struct sockaddr_in sockaddr;
 sockaddr.sin_family=AF_INET;
 sockaddr.sin_addr.s_addr=inet_addr(IP);
 sockaddr.sin_port=htons(PORT);
 
 if(connect(ConnectSocket,(SOCKADDR*)&sockaddr,sizeof(sockaddr))==SOCKET_ERROR) //connect to host
 {
   printf("Error: Failed to connect...");
   WSACleanup();
   return EXIT_FAILURE;
 }  
 printf("Connected\n");
 int bytesSent;
 int bytesRecv=SOCKET_ERROR;
 char sendbuf[]="get / http/1.1";
 char recvbuf[500];
 
 while( bytesRecv == SOCKET_ERROR )
 {
    bytesRecv=recv(ConnectSocket,recvbuf,500,0);
    bytesSent=send(ConnectSocket,sendbuf,strlen(sendbuf),0);
    printf("\ntest\n");
    if(bytesSent==0||bytesSent==WSAECONNRESET)
    {
      printf("Connection closed\n");
      break;
    } 
 }
 printf("\n%s\n",bytesRecv);
 
 WSACleanup();
 return EXIT_SUCCESS;
}

this is just a simple program to see if it works, but i have an output:
Quote:

Connected

test

(null)
all this program should do is send a "get / http/1.1" request to the server and receive the first page...

if i do the same with netcat it works fine, but with this program it doesn't work

i've ran the program while i had a sniffer on, and appearantly it is sending the get request to the server, but nothing is send back from the server...

what did i do wrong?


thanks in advance,

regards
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Jan 2nd, 2005, 3:42 PM   #2
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
It would appear that in the last few blocks of your code:

 while( bytesRecv == SOCKET_ERROR )
 {
   bytesRecv=recv(ConnectSocket,recvbuf,500,0);
   bytesSent=send(ConnectSocket,sendbuf,strlen(sendbuf),0);
   printf("\ntest\n");
   if(bytesSent==0||bytesSent==WSAECONNRESET)
   {
      printf("Connection closed\n");
      break;
   } 
 }

That you are not properly deciphering the error message the application is attempting to return to you. Here the only time an error will report properly is if the connection was reset... but it ignores all other possible errors...

You need to revanp your error handling code to check for other errors types and let us know when you have determined the error that is being returned.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Jan 4th, 2005, 4:12 PM   #3
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
it is nice to see you tell me the code is wrong, for the biggest part i just copied and pasted from the msdn example to try to get it to work, this is from the msdn site...way to go Microsoft!

i think i have figured out how it works, i have written a program to send email (it is beta), and that program works, so i think i know how to do it, if you would like to see the source i will PM it to you, but i have to warn you, it IS a REAL mess, since it is only for testing purposes...

thanks for your reply, i will take a look at the msdn code again, i think i understand more of it now
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC