Why the simple Socket Snippet doesn't work?

This is a discussion on Why the simple Socket Snippet doesn't work? within the Networking/Device Communication forums, part of the General Programming Boards category; 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);...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

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, 07:03 PM
  2. Replies: 7
    Last Post: 11-15-2007, 12: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, 10:05 AM
  5. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 09:33 AM

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