Thread: Problem with my program.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    Problem with my program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <netdb.h>
    
    #define MYPORT 9034
    
    int main(void)
    {
    	int sockfd;
    	char ch;
    	struct sockaddr_in my_addr, their_addr;
    	char buffer[1000];
    	
    	sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	if(sockfd == -1)
    	{
    		printf("ERROR!!");
    		perror("socket");
    		getchar();
    		exit(1);
    	}
    	
            my_addr.sin_family = AF_INET;
            my_addr.sin_addr.s_addr = INADDR_ANY;
            my_addr.sin_port = htons(MYPORT);
            memset(&(my_addr.sin_zero), '\0', 8);
    		if( (bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)))==-1)    
    		{         
    			perror("bind");
                exit(1);
            }
    	
    	if( listen(sockfd, 10) == -1 )
    	{
    		printf("ERROR!!");
    		perror("listen");
    		getchar();
    		exit(1);
    	}
    	
    	scanf("&#37;c", &ch);
    	while( ch != 'q')
    	{
    		int state = sizeof(struct sockaddr);
    		int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &state);
    		if(new_fd == -1)
    		{
    			perror("accept");
    			exit(1);
    		}
    		
                    state = send(new_fd, buffer, strlen(buffer), 0);
    		sprintf(buffer, "%s has connected.\n", inet_ntoa(their_addr.sin_addr));
    		if(state == -1)
    		{	
    		printf("ERROR!!");
    		perror("send");
    		getchar();
    		exit(1);
    		}
    	}
    	
    	return 0;
    }
    ERRORS:
    Code:
    untitled.c:49: warning: pointer targets in passing argument 3 of ‘accept’ differ in signedness
    untitled.c:56: warning: implicit declaration of function ‘inet_ntoa’
    untitled.c:56: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    The compilation was finished successfully though but it doesn't work properly.

    How to fix it?
    I might be missing some headers.

    BTW,
    Is there any other function that I could get 1 character? fgets doesn't work for characters just for strings, and gets & scanf are dangerous.
    Any suggestions?
    Last edited by eXeCuTeR; 12-07-2007 at 09:12 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The first problem is that "state" should be a socklen_t type.

    And yes, the second problem is missing include file(s). Check up the inet_ntoa.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by matsp View Post
    The first problem is that "state" should be a socklen_t type.

    And yes, the second problem is missing include file(s). Check up the inet_ntoa.

    --
    Mats
    Socket length? strlen?
    #include <arpa/inet.h>

    why do I need to cast it to struct sockaddr? why to cast and why sockaddr and not sockaddr_in?
    also, why I have to type & before state and this casting thing and many others?
    why is there a struct sockaddr_in their_addr and then I cast it? and in the cast & is added? why is this casted to be a pointer?
    why is state stores accept too? it stores the size of sockaddr.
    why do I need the size of sockaddr?
    why do I need sin_zero?

    memset(&(my_addr.sin_zero), '\0', 8);
    again, why & is added?

    basically, these are whole of my questions, im reading Beej's book.
    I'll try to see if I have more questions.

    thank you for your time and patience

    edit:
    It doesn't work :S
    Last edited by eXeCuTeR; 12-07-2007 at 10:46 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    socklen_t is a typedef of some type that is unsigned. So you SHOULD use "socklen_t share" rather than "int share".

    As to the other questions, I don't know much about socket programming, so I can't comment on most.

    You need the & to get an address for memset, so that you can fill an area of memory with zero bytes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by matsp View Post
    socklen_t is a typedef of some type that is unsigned. So you SHOULD use "socklen_t share" rather than "int share".

    As to the other questions, I don't know much about socket programming, so I can't comment on most.

    You need the & to get an address for memset, so that you can fill an area of memory with zero bytes.

    --
    Mats
    Could anyone answer these please?

    and btw,
    Is there any other function that I could get 1 character? fgets doesn't work for characters just for strings, and gets & scanf are dangerous.
    maybe getchar();?
    Last edited by eXeCuTeR; 12-07-2007 at 10:39 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by eXeCuTeR View Post
    Could anyone answer these please?

    and btw,
    Is there any other function that I could get 1 character? fgets doesn't work for characters just for strings, and gets & scanf are dangerous.
    fgetc()?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    finally works
    btw, if any of you could still answer my questions ill be SO THANKFUL!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    #define MYPORT 6666
    
    int main(int argc, char *argv[])
    {
    	int sockfd, new_fd; // file descriptions: sockfd to create a connection, and new_fd to send and receive data.
    	char ch; // if q is pressed then stop the program
    	struct sockaddr_in my_addr; // server's info
    	struct sockaddr_in their_addr; // client's info
    	char buffer[1000]; // buffer for the output
    	socklen_t state;
    	
    	sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	if(sockfd == -1)	{
    		perror("socket");
    		exit(1);
    	}
    	
            my_addr.sin_family = AF_INET;
            my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
            my_addr.sin_port = htons(MYPORT);
            memset(&(my_addr.sin_zero), '\0', 8);
            
    		if( bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)	{         
    			perror("bind");
                exit(1);
            }
    	
    	if( listen(sockfd, 10) == -1 )	{
    		perror("listen");
    		exit(1);
    	}
    	
    	while(ch != 'q')
    	{
    		state = sizeof(struct sockaddr);
    		new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &state);
    		if(new_fd == -1)	{
    			perror("accept");
    			exit(1);
    		}
    		
    		state = send(new_fd, buffer, strlen(buffer), 0);
            printf("Your IP is: &#37;s\n", inet_ntoa(their_addr.sin_addr));
    		sprintf(buffer, "%s has connected.\n", inet_ntoa(their_addr.sin_addr));
    		if(state == -1)
    			printf("Couldn't send the message.\n");
    		
    		printf("Press 'q' to quit.\n");
    		ch = getchar();
    		putchar(ch);
    		close(new_fd);
    	}
    	close(sockfd);
    	return 0;
    }
    Last edited by eXeCuTeR; 12-07-2007 at 10:45 AM.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Please guys, answer my questions.

  9. #9
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by matsp View Post
    fgetc()?

    --
    Mats
    Didn't he answer your question? What other question are you seeking answer for?

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Aia View Post
    Didn't he answer your question? What other question are you seeking answer for?

    why do I need to cast it to struct sockaddr? why to cast and why sockaddr and not sockaddr_in?
    also, why I have to type & before state and this casting thing and many others?
    why is there a struct sockaddr_in their_addr and then I cast it? and in the cast & is added? why is this casted to be a pointer?
    why do I need the size of sockaddr?
    why do I need sin_zero?
    Last edited by eXeCuTeR; 12-08-2007 at 06:43 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you understand pointers?
    If you didn't use &, it would simply pass a local copy to the function which it modifies and which does not reflect its value back in your function, so you need to pass a pointer, hence you pass the address of the variable to make it a pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I know it does that.
    But I'm asking why? is it just the function parameters?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you understand pointers, and my reply, then you should understand that passing a non-pointer to memset would make no sense. Additionally, here's the prototype of memset:

    void *memset(void *dest, int c, size_t count);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Elysia View Post
    If you understand pointers, and my reply, then you should understand that passing a non-pointer to memset would make no sense. Additionally, here's the prototype of memset:
    I know how memset works but I'm talking about accept and the others.

    edit: lol, I forgot to delete this question.
    Last edited by eXeCuTeR; 12-08-2007 at 06:43 AM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You did ask about memset, and I did reply.
    As for accept - all I know is that its declaration is:

    Code:
    SOCKET accept(
      __in          SOCKET s,
      __out         struct sockaddr* addr,
      __in_out      int* addrlen
    );
    Actually, it is declared as socketaddr* for backwards compability, and does not strictly want a socketaddr structure. So if you pass something else, you need to cast it because socketaddr != socketaddr_in.
    Last edited by Elysia; 12-08-2007 at 06:48 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM