Important alert: (current site time 7/15/2013 6:40:16 PM EDT)
 

VB icon

Send a TCP packet to a server

Email
Submitted on: 8/12/2000 5:07:21 AM
By: Markus Delves  
Level: Beginner
User Rating: By 6 Users
Compatibility: C
Views: 66945
 
     /* 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 to 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 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". */
// By: Markus Delves
//
// Inputs:usage: program_name <ip address> <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/vb/scripts/ShowCode.asp?txtCodeId=717&lngWId=3//for details.//**************************************

int sock;
// Start main with command line arguments
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' address
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 tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

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 PMJeffTM

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.
(If this comment was disrespectful, please report it.)

 
9/26/2000 6:08:43 PMUltimatum

DJGPP has all those files.
(If this comment was disrespectful, please report it.)

 
9/27/2000 9:17:51 PMeslipak@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
(If this comment was disrespectful, please report it.)

 
10/13/2000 4:20:21 AMIamDeth

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.
(If this comment was disrespectful, please report it.)

 
10/16/2000 3:25:28 AMiamdarkphoenix@yahoo.com

sir:
im a newbie. in your program submittion: tcp packet, inoticed the include directives for:
#include
#include
#include
#include
#include
#include
#include

and im asking where do i see these library files?

(If this comment was disrespectful, please report it.)

 
11/19/2000 6:27:08 PMMarkus 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
(If this comment was disrespectful, please report it.)

 
12/17/2000 12:13:01 AMWolvenWraith

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
(If this comment was disrespectful, please report it.)

 
8/6/2001 12:52:32 PMZecho

Don't worry.. it doesnt run under linux either
(If this comment was disrespectful, please report it.)

 
1/27/2002 9:21:16 AMdave

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)

(If this comment was disrespectful, please report it.)

 
7/9/2002 4:43:43 AMAbhijeet

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 !

(If this comment was disrespectful, please report it.)

 
8/12/2002 3:06:38 AMRaymond

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!
(If this comment was disrespectful, please report it.)

 
8/14/2003 3:06:58 PM

I'm using VBC++ 6.0 limited edition to run this program and the result is error. Could you tell me why
(If this comment was disrespectful, please report it.)

 
12/3/2003 12:58:30 PM

$ g++ -g -Wall -ogmsTCP1 gmsTCP1.cpp
gmsTCP1.cpp: In function `int main(int, char**)':
gmsTCP1.cpp:72: parse error before `=' token
gmsTCP1.cpp:77: `inet' undeclared (first use this function)
gmsTCP1.cpp:77: (Each undeclared identifier is reported only once for each
function it appears in.)
gmsTCP1.cpp:77: `ADDR_NONE' undeclared (first use this function)

(If this comment was disrespectful, please report it.)

 
6/24/2007 6:07:25 AMkronso.23

Doesn't work for me either, tons of missing files, and I'm even using MinGW's GCC compiler on Windows. Crappy code.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.