Thread: Why the simple Socket Snippet doesn't work?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why the simple Socket Snippet doesn't work?

    Sender side:
    Code:
    int len = strlen(str);
    write(sock,&len,sizeof(int));
    write(sock,str,len);
    Receiver Side:
    Code:
    int len;
    read(sock,&len,sizeof(int));
    char* str=(char*)malloc(len*sizeof(char));
    read(sock,str,len);

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what are return values of write and read?
    what does it mean - "doesn't work" - how do you check it?

    PS. And this is C, why to post on C++ board?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    maybe because write() and read() aren't part of the socket API !?

    also, what OS? I don't see any of the initialization or setup functions in that snippet. without some more context information it could be anything, a bad pointer, a bad socket, unitialized socket stack, improperly installed drivers, poor feng shui, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program won't work on a friend's comp
    By Ouendanation in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2008, 08:03 PM
  2. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  3. Novice Beginner: Simple hello world wont work
    By hern in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2005, 12:16 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM