Code:
/* *****************************************************************************
*/
gint
main_function (GtkWidget * widget, guint datum)
{
/* which map in order is going to be downloaded? */
mapyczcount = 0;
int j;
for (j = 0; j <= 19; j++)
{
while (gtk_events_pending ())
gtk_main_iteration ();
downloadstart_cb(widget, datum, writebuff);
/* writebuff is char array, contents all info for server and seems to work ok */
}
}
gint
downloadstart_cb (GtkWidget * widget, guint datum, gchar writebuff3[2000])
{
struct sockaddr_in server;
struct hostent *server_data;
gchar str[100], sn[1000];
downloadfilelen = 0;
downloadactive = TRUE;
while (gtk_events_pending ())
gtk_main_iteration ();
/* open socket to port80 */
if ((dlsock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
perror (_("can't open socket for port 80"));
return (FALSE);
}
server.sin_family = AF_INET;
/* We retrieve the IP address of the server from its name: */
if (haveproxy)
g_strlcpy (sn, proxy, sizeof (sn));
else
{
g_strlcpy (sn, WEBSERVER, sizeof (sn));
}
if ((server_data = gethostbyname (sn)) == NULL)
{
perror (_("Can't resolve webserver address"));
}
memcpy (&server.sin_addr, server_data->h_addr, server_data->h_length);
server.sin_port = htons (proxyport);
/* We initiate the connection */
if (connect (dlsock, (struct sockaddr *) &server, sizeof server) < 0)
{
perror (_("unable to connect to Website"));
}
write (dlsock, writebuff3, strlen (writebuff3));
printf ("dlsock: %d, writebuff3: %s\n", dlsock, writebuff3);
dlbuff = g_new0 (gchar, 8192);
dlpstart = NULL;
dldiff = dlcount = 0;
g_snprintf (str, sizeof (str), _("Now connected to %s"),
WEBSERVER);
gtk_timeout_add (100, (GtkFunction) downloadslave_cb, widget);
return TRUE;
}
gint
downloadslave_cb (GtkWidget * widget, guint datum)
{
if (!downloadwindowactive ))
return FALSE;
FD_ZERO (&readmask);
FD_SET (dlsock, &readmask);
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
if (select (FD_SETSIZE, &readmask, NULL, NULL, &timeout) < 0)
{
perror ("select() call");
}
if (FD_ISSET (dlsock, &readmask))
{
memset (tmpbuff, 0, 8192);
if ((e = read (dlsock, tmpbuff, 8000)) < 0)
perror (_("read from Webserver"));
if (e > 0)
{
/* in dlbuff we have all download data */
memcpy ((dlbuff + dlcount), tmpbuff, e);
/* in dlcount we have the number of download bytes */
dlcount += e;
/* now we try to get the filelength and begin of the gif image data */
if (dlpstart == NULL)
{
/* CONTENT-LENGTH string should hopefully be in the first 4kB */
memcpy (tmpbuff, dlbuff, 4096);
/* We make of this a null terminated string */
tmpbuff[4096] = 0;
g_strup (tmpbuff);
p = strstr (tmpbuff, "CONTENT-LENGTH:");
if (p != NULL)
{
sscanf (p, "%s %d", str,
&downloadfilelen);
/* now we look for 2 cr/lf which is the end of the header */
dlpstart = strstr (tmpbuff, nn);
dldiff = dlpstart - tmpbuff + 4;
}
else if (dlcount > 1000)
{
/* Seems there is no CONTENT-LENGTH field */
dlpstart = strstr (tmpbuff, nn);
dldiff = dlpstart - tmpbuff + 4;
downloadfilelen = 200000;
}
}
/* Now we have the length and begin of the gif image data */
if ((downloadfilelen != 0) && (dlpstart != NULL))
{
dlbuff = g_renew (gchar, dlbuff,
dlcount + 8192);
g_snprintf (str, sizeof (str),
_("Downloaded %d kBytes"),
(dlcount - dldiff) / 1024);
while (gtk_events_pending ())
gtk_main_iteration ();
}
}
if ((e == 0) || ((downloadfilelen + dldiff) == dlcount))
{
if (downloadfilelen == 0)
g_snprintf (str, sizeof (str),
_("Download FAILED!"));
else
g_snprintf (str, sizeof (str),
_("Download finished, got %dkB"),
dlcount / 1024);
close (dlsock);
if (downloadfilelen != 0)
{
// save downloaded picture
s = gtk_entry_get_text (GTK_ENTRY (dltext));
if (local_config.dir_home[strlen (local_config.dir_home) - 1] != '/')
g_strlcat (local_config.dir_home, "/",
sizeof (local_config.dir_home));
g_strlcpy (downloadfilename, local_config.dir_home,
sizeof (downloadfilename));
g_snprintf (s2, sizeof (s2),
_("mapycz%d"), mapyczcount);
g_strlcat (downloadfilename, s2,
sizeof (downloadfilename));
fd = open (downloadfilename,
O_RDWR | O_TRUNC | O_CREAT, 0644);
write (fd, dlbuff + dldiff, dlcount - dldiff);
close (fd);
if (mapyczcount == 19)
{
// save info about big picture
g_strlcpy (downloadfilename, local_config.dir_home,
sizeof (downloadfilename));
g_strlcat (downloadfilename, s,
sizeof (downloadfilename));
// now join 20 small pictures together and make big one and save it
// ...
}
else
mapyczcount++;
}
}
downloadwindowactive = FALSE;
gtk_widget_destroy (downloadwindow);
gtk_timeout_add (3000, (GtkFunction) dlstatusaway_cb, widget);
return FALSE;
}
}
else
{
return TRUE;
}
return TRUE;
}
Where the problem could be? I`m quit desperate.