Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Basic Socket Programming Error in Linux.. Pls help (http://www.programmingforums.org/showthread.php?t=13936)

boraciner Sep 7th, 2007 8:52 AM

Basic Socket Programming Error in Linux.. Pls help
 
Hi! I don't have experiance in linux C programming. I took a linux socket programming book and tried the first example code but It didn't work. I use these codes.. please help me!
Thanks for your replies

:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
int
main(int argc,char **argv) {
    int z;                  /* Status return code */
    int s[2];                /* Pair of sockets */
    /*
      * Create a pair of local sockets:
      */
    z = socketpair(AF_LOCAL,SOCK_STREAM,0,s);
    if ( z == -1 ) {
        fprintf(stderr,
            “%s: socketpair(AF_LOCAL,SOCK_STREAM,0)\n”,
            strerror(errno));
        return 1;          /* Failed */
    }
    /*
      * Report the socket file descriptors returned:
      */
    printf(“s[0] = %d;\n”,s[0]);
    printf(“s[1] = %d;\n”,s[1]);
    system(“netstat --unix -p”);
    return 0;
}



Error Message:

:

bora.c: In function ‘main’:
bora.c:18: error: stray ‘\342’ in program
bora.c:18: error: stray ‘\200’ in program
bora.c:18: error: stray ‘\234’ in program
bora.c:18: error: expected expression before ‘%’ token
bora.c:18: error: stray ‘\’ in program
bora.c:18: error: stray ‘\342’ in program
bora.c:18: error: stray ‘\200’ in program
bora.c:18: error: stray ‘\235’ in program
bora.c:25: error: stray ‘\342’ in program
bora.c:25: error: stray ‘\200’ in program
bora.c:25: error: stray ‘\234’ in program
bora.c:25: error: expected expression before ‘%’ token
bora.c:25: error: stray ‘\’ in program
bora.c:25: error: stray ‘\342’ in program
bora.c:25: error: stray ‘\200’ in program
bora.c:25: error: stray ‘\235’ in program
bora.c:26: error: stray ‘\342’ in program
bora.c:26: error: stray ‘\200’ in program
bora.c:26: error: stray ‘\234’ in program
bora.c:26: error: expected expression before ‘%’ token
bora.c:26: error: stray ‘\’ in program
bora.c:26: error: stray ‘\342’ in program
bora.c:26: error: stray ‘\200’ in program
bora.c:26: error: stray ‘\235’ in program
bora.c:27: error: stray ‘\342’ in program
bora.c:27: error: stray ‘\200’ in program
bora.c:27: error: stray ‘\234’ in program
bora.c:27: error: ‘netstat’ undeclared (first use in this function)
bora.c:27: error: (Each undeclared identifier is reported only once
bora.c:27: error: for each function it appears in.)
bora.c:27: error: expected ‘)’ before numeric constant
bora.c:27: error: stray ‘\342’ in program
bora.c:27: error: stray ‘\200’ in program
bora.c:27: error: stray ‘\235’ in program




I used this command to compile

:

gcc bora.c -o bora `pkg-config --cflags --libs gtk+-2.0`

DaWei Sep 7th, 2007 11:08 AM

You cannot use quotes of the opening/closing type. Use the ordinary double-quote character, ".

boraciner Sep 8th, 2007 6:47 AM

First of all, Thanks for your reply DaWei.
As I see, I didn't use ' character instead of " in my codes. My english isn't very well so, I couldn't understand where I did a mistake. Can you edit for me then I will be able to understand these codes correctly.

DaWei Sep 8th, 2007 6:56 AM

This: “s[0] = %d;\n” is not the same thing as
this: "s[0] = %d;\n"

boraciner Sep 8th, 2007 7:40 AM

ok! I will try '' in a moment. I'll write what terminal displays.

DaWei Sep 8th, 2007 7:50 AM

Bear in mind that " is a single character, not two of these: '

Crap, man, I have no idea what kind of keyboard you have, or even where you're getting the open/close variety of double-quote. Presuming you have a normal American/English keyboard, look at the dam' key just to the left of ENTER. It has a double quote and a single quote on it. Press SHIFT to get the double quote. Don't use some kind of editor or word-processor that substitutes in the open/close variety.

boraciner Sep 8th, 2007 7:54 AM

Ok sir , I understand what you say , I tried again then terminal said ;

I wrote these codes :

:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
int
main(int argc,char **argv) {
    int z;                  /* Status return code */
    int s[2];                /* Pair of sockets */
    /*
      * Create a pair of local sockets:
      */
    z = socketpair(AF_LOCAL,SOCK_STREAM,0,s);
    if ( z == -1 ) {
        fprintf(stderr,
            ''%s: socketpair(AF_LOCAL,SOCK_STREAM,0)\n'',
            strerror(errno));
        return 1;          /* Failed */
    }
    /*
      * Report the socket file descriptors returned:
      */
    printf(''s[0] = %d;\n'',s[0]);
    printf(''s[1] = %d;\n'',s[1]);
    system(''netstat --unix -p'');
    return 0;
}



:

bora@ciner:~/Desktop> gcc bora.c -o bora `pkg-config --cflags --libs gtk+-2.0`
bora.c:18:13: error: empty character constant
bora.c: In function ‘main’:
bora.c:18: error: invalid operands to binary %
bora.c:18: error: expected ‘)’ before ‘:’ token
bora.c:18: error: stray ‘\’ in program
bora.c:18:55: error: empty character constant
bora.c:25:12: error: empty character constant
bora.c:25: error: expected ‘)’ before ‘s’
bora.c:25: error: stray ‘\’ in program
bora.c:25:26: error: empty character constant
bora.c:26:12: error: empty character constant
bora.c:26: error: expected ‘)’ before ‘s’
bora.c:26: error: stray ‘\’ in program
bora.c:26:26: error: empty character constant
bora.c:27:12: error: empty character constant
bora.c:27: error: expected ‘)’ before ‘netstat’
bora.c:27:31: error: empty character constant



I think there are less errors then the other , but I can't compile it again.. :(

DaWei Sep 8th, 2007 7:55 AM

See my previous response. You may have additional problems, but you need to get around this one, first.

boraciner Sep 8th, 2007 8:00 AM

I have put '' (by pressing 2 times) character instead of " which is created by pressing one time. there are errors again :( but aren't as much as it was

DaWei Sep 8th, 2007 8:12 AM

Don't use two single quotes! Use one double quote!
I don't know any other way to say that, without getting unduly profane.


All times are GMT -5. The time now is 9:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC