GTK with tcp/ip socket: how can you do both?

This is a discussion on GTK with tcp/ip socket: how can you do both? within the C Programming forums, part of the General Programming Boards category; I put together a quick program from tutorials that acts like a server listening on a socket. The program pauses ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    GTK with tcp/ip socket: how can you do both?

    I put together a quick program from tutorials that acts like a server listening on a socket. The program pauses on the line

    n = read(newsockfd,buffer,255);

    I wanted to add a window to this program with gtk, but I realized I don't know how to have both together. As gtk_main() also causes the program to pause...

    How can you have a gtk window and have the program listen on a socket?

    Code:
    #include <stdio.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <gtk/gtk.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(1);
    }
    
    unsigned char readSocket(){
         unsigned char temp;
         int sockfd, newsockfd, portno, clilen;
         char buffer[256];
         struct sockaddr_in serv_addr, cli_addr;
         int n;
         sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if (sockfd < 0) 
            error("ERROR opening socket");
         bzero((char *) &serv_addr, sizeof(serv_addr));
         portno = atoi("60000");
         serv_addr.sin_family = AF_INET;
         serv_addr.sin_addr.s_addr = INADDR_ANY;
         serv_addr.sin_port = htons(portno);
         if (bind(sockfd, (struct sockaddr *) &serv_addr,
                  sizeof(serv_addr)) < 0) 
                  error("ERROR on binding");
         listen(sockfd,5);
         
         for(;;){
         clilen = sizeof(cli_addr);
         newsockfd = accept(sockfd, 
                     (struct sockaddr *) &cli_addr, 
                     &clilen);
         if (newsockfd < 0) 
              error("ERROR on accept");
         bzero(buffer,256);
         n = read(newsockfd,buffer,255);
         if (n < 0) error("ERROR reading from socket");
         printf("%s",buffer);
    
         close(newsockfd);
         }
         return 0;
    
    }
    
    int main(int argc, char *argv[])
    {
       
      GtkWidget *window;
    
      gtk_init(&argc, &argv);
    
      window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      gtk_widget_show(window);
    
      gtk_main();
    
      readSocket();  
        
         return 0; 
    }

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    I guess I'll try making it a multi-thread app?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Never mind, got it to work by making it a multithreaded app.

    Yay google

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the local socket program occur core dump?
    By chenayang in forum Linux Programming
    Replies: 16
    Last Post: 08-16-2010, 08:39 AM
  2. Gtk Plug, Socket, embed one program into another.
    By arkashkin in forum Linux Programming
    Replies: 2
    Last Post: 03-27-2010, 01:01 AM
  3. TCP/IP Socket Programming Exercise
    By Martin_T in forum C Programming
    Replies: 9
    Last Post: 11-10-2009, 03:20 PM
  4. TCP/IP and Socket programming
    By CompiledMonkey in forum C Programming
    Replies: 2
    Last Post: 02-21-2003, 02:47 PM
  5. TCP/IP Socket
    By Nor in forum Linux Programming
    Replies: 4
    Last Post: 04-22-2002, 11:21 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21