socket problem with broadcasting
Hi,
I am working on creating a small file Sender program. It's for a local program running on a personal intranet. I would need a master machine to be able to broadcast some data to slave machines. The master machine's ip is known in advanced. I've looked at the winsock documentation on msdn and it's not too clear about broadcasting stuff.
I currently can open a bound connection with another machine with a fix ip thanks to examples on msdn but the broadcasting stuff does not work.
Any pointers would be highly appreciated. Thanks
Amish
Re: socket problem with broadcasting
I found this piece of code on another thread
Code:
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// Create a socket.
SOCKET m_socket;
//m_socket = socket( AF_INET, SOCK_STREAM, 0 );
m_socket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return nRetCode;
}
// Connect to a server.
sockaddr_in clientService;
//Socket Options
int optval=1;
int optlen=sizeof(optval);
if(setsockopt(m_socket,SOL_SOCKET,SO_BROADCAST,(char *) &optval,optlen))
{
printf("Cannot create a BROADCAST socket\n");
closesocket(m_socket);
return nRetCode;
}
clientService.sin_family = AF_INET;
clientService.sin_port = htons( 27015 );
clientService.sin_addr.s_addr = htonl(INADDR_BROADCAST);
if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return nRetCode;
}
// Send and receive data.
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";
if(sendto(m_socket,sendbuf,strlen(sendbuf),MSG_DONTROUTE,(SOCKADDR*) &clientService,sizeof(struct sockaddr_in))==-1)
{
printf("Can't send message\n");
closesocket(m_socket);
}
else
{
printf("\nMessage sent successfully\n");
closesocket(m_socket);
}
but when I use a program called tcpview This is what I see
Process || Protocol || local address || remote address
soctest2.exe:3248 || UDP || 0.0.0.0:1955 || *:*
How come the local address shows these weird values. Can somebody enlighten me here cause I am at a loss. Thanks I really appreciate any help
Amish
Re: socket problem with broadcasting
Quote:
Process || Protocol || local address || remote address
This is all good and it is rite,
what u have done is opened a socket handle -> set protocol type UDP.
the local address is 0.0.0.0 and port 1925. the foreign address will be *.* because it is not connected to any thing, and it is in broadcast state.
now u are creating a SOCK_DGRAM and then u are "connecting" it ????
DGRAM is not connected !!! SOCK_STREAM is connected.
anyway, ur sendto function seems to be at fault here !
Quote:
To send a broadcast (on a SOCK_DGRAM only), the address in the to parameter should be constructed using the special IP address INADDR_BROADCAST (defined in Winsock2.h), together with the intended port number. It is generally inadvisable for a broadcast datagram to exceed the size at which fragmentation can occur, which implies that the data portion of the datagram (excluding headers) should not exceed 512 bytes.
If no buffer space is available within the transport system to hold the data to be transmitted, sendto will block unless the socket has been placed in a nonblocking mode. On nonblocking, stream oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both the client and server systems. The select, WSAAsyncSelect or WSAEventSelect function can be used to determine when it is possible to send more data.
Calling sendto with a len of zero is permissible and will return zero as a valid value. For message-oriented sockets, a zero-length transport datagram is sent.
The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket. The semantics of this function are determined by the socket options and the flags parameter. The latter is constructed by using the bitwise OR operator with any of the following values.
Thats what MSDN has to say abt it. :wave:
Re: socket problem with broadcasting
Thanks for the reply. Lemme see if I get this right
So if I am creating a SOCK_DGRAM, I cannot use connect function. So can I just remove the connect part and use the sendto without connecting to anything. As for 0.0.0.0 and port 1925, I am still confused where it's getting the numbers. Is it automatically assigned by the OS. Can I modify the port number to be what I want.
As for the sendto, it seems alright. I don't understand why you said the problem is there. It follows the instruction on the website.
The to parameter uses
clientService.sin_family = AF_INET;
clientService.sin_port = htons( 27015 );
clientService.sin_addr.s_addr = htonl(INADDR_BROADCAST);
and the size of the packet is < 512
Thanks for your time
Amish
Re: socket problem with broadcasting
Hi axr0284...
1. You can modify port number by using bind() function. This function will assign your connection port number. If you don't specify connection port number, OS will assign for you.
2. Please be careful when call WSACleanup(). WSACleanup() will stop winsock library. So don't call this function if you are not finished using socket yet.
3. If you fail to send data to broadcast address, check your windows (policy) setting. It allows you to send data to broadcast or not. In some Operating Systems, only administrator is allowed to send to broadcast address.
Henky
[email protected]