Re: WSA & GTK
- From: Igor Chetverovod <chetverovod gmail com>
- To: Martin Pavelka <dlxxko gmail com>
- Cc: "gtk-list gnome org" <gtk-list gnome org>
- Subject: Re: WSA & GTK
- Date: Mon, 5 Mar 2012 10:08:16 +0400
2012/2/25, Martin Pavelka <dlxxko gmail com>:
> Dear GTK Team
>
> I would like to ask just one question... I'm working on WinSock2 project
> using GTK2 and I would like to make asynchronous sockets using
> WSAAsyncSelect function... I need it only on client side :)
> I would like to ask about way have to make program checking arriving
> connection and reciving bytes with this function without making loop which
> cause program freeze without async and just one check with async... I need
> to run this check during normal program main progress...
>
> Is there any possible way how to implement it? How to perform this check? Or
> at least to do my WSA loop and run GTK program together? I have googled but
> everywhere is only WinApi stupid solution :/
>
> Thank you for your reply :)
> Martin Pavelka
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
>
Hi Martin,
If you have no ability to use GSocket, you can use winsock2 in
nonblocking mode by means gio_channel.
Like this(Sorry for not english comments):
gboolean ethernet_port::udp_bind(void)
{
/**\brief Функция выполняет привязку сокета к внутреннему порту компьютера.
*
* Номер порта читается из файла конфигурации. В случае успеха, функция
* регистрирует в основном цикле функцию-обработчик события прихода данных
* от сервера.
*/
gboolean no_success = true;
int res = -1;
res = bind( sock, (struct sockaddr *) &(local_addr), sizeof(local_addr));
if (res<0)
{
// Error...
#ifdef WIN32
int error = WSAGetLastError();
g_print ("%s socket port %i binding... Error=%i.\n ", name,
ntohs(local_addr.sin_port), error );
#endif
}
else
{
g_print ("\nBinding for client socket %i (%s) was done.\n", sock, name);
is_binded=true;
/*
Если порт используется не для сервера, то чтобы оградить себя от
дейтаграмм отправленных не с адреса addr. Делаем наш UDP сокет
присоединенным.
*/
if(!server_mode) connect(sock,(struct sockaddr *)
&(remote_addr), sizeof(remote_addr));
#ifdef WIN32
// Создаем на базе сокета канал ввода вывода
gich = g_io_channel_win32_new_socket (sock);
#else
// Создаем на базе сокета канал ввода вывода
gich = g_io_channel_unix_new(sock);
#endif
// устанавливаем режим кодировки
switch (g_io_channel_set_encoding(gich, NULL, NULL))
{
case G_IO_STATUS_NORMAL: break;
default: g_print("GIOChannel initialization: can't change
coding.\n");
}
g_io_channel_set_close_on_unref(gich, TRUE);
/* Регистрируем в главном цикле и назначаем callback функцию на
* событие связанное с появлением данных на входе сокета.
*/
event_source_id = g_io_add_watch_full( gich, 0, G_IO_IN,
trigger_data_in_buf, this, NULL);
if(!event_source_id) g_on_error_stack_trace (NULL);
g_print("GIOChannel initialization event_source_id:%i\n",
event_source_id);
no_success = false;
}
return no_success;
}
Best regards,
Igor
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]