Archived

This topic is now archived and is closed to further replies.

Xtremehobo

Odd winsock problem

Recommended Posts

I''m trying to write a simple winsock server application that listens to port 21. When I use the telnet app to connect to my server, it all works fine and I''m able to pass data between telnet and my app. When I try to connect to the server through Internet Explorer however (using http://localhost:21/, my app won''t even detect internet explorer establishing a connection. Here''s the code I use for setting the server socket in the listening state, and for accepting connections:
  
/Wait for connection
	listen( server, SOMAXCONN );
	
	//Accept Connection

	SOCKET client;
	int length;
	length = sizeof sin;
	client = accept( server, (LPSOCKADDR)&sin, &length );

	int keepon=1;
	
	MessageBox(NULL,"Connection","Simple Winsock Server",MB_OK);
  
Is there some specific thing that I have to do if I wan''t internet explorer to be able to connect to my app? Thanks ----- I 4M T3H SIGN4TUR3 FR0M H311x0res!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it might be because your server app isn''t talking http, so internet explorer can''t connect.

Share this post


Link to post
Share on other sites
that shouldn''t matter. the client is suppose to make the first request (eg. "GET index.html HTTP/1.1")
IE isn''t even making a tcp connection to my server :|

Share this post


Link to post
Share on other sites
I just wrote up a simple server and it works with http (http://[ip addy]:[port]), so maybe if you showed some more code we could help you more.

-noix-

In this world gone mad, we won't spank the monkey; the monkey will spank us.

[edited by - noixtirdoe on May 7, 2003 2:31:05 PM]

Share this post


Link to post
Share on other sites
ok, here''s the complete code (for visual c++)

  
// Sock.cpp : Defines the entry point for the application.

//


#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
WSADATA wsaData;
WORD version;

bool InitSockets();

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Simple Winsock Server

if(!InitSockets()) {MessageBox(NULL,"Winsock failed to initialize.","Simple Winsock Server",MB_OK); return false;}

//Create the server socket

SOCKET server;
server = socket( AF_INET, SOCK_STREAM, 0 );

//Start the server

struct sockaddr_in sin;

memset( &sin, 0, sizeof sin );

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons( 21 );

if ( bind( server, (LPSOCKADDR)&sin, sizeof sin ) == SOCKET_ERROR ) { MessageBox(NULL,"Socket unable to bind with port.","Simple Winsock Server",MB_OK); return false; }

//Wait for connection

listen( server, SOMAXCONN );

//Accept Connection

SOCKET client;
int length;
length = sizeof sin;
client = accept( server, (LPSOCKADDR)&sin, &length );

int keepon=1;

MessageBox(NULL,"Connection","Simple Winsock Server",MB_OK);


while(keepon==1)
{
//Check for data

char buffer[2];
char sendbuffer[255];
sendbuffer[0]=0x44;
sendbuffer[1]=0xa3;
send(client,sendbuffer,sizeof(sendbuffer),0);
//for(int i=0;i<3;i++) { buffer[i]=0x00; sendbuffer[i]=0x00; }


recv(client,buffer,sizeof(buffer),0);
MessageBox(NULL,buffer,"Simple Winsock Server",MB_OK);

}
//Close sockets

closesocket(server);
closesocket(client);
WSACleanup();
// End Simple Winsock Server Code

return 0;
}

bool InitSockets()
{

int error;

version = MAKEWORD( 2, 0 );

error = WSAStartup( version, &wsaData );

/* check for error */
if ( error != 0 )
{
/* error occured */
return FALSE;
}

/* check for correct version */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
/* incorrect WinSock version */
WSACleanup();
return FALSE;
}
/* WinSock has been initialized */

return true;
}

Share this post


Link to post
Share on other sites