The socket code works when i compile them in a separately programm. It looks like the vlc compiler ignore the Header file winsock2.h.
Here is my code :
Code: Select all
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <stdlib.h> /* malloc(), free() */
#include <string.h>
#include <stdio.h>
#include <vlc/vlc.h>
#include <vlc/intf.h>
#include <winsock2.h>
/*****************************************************************************
* Local prototypes.
*****************************************************************************/
static void Run ( intf_thread_t * );
/*****************************************************************************
* Open: initialize dummy interface
*****************************************************************************/
int E_(OpenIntf) ( vlc_object_t *p_this )
{
intf_thread_t *p_intf = (intf_thread_t*) p_this;
#ifdef WIN32
vlc_bool_t b_quiet;
b_quiet = config_GetInt( p_intf, "dummy-quiet" );
if( !b_quiet )
CONSOLE_INTRO_MSG;
#endif
msg_Info( p_intf, "Using the dummy interface module..." );
p_intf->pf_run = Run;
return VLC_SUCCESS;
}
/*****************************************************************************
* Run: main loop
*****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
int sock ; // our socket
int client ; // client socket
struct sockaddr_in local_addr ; // neded to specificate on what ethernet card/or modem we want to listen
struct sockaddr_in client_addr ; // stored client address
int len ; // nedded in accept () call
char buf[64] ;
WSADATA wsd ;
if (WSAStartup (0x2020, &wsd) != 0)
{
printf ("WSAStartup err = %d\n", WSAGetLastError ()) ;
return 0 ;
}
// 0 - selest the best protocol
// SOCK_STREAM - Protocol provides continuous transmission
// AF_INET - internet address family - IP
sock = socket (AF_INET, SOCK_STREAM, 0) ; // create socket
if (sock == -1) // socket () returns -1 on error
{
printf ("socket err = %d\n", WSAGetLastError ()) ;
return 0 ;
}
// fill address structure
local_addr.sin_family = AF_INET ; // same as in socket
local_addr.sin_port = htons (14500) ; // our port, i explain later htons ()
local_addr.sin_addr.s_addr = inet_addr ("0.0.0.0") ; // 0.0.0.0 address tell to listen on all ethernet
// cards/modems,
// inet_addr () - translates address with dots to number
// sock - socket handle
if (bind (sock, (struct sockaddr*)&local_addr, sizeof (local_addr)) == -1) // error, same as in socket
{
printf ("bind err = %d\n", WSAGetLastError ()) ;
return 0 ;
}
// 1 - tells to accept one client, read: (queqe has 0 lenght)
listen (sock, 1) ; // tell to listen for inbounds connections
// len is multi puprose
len = sizeof (client_addr) ; // first store in len leght of client_addr
// accept client
client = accept (sock, (struct sockaddr*)&client_addr, &len) ; // and then receive size of accepted address
// usually stored size == received size
if (sock == -1) // error
{
printf ("accept err = %d\n", WSAGetLastError ()) ;
return 0 ;
}
// recvs data
// buf - buffer to store incoming data
// 64 - max buffer lenght
// 0 - nothing interesting, should be zero. Other values only for special purprose
if (recv (client, buf, 64, 0) == -1)
{
printf ("recv err = %d\n", WSAGetLastError ()) ;
return 0 ;
}
printf ("%s received from %s:%d\n", buf, inet_ntoa (client_addr.sin_addr), ntohs (client_addr.sin_port)) ;
closesocket(sock) ; // you must close socket at end
WSACleanup () ;
return 0 ;
}