|
-
July 10th, 2002, 09:39 PM
#1
basic Socket program (Server)
hi
The following code snippet when i run under win2k advanced server, is giving error in send function...can anybody help me out as whats going wrong ??
#include <winsock.h>
#include <iostream.h>
void main()
{
WSADATA ws;
SOCKET s1,TempSock;
sockaddr_in SOCKADDR_IN;
WORD wVersionRequested;
char buffer[1024];
int size = 0;
int retVal = 0;
TempSock = SOCKET_ERROR;
wVersionRequested = MAKEWORD( 2, 2 );
cout<<"Server starting ..........\n";
retVal = WSAStartup(wVersionRequested,&ws);
if ( retVal != 0 )
{
cout<<"WSAStartup failed.....exiting....................\n";
exit(0);
}
else
{
cout<<"WSAStartup successful...............................\n";
}
s1=socket(PF_INET, SOCK_STREAM,0); /* SOCK_DGRAM for UDP protocol */
/* SOCK_STREAM for TCP/IP protocol */
if (s1 == INVALID_SOCKET)
{
cout<<"Socket creation failed..........................\n";
}
else
{
cout<<"Socket created successfully...............................\n";
}
SOCKADDR_IN.sin_family = AF_INET;
SOCKADDR_IN.sin_port = 50;
//SOCKADDR_IN.sin_addr.s_addr =INADDR_ANY; /* if we want to listen on multiple addresses */
SOCKADDR_IN.sin_addr.S_un.S_un_b.s_b1 = 127;
SOCKADDR_IN.sin_addr.S_un.S_un_b.s_b2 = 0;
SOCKADDR_IN.sin_addr.S_un.S_un_b.s_b3 = 0;
SOCKADDR_IN.sin_addr.S_un.S_un_b.s_b4 = 1;
retVal = bind(s1,(sockaddr *) (&SOCKADDR_IN),sizeof(SOCKADDR_IN));
if ( retVal == SOCKET_ERROR )
{
cout<<"Attempt to Bind failed.....exiting....................\n";
exit(0);
}
else
{
cout<<"Bind successful...............................\n";
}
// Start Listening.
listen(s1,1);
cout<<"Server fully UP.....Listening on port................\n.";
retVal = send(s1,"Hello",5,0);
if ( retVal != 0 )
{
cout<<"send failed.....exiting....................\n" ;
cout<<"Error : "<<WSAGetLastError()<<"\n";
exit(0);
}
else
{
cout<<"send successful...............................\n";
}
//Accept the connection from client
while (TempSock == SOCKET_ERROR)
{
TempSock = accept(s1, NULL, NULL);
}
s1 = TempSock;
}
-
July 10th, 2002, 11:37 PM
#2
What is the error you're getting ... could it be WSAE_NOTCONNECTED [or whatever it is]?
From looking at your code, it looks like you're calling send() before you've established a connection with your client.
--Paul
-
July 11th, 2002, 05:31 AM
#3
Yes, i am getting error as WSAENOTCONN
but its not the problem that i am using send before establsing the connection as if i put that accept loop before , send then it just goes into hang state??
You can compile above snippet under Microsoft C++ compile and check the output as i ve put few cout statements to debug it...
Thanks in advance!
-
July 11th, 2002, 06:31 AM
#4
Your assertion that the program is hanging is incorrect. Change your debug strings; take out the "\n" and use endl instead. endl flushes the buffer, whereas putting "\n" directly in the string does not. The program isn't hanging at all; it's going through the while loop like you asked it to.
You cannot send() on a socket if you haven't established a connection; trust me.
--Paul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|