Thread: Socket Programming in C

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    Socket Programming in C

    Trying to program a stop and wait network protocol in C which will send a packet in the form of a struct

    Code:
    struct MyHdr{
    	unsigned char seq;  //sequence number
    	unsigned char wndwsize;  //size of the send window
    	unsigned char pcktsize;   //packet size
    	data char[sizeof pcktsize];    //data array size of packetsize (-overhead)
    };
    I have been reading up on the send() and recv() classes but still don't have enough knowledge to know how to send a "packet" over. Can someone clarify how to do this with me?

    Here's what I have been playing with

    Code:
    	
    
    while(fread(buffer,1,64,in) !=0){
    		struct MyHdr packet;
    		
    		resent =0;
    		count++;
    		
    		packet.pcktsize = buffer;//copy buffer into our packet 64bytes
    		packet.seq = count;
    		packet.wndwsize = count;
    		
    		do
    		{
    		//something with a CRC
    		//
    		}
    
    		send(sockfd,packet);
    
    	}
    Can I just send with the given socket and the packet? Also how would I receive it?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    can someone move this to the C programming forum?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TaiL View Post
    I have been reading up on the send() and recv() classes but still don't have enough knowledge to know how to send a "packet" over. Can someone clarify how to do this with me?
    They are not classes. They are functions.

    Before you can do something special in the way that you want it, you may have to be able to do something normal and basic. So trying going through a simple socket/networking tutorial and write a simple client and server program or something. They use send and recv. Then you will have the understanding you need to proceed onto constructing, transmitting, and deconstructing packets.

    Right now, you do not have this basic understanding, so there is not much that can be said, I think.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You do not need to worry about the header portion of the packet; this will be done for you. All you need to worry about is the packet payload (the data you are sending and receiving).

    In other words, something like this is perfectly valid:
    Code:
    send(fd, "here is some data", 18, 0);
    From your code, it's clear that you need to read the documentation on the send() function. You do not need to worry about CRCs, sequence numbers, or window sizes.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    writing dhcp client using sockets

    Hi all,

    Can anybody give me a guideline for writing a DHCP client using socket programming on Linux platform?

    Any kind of help, suggestion of good books and links will be highly appreciated ...

    Thanks and regards,
    Dash

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    netlink sockets..

    what are netlink sockets. When and why they are used.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    "me too" bumpers should start a new thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 07:40 PM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM