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

Posted on 2010-01-08
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 ( 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!
Question by:level9wizard
    LVL 17

    Expert Comment

    You want for Windows Only, right?
    LVL 11

    Author Comment

    [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.
    LVL 17

    Accepted Solution

    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() );
    return 0;
    // Connect to a server.
    sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "" );
    clientService.sin_port = htons( 27015 );
    if ( connect( m_socket, (SOCKADDR*) &clientService,
    sizeof(clientService) ) == SOCKET_ERROR) {
    printf( "Failed to connect.\n" );
    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");
    if (bytesRecv < 0)
    return 0;
    printf( "Bytes Recv: %ld\n", bytesRecv );
    return 0;

    Open in new window

    LVL 11

    Author Closing Comment

    This example really only shows how to create a socket; a higher layer than forming a packet itself - correct?
    LVL 17

    Expert Comment

    Yes, for forming packet itself you need WinPcap

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now