abdul
Jul 30th, 2001, 01:14 AM
I have just started learning Winsock. I have problem running the first example of winsock then just sits and listens to the port. I am using VC++ 6.0 and here is the code:
#include <winsock.h>
#include <windows.h>
WSADATA info;
WORD version = MAKEWORD(1,1);
WSAStartup(version, &info);
/* code to establish a socket
*/
SOCKET establish(unsigned short portnum)
{ char myname[256];
SOCKET s;
struct sockaddr_in sa;
struct hostent *hp;
memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
gethostname(myname, sizeof(myname)); /* who are we? */
hp = gethostbyname(myname); /* get our address info */
if (hp == NULL) /* we don't exist !? */
return(INVALID_SOCKET);
sa.sin_family = hp->h_addrtype; /* this is our host address */
sa.sin_port = htons(portnum); /* this is our port number */
s = socket(AF_INET, SOCK_STREAM, 0); /* create the socket */
if (s == INVALID_SOCKET)
return INVALID_SOCKET;
/* bind the socket to the internet address */
if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) ==
SOCKET_ERROR) {
closesocket(s);
return(INVALID_SOCKET);
}
listen(s, 3); /* max # of queued connects */
cout<<myname;
return(s);
WSACleanup();
}
The errors that I get are here:
--------------------Configuration: winsockcon - Win32 Debug--------------------
Compiling...
winsock1.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2501: 'WSAStartup' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2373: 'WSAStartup' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winsock.h(781) : see declaration of 'WSAStartup'
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2078: too many initializers
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2440: 'initializing' : cannot convert from 'struct WSAData *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(33) : error C2065: 'cout' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(33) : error C2297: '<<' : illegal, right operand has type 'char [256]'
Error executing cl.exe.
winsockcon.exe - 6 error(s), 0 warning(s)
Can you please tell me what I am doing wrong here.
I am now going to sleep and I will check tomorrow : (
#include <winsock.h>
#include <windows.h>
WSADATA info;
WORD version = MAKEWORD(1,1);
WSAStartup(version, &info);
/* code to establish a socket
*/
SOCKET establish(unsigned short portnum)
{ char myname[256];
SOCKET s;
struct sockaddr_in sa;
struct hostent *hp;
memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
gethostname(myname, sizeof(myname)); /* who are we? */
hp = gethostbyname(myname); /* get our address info */
if (hp == NULL) /* we don't exist !? */
return(INVALID_SOCKET);
sa.sin_family = hp->h_addrtype; /* this is our host address */
sa.sin_port = htons(portnum); /* this is our port number */
s = socket(AF_INET, SOCK_STREAM, 0); /* create the socket */
if (s == INVALID_SOCKET)
return INVALID_SOCKET;
/* bind the socket to the internet address */
if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) ==
SOCKET_ERROR) {
closesocket(s);
return(INVALID_SOCKET);
}
listen(s, 3); /* max # of queued connects */
cout<<myname;
return(s);
WSACleanup();
}
The errors that I get are here:
--------------------Configuration: winsockcon - Win32 Debug--------------------
Compiling...
winsock1.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2501: 'WSAStartup' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2373: 'WSAStartup' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\winsock.h(781) : see declaration of 'WSAStartup'
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2078: too many initializers
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(5) : error C2440: 'initializing' : cannot convert from 'struct WSAData *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(33) : error C2065: 'cout' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\winsockcon\winsock1.cpp(33) : error C2297: '<<' : illegal, right operand has type 'char [256]'
Error executing cl.exe.
winsockcon.exe - 6 error(s), 0 warning(s)
Can you please tell me what I am doing wrong here.
I am now going to sleep and I will check tomorrow : (