Linux Socket programming help

This is a discussion on Linux Socket programming help within the C Programming forums, part of the General Programming Boards category; Hi, I'm creating a very basic chat program which doesn't use a server as such. If the application is run ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Linux Socket programming help

    Hi,

    I'm creating a very basic chat program which doesn't use a server as such.

    If the application is run with no arguments then it acts a pseudo server. If it has an argument (ip address) then it will connect to that host.

    I am fairly ( ) sure that the applications are connecting properly but when trying to send and receive messages all I get is either a few question marks or nothing. Often it garbles the display on the client terminal.

    once comiled usage is ./msg and ./msg 127.0.0.01

    for server and client respectively

    Any help would be greatly appreciated.




    PS

    I know that the whole send - receive timing and such isnt working yet I am trying to get them just to display any received messages first.
    Attached Files Attached Files
    • File Type: c msg.c (1.9 KB, 96 views)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, your indentation is horrible. Next, main returns an int, so stop with the "implicit int" stuff. Is it that hard to type three characters?
    Code:
    int buffsize=1024; /* Why aren't you using BUFSIZ? */
    
    char *recvd = malloc(buffsize);
    
    char *msg = malloc(buffsize);
    
    ...snip... 
    
    	//while (1)
    	//{
    		printf("Client says %s\n", &recvd); /* This is wrong. Lose the address-of. */
    		//break;
    //	}
    
    ...snip...
    	
    	scanf("%s", &msg); /* This is wrong. address-of isn't for pointers. Just use its name. */
    
    ...snip...
    
    int buffsize=1024; /* Why are you declaring another of these? */
    int buffsize1=1024;
    char *msgn = malloc(buffsize);
    
    char *msg= malloc(buffsize1);
    
    while (1)
    {
    //scanf("%s", &msg); /* This is wrong. */
    msg = "hello world\n\r";
    if(send (sock,msg, buffsize1,0)==-1)
    
    ...snip...
    
    printf("Server says: %s\n", &msgn); /* This is wrong. Lose the address-of. */
    That'll do for a start.

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

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Smile

    Hey thanks, a seems to be working much better. My lecturers never mention that address-of was only for variables that werent pointers.

    And yep my indenting sux lol, by the way, are there any user friendly IDE's for Linux, I'm currently using the text editor in RH9 which doesnt have even syntax highlighting.

    Thanks again, Mat

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I use vim. (Which is aliased by default on my machine as vi.) You can turn on syntax hilighting in it if it isn't on by default. You may not like it, but I do.

    Well address-of gives you the address of a variable. Pointers are just variables that store addresses. Everything is passed by value. That means, if you pass a variable X to a function, it takes the value that's stored in X and passes that. When a function takes a pointer as an argument, what actually happens is that it takes the value stored in the pointer (an address) and passes that value to the function.

    Therefore, you can fake you're using a pointer by passing the value of a variable's address to a function which would normally want a pointer's value. Remember, the value of a pointer is an address. So, by using the address-of operator, you get a value which is an address. You can take that value and pass it to a function which wants that sort of thing.

    Now all of that either helped greatly, or confused the hell out of you. Either way, my work here is done.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux raw socket programming
    By cnb in forum Networking/Device Communication
    Replies: 17
    Last Post: 11-08-2010, 07:56 AM
  2. socket programming in c or c++ in linux...
    By pk20 in forum Linux Programming
    Replies: 5
    Last Post: 02-12-2009, 12:54 AM
  3. linux, doubles, and unions
    By paraglidersd in forum Linux Programming
    Replies: 14
    Last Post: 11-19-2008, 10:41 AM
  4. pthread and socket porting from Linux to Windows
    By mynickmynick in forum C Programming
    Replies: 2
    Last Post: 07-18-2008, 06:57 AM
  5. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 07:40 PM

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