Thread: Sending Integer Via C Socket

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14

    Sending Integer Via C Socket

    Please Varify my Method .
    I am not getting expected output ..

    Code:
    int Amount=500;
    send(newSocket,Amount,4,0);
    close(welcomeSocket);
    
      recv(clientSocket,Amount,4,0);
      // Amount= ntohl(Amount);
       printf("Data received: %d",atoi(Amount[0]));

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by SimplySud View Post
    Please Varify my Method .
    I am not getting expected output ..
    The parameter of 'send' and 'recv' are wrong.
    Attempt this:
    Code:
    int Amount=500;
    send(newSocket, &Amount, sizeof(Amount), 0);
    close(welcomeSocket);
    
    recv(clientSocket, &Amount, sizeof(Amount), 0);
    // Amount= ntohl(Amount);
    printf("Data received: %d\n",Amount));
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14
    sudhakar@sudhakar-Aspire-E1-570:~/Desktop$ ./Receiver -m 0 -p 5000 -z 256 -P t
    *** Error in `./Receiver': munmap_chunk(): invalid pointer: 0x00007fff7725f2a0 ***
    Aborted (core dumped)


    Error is coming after applying above code

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The error is probably in code unrelated to what you have shown. There is nothing in the code WoodSTokk provided that would cause a pointer related problem.

    Incidentally, the original code as you showed it here would not have even compiled.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    n = write(sockfd,buffer,packet_size);

    I used this socket to send traffic .Is there any problem with using same socket for another communication ,What I am doing righ now else I should create a separate socket

  6. #6
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28
    Wait what? I haven't really played with the sys/socket.h stuff but I have done some socket programming in delphi. A RAT tool that I used in class Just speaking from memory. Usually you'll create a socket to either connect to a host ( yourself or any other address ) , this socket cannot be used for hosting a server ( bare with me ) - You'll get an error if you try to bind a socket that is already connected to a server so usually you either destroy ( a delphi destructor ) the socket that is currently in use and then use bind to create a server. I'm not sure whether close(socket);

    I wouldn't play around with sockets, read up on them first they are far from easy. I didn't do much reading on this because well I have things to do and if this really means anything to you I hope you'll research it yourself. If you really want to get into sockets take a look at threading first. Especially if your going to be writing a server program ^^. They are huge fun but can also cause all sorts of trouble.

    I suspect the close function only terminates the connection made to/with the socket. So I suppose if you close the socket using it for another operation should be fine (assuming you thereafter use bind/connect for the right situation XD ) .

    A reference to the close() function : The GNU C Library: Closing a Socket
    Last edited by RagingGrim; 12-24-2014 at 09:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending matrix over UDP socket
    By n.a.s in forum C Programming
    Replies: 3
    Last Post: 05-21-2014, 10:33 AM
  2. Sending files true tcp socket
    By Alix in forum C Programming
    Replies: 8
    Last Post: 01-05-2012, 02:14 PM
  3. Socket Overflow by sending / receiving ACK's
    By MaSSaSLaYeR in forum C Programming
    Replies: 13
    Last Post: 11-24-2011, 02:38 AM
  4. sending in socket
    By fairyjerry14 in forum C Programming
    Replies: 4
    Last Post: 10-08-2007, 07:11 AM
  5. sending zero bytes over TCP socket
    By nantonop in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-03-2007, 08:10 AM

Tags for this Thread