Thread: compiler destroys terminal

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Question compiler destroys terminal

    Hi!

    I'm trying to compile a program in C, but when I run it the terminal goes nuts and looks like this:
    Code:
    ├┤␍␊┼├@⎽▒┼┼├␋␍⎽⎻␌:·/⎽▒┼┼⎻⎼⎺±01/␤␋␤␋/⎻⎼⎺⎽┘␊┐├/␤␊␋⎽$
    I'm compiling it with "gcc -lpthread -o server server.c" and the code I'm compiling follows beneath my name. I'm posting the whole thing since I can't read what the error may be. Can anyone see what's wrong? :|

    Harald

    Code:
    /* Creates a datagram server.  The port 
       number is passed as an argument.  This
       server runs forever */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <arpa/inet.h>
    
    #define BROADCASTPORT 9856    // the port users will be connecting to
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    /* Broadcasting all info to the network */
    int server_broadcast()
    {
       int sock, length, fromlen, n;
       struct sockaddr_in server;
       struct sockaddr_in from;
       char buf[1024];
    	int broadcast = 1;
    	
       sock=socket(AF_INET, SOCK_DGRAM, 0);
       if (sock < 0) error("Opening socket");
          
       if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast,
           sizeof(broadcast)) == -1) {
           perror("setsockopt (SO_BROADCAST)");
           exit(1);
       }   
        
       length = sizeof(server);
       bzero(&server,length);
       server.sin_family=AF_INET;
       server.sin_addr.s_addr=INADDR_ANY;
       server.sin_port=htons(BROADCASTPORT);
       if (bind(sock,(struct sockaddr *)&server,length)<0) 
           error("binding");
       fromlen = sizeof(struct sockaddr_in);
       while (1) {
           write(1,buf,n);
           n = sendto(sock,"Broadcast message\n",19, 0,(struct sockaddr *)&from,fromlen);
           if (n  < 0) error("sendto");
       }
    }
    
    /* Listener for incoming datagrams from clients */
    int client_listener()
    {
       int sock, length, fromlen, n;
       struct sockaddr_in server;
       struct sockaddr_in from;
       char buf[1024];
    
      
       sock=socket(AF_INET, SOCK_DGRAM, 0);
       if (sock < 0) error("Opening socket");
       length = sizeof(server);
       bzero(&server,length);
       server.sin_family=AF_INET;
       server.sin_addr.s_addr=INADDR_ANY;
       server.sin_port=htons(BROADCASTPORT);
       if (bind(sock,(struct sockaddr *)&server,length)<0) 
           error("binding");
       fromlen = sizeof(struct sockaddr_in);
       while (1) {
           n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
           if (n < 0) error("recvfrom");
           write(1,"Received a datagram: ",21);
           write(1,buf,n);
           n = sendto(sock,"Got your message\n",17, 0,(struct sockaddr *)&from,fromlen);
           if (n        < 0) error("sendto");
       }
     }
     
    int main(int argc, char *argv[])
    {
    	pthread_t thread1, thread2;
    	int nr1, nr2, var;
    	
    	nr1 = pthread_create(&thread1,NULL, (void *)server_broadcast, NULL);
    	/* nr2 = pthread_create(&thread2,NULL, (void *)client_listener, (void*) port);	*/
    
    	pthread_exit(NULL);	
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The compiler isn't doing anything to your terminal. Your crappy program is. Man I hate sensationalists.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Something is printed. That means you can find your problem by tracing it backwards starting from the function calls that output stuff.
    Also, use STDOUT_FILENO instead of 1 in your write() calls.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > write(1,buf,n);
    Both buf and n contain garbage at this point in the progrm.

    Also, try to do this without using threads, it only makes it way more complicated than it needs to be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM