Socket Programming Introduction Source Code

This is a discussion on Socket Programming Introduction Source Code within the Linux Programming forums, part of the Platform Specific Boards category; BROUGHT TO YOU BY http://www.linuxsocket.org/ soooo many people have asked or had problems with BASIC Socket Programming, might as well ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Socket Programming Introduction Source Code

    BROUGHT TO YOU BY
    http://www.linuxsocket.org/

    soooo many people have asked or had problems with BASIC Socket Programming, might as well post a template source code right?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <resolv.h>
    #define PORT_HTTP	81
    #define PORT_TIME	13
    #define PORT_FTP	21
    #define SERVER_ADDR	"127.0.0.1"
    #define MAXBUF		1024
    
    int main(int argc, char* argv[])
    {	int acc;
    	int sockfd;
    	struct sockaddr_in dest;
    	char buffer[MAXBUF];
    	
    	/* Open socket for streaming */
    	if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    	{
    		perror("Socket");
    		exit(errno);
    	}
    	/* Init Server Address/port struct */
    	bzero(&dest,sizeof(dest));
    	dest.sin_family = AF_INET;
    	if(argv[1] == NULL)
    		dest.sin_port = htons(PORT_FTP);
    	else
    	{
    		scanf("%d",&acc);
    		dest.sin_port = htons(acc);
    	}
    	if ( inet_aton(SERVER_ADDR, &dest.sin_addr.s_addr) == 0)
    	{
    		perror(SERVER_ADDR);
    		exit(errno);
    	}
    	if( connect(sockfd, (struct sockaddr*)&dest,sizeof(dest)) != 0)
    	{
    		perror("Connect ");
    		exit(errno);
    	}
    	
    	bzero(buffer,MAXBUF);
    	recv(sockfd,buffer,sizeof(buffer),0);
    	printf("%s",buffer);
    
    	close(sockfd);
    	return 0;
    }
    Slightly modified because I am working on a scanner but here is some code that opens up FTP and stuff...
    When I finish the scanner, I will post that too.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: Socket Programming Introduction Source Code

    Er, just some thoughts:
    Code:
    #define PORT_HTTP	81    /* Port 80 is the reserved HTTP port */
    
    bzero(&dest,sizeof(dest));  /* bzero() has been deprecated.  Use memset(). */ 
    Also, what is this?
    Code:
    bzero(buffer,MAXBUF);
    recv(sockfd,buffer,sizeof(buffer),0);
    printf("%s",buffer);
    Your first use of buffer is to zero it out. Save yourself the function call and initialize 'buffer' with:
    Code:
    char buffer[MAXBUF] = { 0 };
    Since you know the size of the buffer (MAXBUF), why call sizeof()? Also, how will the user know if recv() fails?

    [EDIT]
    Here is a socket tutorial written a while back that goes into more detail.
    [/EDIT]
    Last edited by Deckard; 04-30-2002 at 05:38 AM.
    Jason Deckard

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    This isn't my source code
    it is Linux SOcket's
    Mine is:
    http://cyber.homeunix.net:81/c/scan.c

    I am still new to Network Programming but I am a SEMI_PRO at Kernel Modules...

    Still need to find more time to read all the information out there, thanks, when I rewrote my client.c (Linux Socket's name for that program) I noticed that too, well some things
    Last edited by Lynux-Penguin; 04-30-2002 at 05:31 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Type' Error on Build of Officially Released Source Code
    By Jedi_Mediator in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2008, 05:28 PM
  2. How to make a program that prints it's own source code???
    By chottachatri in forum C++ Programming
    Replies: 38
    Last Post: 03-28-2008, 07:06 PM
  3. Replies: 4
    Last Post: 01-18-2008, 06:05 PM
  4. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  5. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM

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