TCP,purpose,article,help,people,know,basics,w
Quick Search for:  in language:    
TCP,purpose,article,help,people,know,basics,w
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 451,578 lines
 Jobs: 558 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
Url Test
By Richard Creary on 8/27


Nice Console Calc
By Andrew Carter on 8/26


Visual Pi Hex 2
By jo122321323 on 8/26


Cascade Clone v1.0
By Paulo Jorente - aka JungleBoy on 8/26


Bubble Sort Algo
By d1rtyw0rm on 8/26


Copy File I/O
By Olivier Bastien on 8/25


Visual Pi Hex
By jo122321323 on 8/25


Click here to see a screenshot of this code!ShortCutSample
By Massimiliano Tomassetti on 8/25

(Screen Shot)

AnPMoneyManager beta
By Anthony Tristan on 8/24


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Send a TCP packet to a server

Print
Email
 
VB icon
Submitted on: 8/12/2000 5:07:21 AM
By: Markus Delves  
Level: Beginner
User Rating: By 3 Users
Compatibility:C

Users have accessed this code 13100 times.
 
 
     /* The purpose of this article is to help out people who know the basics of C but want to start learning TCP controls in C. This program will connect to a server and send a TCP packet containing "La la la la". */
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

//**************************************
//     
//INCLUDE files for :Send a TCP packet t
//     o a server
//**************************************
//     
/* The Includes */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: Send a TCP packet to a server
// Description:/* The purpose of this ar
//     ticle is to help out people who know the
//     basics of C but want to start learning T
//     CP controls in C. This program will conn
//     ect to a server and send a TCP packet co
//     ntaining "La la la la". */
// By: Markus Delves
//
// Inputs:usage: program_name <ip add
//     ress> <port>
//
// Returns:The program will tell you if 
//     it was successful or not
//
// Side Effects:None known
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/xq/ASP/txtCod
//     eId.717/lngWId.3/qx/vb/scripts/ShowCode.
//     htm//for details.//**************************************
//     

int sock;
// Start main with command line argument
//     s

    int main(int argc, char *argv[]) {
    // Get ready for the TCP stuff
    struct hostent *he; // Used for DNS lookup
    struct sockaddr_in blah; // inet addr stuff
    // Create a varible for our packet
    // Remember, TCP packets max at 1024
    char packet[1024];
    // A varible to hold the servers' addres
    //     s
    char *address;
    // A varible for the port
    int port;
    // Extra vars
    int i;
    		// Make sure two arguments were supplied
        		if (argc != 3) {
        		// Print how-to use the program then exit
        		fprintf(stderr, "usage: %s <ip address> <port>\n",argv[0]);
        		return(-1);
        		}	
        		// We know there are two arguments
        		// so let's use them.
        		address = argv[1];
        		port = atoi(argv[2]);
        		// Create the unconnected socket
        		sock = socket (AF_INET, SOCK_STREAM, 0);
        		//Set some settings
        		blah.sin_family = AF_INET; //we're using inet
        		blah.sin_port = htons (port) //set the port
        		he = gethostbyname (address); //set the address
        		fprintf(stderr, "Attempting a connection with %s on port %d\n", address, port);
        		// Is the ip/hostname working?
        		if (!he) 
            		{
            		if ((blah.sin_addr.s_addr = inet.addr (address)) == ADDR_NONE)
            		return(-1);
                		} else {
                		bcopy (he->h_addr, (struct in_addr *) &blah.sin;_addr, he->h_length);
            }

// Did they accept us? if (connect (sock, (struct sockaddr *) &blah;, sizeof (blah)) < 0) { fprintf(stderr, "Connection refused by remote host.\n"); return(-1); }
//Create the packet sprintf(packet, "La la la la"); //And send it write (sock, packet, strlen(packet)); close (sock); // Close the connection fprintf(stderr, "Operation Completed. Exiting..."); }


Other 1 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
9/19/2000 10:07:02 PM:JeffTM
Yes, Iamdarkphoenix (Starcraft Fan :)) 
is right, where are the header files 
you mention?  i'm a newbie too, but 
MSVC++ does not have these files.  I 
looked them up in the MSVC++ library, 
and it doesn't exist.  Do you use Linux 
files?  If so, please mention that next 
time.  Thanks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/26/2000 6:08:43 PM:Ultimatum
DJGPP has all those files.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/27/2000 9:17:51 PM:eslipak@intramed.net.ar
Dear Sir: can you email me the required 
include files needed for your program?. 
i have not access to DJGPP.
Thanks a 
lot
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/13/2000 4:20:21 AM:IamDeth
I belive that this code has been 
written for UNIX/Linux system. I think 
you could compile/link if you download 
gcc for DOS/Windows.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/16/2000 3:25:28 AM:iamdarkphoenix@yahoo.com
sir:
  im a newbie. in your program 
submittion: tcp packet, inoticed the 
include directives for:
#include 
<netdb.h>
#include 
<sys/socket.h>
#include 
<netinet/in.h>
#include 
<netinet/in_systm.h>
#include 
<netinet/ip.h>
#include 
<netinet/tcp.h>
#include 
<arpa/inet.h>
and im asking where do 
i see these library files?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/19/2000 6:27:08 PM:Markus Delves
IamDeth is right, this was coded and 
tested in a *nix environment. I have no 
idea if it'll work in windows or 
MSVC++. Try using gcc for windows/dos 
or DJGPP
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/17/2000 12:13:01 AM:WolvenWraith
Great! but I need to know how to 
receive packets too :P Trying to port a 
simple chat program to linux... Packet 
Sniffers are great
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/6/2001 12:52:32 PM:Zecho
Don't worry.. it doesnt run under linux 
either
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/27/2002 9:21:16 AM:dave
it wont compile even in linux !!! 
(GCC)
tcp.c: In function 
`main':
tcp.c:82: parse error before 
"he"
tcp.c:83:41: warning: multi-line 
string literals are 
deprecated
tcp.c:90: `inet' undeclared 
(first use in this function)
tcp.c:90: 
(Each undeclared identifier is reported 
only once
tcp.c:90: for each function 
it appears in.)
tcp.c:91: `ADDR_NONE' 
undeclared (first use in this 
function)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/9/2002 4:43:43 AM:Abhijeet
Excellent! Nice job. keep it up. Do u 
have any site which is downloadable. 
i.e. I am a cybercafe user. It would be 
a great help. Once again - thanks for 
sharing the code !
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/12/2002 3:06:38 AM:Raymond
Dear all,
I am doing a project 
involves setting a server to accept 
text data from a client and transmit 
voice data back to the client. Who have 
the source codes relavent to such kind 
of server? If yes, please kindly send 
to me through 
badguyzero@yahoo.com
Thanks very much 
for your great help!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.