![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 71
| Could someone explain how to use the Select function. Can anyone tell me how to make a non-blocking socket? [send and recive] |
| azjherben is offline | |
| | #2 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| LMGTFY Depends on OS, really. For most OS, I believe this is it: Code: int flags = fcntl(socket_fd, F_GETFL); fcntl(socket_fd, F_SETFL, flags (add or remove O_NONBLOCK)); Code: unsigned long what = (0 for blocking, anything else for non-blocking.); ioctlsocket(your_socket, FIONBIO, &what);
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
| | #3 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,378
| You use BLOCKING sockets with select(), not non-blocking. The purpose of select is to be able to wait until any one of an entire SET of sockets becomes ready for reading instead of blocking on a single one of them.
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #4 |
| Registered User Join Date: Mar 2009
Posts: 71
| This is one of mi header files. Tell me if I did it right. Code: //Socket.cpp
#include "Socket.h"
Socket::Socket()
{
if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
{
cerr<<"Socket Initialization: Error with WSAStartup\n";
system("pause");
WSACleanup();
exit(10);
}
//Create a socket
mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
unsigned long what = (4);
ioctlsocket(mySocket, FIONBIO, &what);
if ( mySocket == INVALID_SOCKET )
{
cerr<<"Socket Initialization: Error creating socket"<<endl;
system("pause");
WSACleanup();
exit(11);
}
myBackup = mySocket;
}
Socket::~Socket()
{
WSACleanup();
}
bool Socket::SendData( char *buffer )
{
send( mySocket, buffer, strlen( buffer ), 0 );
return true;
}
bool Socket::RecvData( char *buffer, int size )
{
int i = recv( mySocket, buffer, size, 0 );
buffer[i] = '\0';
return true;
}
void Socket::CloseConnection()
{
//cout<<"CLOSE CONNECTION"<<endl;
closesocket( mySocket );
mySocket = myBackup;
}
void Socket::GetAndSendMessage()
{
char message[STRLEN];
cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
cout<<"Send > ";
cin.get( message, STRLEN );
SendData( message );
}
void ServerSocket::StartHosting( int port )
{
Bind( port );
Listen();
}
void ServerSocket::Listen()
{
//cout<<"LISTEN FOR CLIENT..."<<endl;
if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
{
cerr<<"ServerSocket: Error listening on socket\n";
system("pause");
WSACleanup();
exit(15);
}
//cout<<"ACCEPT CONNECTION..."<<endl;
acceptSocket = accept( myBackup, NULL, NULL );
while ( acceptSocket == SOCKET_ERROR )
{
acceptSocket = accept( myBackup, NULL, NULL );
}
mySocket = acceptSocket;
}
void ServerSocket::Bind( int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
myAddress.sin_port = htons( port );
//cout<<"BIND TO PORT "<<port<<endl;
if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
{
cerr<<"ServerSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(14);
}
}
void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( ipAddress );
myAddress.sin_port = htons( port );
//cout<<"CONNECTED"<<endl;
if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
{
cerr<<"ClientSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(13);
}
}
|
| azjherben is offline | |
| | #5 | ||
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| Quote:
Furthermore, and more seriously, the man page for select() notes that: Quote:
1) You can use INADDR_ANY in place of inet_addr( "0.0.0.0" ); 2) If WSAStartup() fails, WSACleanup() is probably unneeded. 3) Code: acceptSocket = accept( myBackup, NULL, NULL );
while ( acceptSocket == SOCKET_ERROR )
{
acceptSocket = accept( myBackup, NULL, NULL );
}
mySocket = acceptSocket;
4) '4' is an arbitrary number... just because the man page said 'non-zero' doesn't mean you have to follow it that closely. Try something more sane, like... 1? Otherwise, readers are going to be wondering what '4' does. 5) In SendData(), check the return value. send() might not send() everything you ask of it, especially with non-blocking sockets. 5) RecvData has a buffer overflow. Code: bool Socket::RecvData( char *buffer, int size )
{
int i = recv( mySocket, buffer, size, 0 );
buffer[i] = '\0';
return true;
}
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) Last edited by Cactus_Hugger; 07-03-2009 at 12:56 PM. | ||
| Cactus_Hugger is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Troubleshooting Input Function | SiliconHobo | C Programming | 14 | 12-05-2007 07:18 AM |
| Calling a Thread with a Function Pointer. | ScrollMaster | Windows Programming | 6 | 06-10-2006 08:56 AM |
| Bisection Method function value at root incorrect | mr_glass | C Programming | 3 | 11-10-2005 09:10 AM |
| Change this program so it uses function?? | stormfront | C Programming | 8 | 11-01-2005 08:55 AM |
| Didn't quite know where to post this.......compiler problem... | incognito | C++ Programming | 5 | 02-08-2003 07:42 PM |