Re: socket prog.

Discussion in 'C++' started by Victor Bazarov, Jul 11, 2003.

  1. "shandy" <> wrote...
    > well i'am working in my poject and got a problem in socket
    > programming,
    > well as i don't have any network in home so i can't use it but i tried
    > to do it with loopback addressing , but was unable to execute it .


    What seemed to be the symptom?

    > here's copy of my code plz tell the problem


    I cannot, really. Perhaps you can tell what the symptoms are
    and then we will redirect you to the newsgroup where they deal
    network programming (yes, comp.lang.c++ is NOT the one).

    I will make suggestions on C++ only.

    >
    >
    > #include<iostream.h>


    The standard header is <iostream>

    > #include<conio.h.>
    > #include<winsock.h>


    The two headers above are non-standard. Use of them constitutes
    either OS-specific or compiler-specific code. No guarantees from
    the C++ POV.

    >
    > void main()


    int main()

    > {
    >
    > int sockfd=0,len=0;
    > char j;
    > struct sockaddr_in dest_addr;


    In C++ one doesn't need to use 'struct' when declaring an object
    of that type. This is not an error, just something that you
    probably learned when programming in C. Or you copied the code
    from a C program.

    'sockaddr_in' is a non-standard type. This newsgroup presumes
    no knowledge of its contents and behaviour.

    > cout<<"server client message passing";
    > sockfd=socket(AF_INET,SOCK_STREAM,0);//socket call


    'socket' is not a standard function. No guarantees here.

    > dest_addr.sin_family=AF_INET;
    > dest_addr.sin_addr.s_addr=inet_addr("127.0.0.0");


    Since 'sockaddr_in' is a non-standar type, nothing can be said
    about the correctness of the code above. 'inet_addr' looks
    like a function. AF_INET looks like a macro. Neither is part
    of the standard C++ language.

    > memset(&(dest_addr.sin_zero),'\0',8);


    This is fine as long as 'dest_addr's member 'sin_zero' is
    a POD and its size is not greater than 8 bytes.

    > connect(sockfd,(struct sockaddr*)&dest_addr,sizeof(struct
    > sockaddr));//connect call


    'connect' is not a standard function.

    > char* msg="hi";


    In C++ it is customary to use 'const' when declaring a character
    pointer that points to a literal. Although not using 'const' is
    allowed, it is not a good style. I'd do

    char msg[] = "hi";

    > len=strlen(msg);
    > while(j!=27)


    Now, this is a HUGE problem, right here. 'j' has NOT be set to
    any value. It contains random bits, and it _may_ for all it's
    worth have value 27. You should think of initialising _all_
    your variables.

    > {
    > send(sockfd,msg,len,0);//send call


    'j' is not changing in this loop. If at the beginning 'j' does
    not have value 27, the program will never stop calling 'send'.

    > }
    >
    > while(j!=27)


    If the loop above ever stops executing, it will mean that 'j'
    _acquired_ value 27. That means that this loop will NEVER start
    executing because 'j' is already 27 here. Logical error, no
    doubt. You need to review what your program is doing.

    > {
    > recv(sockfd,msg,len,0);//recive call


    Again, 'j' is not changing during the execution of this loop.
    'recv' is a non-standard function. I have no comment on its
    use or mis-use.

    > }
    >
    > getch();


    Non-standard function again.

    > }


    All in all, the program _may_ be OK, except for several notes,
    especially the fact that 'j' is uninitialised. Fix them and
    try again.

    Also, try replacing all network-specific calls with simple
    'cout' statements just to verify that your program flows as you
    want it to. Then begin debugging the actual network. And we
    cannot help you there because network programming is not part
    of C++ language. You will need to ask in a newsgroup that deals
    with your OS (Windows? comp.os.ms-windows.programmer).

    Also, read the FAQ.

    Victor
     
    Victor Bazarov, Jul 11, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mei Li
    Replies:
    3
    Views:
    616
    Thomas Schodt
    Jul 20, 2004
  2. -intl.com
    Replies:
    1
    Views:
    383
    Martin Gregorie
    Oct 22, 2006
  3. Laszlo Nagy
    Replies:
    1
    Views:
    5,117
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,033
    Jean-Paul Calderone
    Jan 27, 2009
  5. rampc

    vxworks socket prog

    rampc, Dec 20, 2011, in forum: C Programming
    Replies:
    0
    Views:
    534
    rampc
    Dec 20, 2011
Loading...

Share This Page