![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
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:
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 |
|
|
|
|
|
|
#2 |
|
Expert Programmer
|
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 <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|