/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);
Odd winsock problem
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:
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!
it might be because your server app isn''t talking http, so internet explorer can''t connect.
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 :|
IE isn''t even making a tcp connection to my server :|
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]
-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]
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=0x00; sendbuffer=0x00; }<br></font><br> <br> recv(client,buffer,<font color="blue">sizeof</font>(buffer),0);<br> MessageBox(NULL,buffer,<font color="darkred">"Simple Winsock Server"</font>,MB_OK);<br><br> }<br> <font color="gray">//Close sockets<br></font><br> closesocket(server);<br> closesocket(client);<br> WSACleanup();<br> <font color="gray">// End Simple Winsock Server Code<br></font><br> <font color="blue">return</font> 0;<br>}<br><br><font color="blue">bool</font> InitSockets()<br>{<br> <br> <font color="blue">int</font> error;<br><br> version = MAKEWORD( 2, 0 );<br><br> error = WSAStartup( version, &wsaData );<br><br> <font color="gray">/* check for error */<br> if ( error != 0 )<br> {<br> /* error occured */<br> return FALSE;<br> }<br><br> /* check for correct version */<br> if ( LOBYTE( wsaData.wVersion ) != 2 ||<br> HIBYTE( wsaData.wVersion ) != 0 )<br> {<br> /* incorrect WinSock version */<br> WSACleanup();<br> return FALSE;<br> }<br> /* WinSock has been initialized */</font><br> <font color="blue">return</font> true;<br>}<br> </pre></DIV><!–ENDSCRIPT–>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement