Messaging program?

This is a discussion on Messaging program? within the Linux Programming forums, part of the Platform Specific Boards category; I am going to attempt to create a messaging service and wondered if anyone could help and point me in ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    Messaging program?

    I am going to attempt to create a messaging service and wondered if anyone could help and point me in the right direction. I want to use TCP and the sockets api. I need to have 2 clients that will send messages to each other, but the messages will be relayed from a server so that it can store the messages and statistics.

    Any ideas or pieces of code that will be useful? I haven't tried network programming as of yet.

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    http://www.ecst.csuchico.edu/~beej/guide/net/ might help you with your first socket experience.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    4
    Ok this is what I have but have a few problems.

    This is the server code.

    Code:
    /* server.c */
    
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    main()
    {
    
       int cont,create_socket,new_socket,addrlen;
       int bufsize = 512;
       char filename [10];
       int messageno = 0;
       char *buffer = malloc(bufsize);
       struct sockaddr_in address;
       FILE *newfile;
    
       printf("\x1B[2J");
       if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
         printf("Socket created\n");
    
       address.sin_family = AF_INET;
       address.sin_addr.s_addr = INADDR_ANY;
       address.sin_port = htons(15000);
       if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
         printf("Socket Bound\n");
    
       listen(create_socket,3);
       addrlen = sizeof(struct sockaddr_in);
       new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
       if (new_socket > 0){
          printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
          for(cont=1;cont<5000;cont++)
    	printf("\x7");
       }
    
       do{
          recv(new_socket,buffer,bufsize,0);
          printf("Message recieved: %s\n",buffer);
          messageno = (messageno + 1);
          printf ("Saving to file\n");
          newfile = fopen ("messages.txt", "a");
          fprintf (newfile,"%d %s \n",messageno, buffer);
    
       }while(strcmp(buffer,"/q")); //user q to quit
          fclose (newfile);
       close(new_socket);
       close(create_socket);
    }
    And this is the client code.

    Code:
    /* client.c */
    
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<arpa/inet.h>
    
    
    main(int argc,char *argv[])
    {
    
       int create_socket;
       int bufsize = 160;
       char *buffer = malloc(bufsize);
       struct sockaddr_in address;
       int choice;
    
       void sendmess ()
       	 {
       		   printf("\x1B[2J");
       		   if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
       		     printf("Socket created\n");
       		   address.sin_family = AF_INET;
       		   address.sin_port = htons(15000);
       		   inet_pton(AF_INET,argv[1],&address.sin_addr);
    
       		   if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
       		     printf("Connection accepted with the server %s...\n\n",inet_ntoa(address.sin_addr));
    
       	    do{
       	 	   printf("Message to send: ");
       	 	   gets(buffer);
       	 	   send(create_socket,buffer,bufsize,0);
    
    
       	    }while (strcmp(buffer,"/q"));
       	    close(create_socket);
    }
    
    void recvmess ()
    {
    	 recv(create_socket,buffer,bufsize,0);
       	 printf("Message recieved: %s\n",buffer);
    }
    
    do {
    	 		printf(" Plesae choose and option\n");
    	 		printf(" \n\n");
    	 		printf(" Press <1> to send a message\n");
    	 		printf(" Press <2> to receive a message\n");
    	 		printf (" Please enter number and hit return key: ");
    	 		scanf ("%d",&choice);
    	 		switch(choice)
    	 		{
    	 			 case 1 : sendmess(); break;
    	 			 case 2 : recvmess(); break;
    	 			 case 0 : break;
    	 			 default :{
    	 							printf ("\nUnknown Command\n");
    	 							printf ("hit enter to continue\n");
    	 							fflush(stdin);
    	 							getchar();
    	 					  };break;
    	 		}
    	 	}while (choice != 0);
    
    }
    When the user on a client inputs a menu option, the server thinks this is a message and receives it, I don't know how to fix this. Also I am trying to get the server to store the messages as I want more than 1 client and so that they can address messages to each other which are relayed from the server.

    Any help would be appreciated.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM

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