You are not logged in.
Pages: 1
Hello all.
i write function to get text from gtk_text_view, and this text me need to send with socket..how to recive value which i get in gtk_text_view..
this is my full source
#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#define RCVBUFSIZE 1024
static void destroy(GtkWidget *widget);
int getext(GtkWidget *textview);
int inet(char *query);
GtkWidget *query;
int main(int argc, char** argv) {
GtkWidget *window, *button, *vbox, *button2;
GtkTextBuffer *buffer;
gchar *txt;
txt = "text";
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label("Close");
button2 = gtk_button_new_with_label("Send");
vbox = gtk_vbox_new(TRUE,10);
query = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(window),vbox);
gtk_container_add(GTK_CONTAINER(vbox),query);
gtk_container_add(GTK_CONTAINER(vbox),button);
gtk_container_add(GTK_CONTAINER(vbox),button2);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(query));
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(buffer),txt,strlen(txt));
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(destroy),NULL);
g_signal_connect_swapped(G_OBJECT(button),"clicked",G_CALLBACK(getext),GTK_TEXT_VIEW(query));
g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(inet),NULL);
gtk_widget_show(window);
gtk_widget_show(vbox);
gtk_widget_show(button);
gtk_widget_show(button2);
gtk_widget_show(query);
gtk_main();
return 0;
}
static void destroy(GtkWidget *widget) {
gtk_main_quit();
}
int getext(GtkWidget *textview) {
GtkTextIter start, end;
GtkTextBuffer *buffer;
char *text;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
gtk_text_buffer_get_bounds(buffer, &start, &end);
text = gtk_text_iter_get_text(&start, &end);
inet(text);
g_free(text);
return 0;
}
int inet(char *query) {
char message[sizeof(query)];
char buff[RCVBUFSIZE];
struct sockaddr_in addr;
int bytesRcvd, totalresv;
int sock;
sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sock < 0 ) {
perror("Socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.s_addr = inet_addr("some ip");
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("connect");
exit(2);
}
send(sock, query, sizeof(query), 0);
totalresv = 0;
while(totalresv < strlen(query)) {
if ((bytesRcvd = recv(sock, buff, RCVBUFSIZE -1, 0)) <= 0)
perror("Resv");
totalresv += bytesRcvd;
buff[bytesRcvd] = '\0';
printf("%s", buff);
}
printf("\n");
close(sock);
return 0;
}
Last edited by Dimon-z (2009-04-27 10:49:04)
Offline
Gw.. i'll check it out.
Offline
Ok...and if you can help me with this
Offline
up..up..very need help please
Offline
The function inet, query is a pointer, not an array, so sizeof(query) is equal to the size of the pointer (ie. the word size of your machine.) If you want to send the text itself, you ought to be transmitting strlen(query) (+ 1 byte if you want to include the null byte in the transmission.) Of course, given the lack of comments and the fact that the 'inet' function also seems to want to receive a string of equal length to the input from the same socket it just sent it down, I'm not really certain I understand what you're trying to achieve.
Offline
Pages: 1