?
Solved

How can I send a packet with c++? [example required]

Posted on 2010-01-08
5
Medium Priority
?
1,026 Views
Last Modified: 2013-11-13
How can I create and send a UDP and/or TCP packet (stream) from c++? I'm compiling using MingW in Code::Blocks IDE, on Windows Vista 64-bit.

In my previous question (http://www.experts-exchange.com/Programming/Languages/CPP/Q_25001051.html) I was not very clear; so to be more precise, please provide an actual compilable example (as per my specs). Please do not link to any other sites, paste directly or attach a .zip or of the build-able code.

Thanks in adance!
0
Comment
Question by:level9wizard
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:CSecurity
ID: 26210367
You want for Windows Only, right?
0
 
LVL 11

Author Comment

by:level9wizard
ID: 26211150
[CSecurity]>>You want for Windows Only, right?

For now; yes, specifically (as I've mentioned) using MingW in Code::Blocks IDE, on Windows Vista 64-bit.
0
 
LVL 17

Accepted Solution

by:
CSecurity earned 2000 total points
ID: 26211244
Don't forget to link against ws2_32 library
#include <stdio.h>
#include <winsock2.h>

int main()
{

WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");

SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_DGRAM, 0 );

if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return 0;
}

// Connect to a server.
sockaddr_in clientService;

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );

if ( connect( m_socket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return 0;
}

// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";

bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );

while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 32, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return 0;
printf( "Bytes Recv: %ld\n", bytesRecv );
}

return 0;
}

Open in new window

0
 
LVL 11

Author Closing Comment

by:level9wizard
ID: 31674540
This example really only shows how to create a socket; a higher layer than forming a packet itself - correct?
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 26293720
Yes, for forming packet itself you need WinPcap
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question