Solved

Simple Windows UDP Client Example

Posted on 2000-02-25
2
746 Views
Last Modified: 2013-12-03
Where can I find a really basic simple example of how to create a UDP client which can connect and send string data for Windows NT/95/98?
0
Comment
Question by:mr_walsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 50 total points
ID: 2560232
Here you can find alot...
http://www.sockaddr.com/ExampleSourceCode.html

Here a sample:
//This code connects to a UDP server //listening on port SERVER_PORT.
#include <stdio.h>
#include <winsock.h>
#include <string.h>
#include <stdlib.h>
#include<windows.h>

#define SERVERPORT 5555
#define CLIENTPORT 5554

int SockId=0;
static int iCount=0;


int InitialiseClient(void)
{
    int error;
SOCKADDR_IN SockData;
SOCKADDR_IN SockSock;
    int sockid;
    WORD wVersionRequested;
WSADATA wsaData;
    wVersionRequested = MAKEWORD( 2, 0 );
    error = WSAStartup( wVersionRequested, &wsaData );
   if ( error != 0 ) {exit(0);}

SockData.sin_family = AF_INET;
SockData.sin_port = htons( CLIENTPORT );
    SockData.sin_addr.s_addr=htonl(INADDR_ANY);

sockid = socket ( AF_INET , SOCK_DGRAM ,0);

if ( sockid == -1 )
{
    printf ( "\n Socket Failed %d\n",WSAGetLastError() );
exit ( 1 );
}
error = bind ( sockid , (struct sockaddr *)&SockData , (int ) sizeof ( SockData ) );
if ( error )
{
    printf ( "\n  Bind Failed %d \n" ,WSAGetLastError());
closesocket( sockid );
    exit(1);
}

SockSock.sin_family = AF_INET;
/*Connect to server at IP:192.168.174.32 on port SERVER_PORT*/
SockSock.sin_port = htons(SERVER_PORT );
    SockSock.sin_addr.s_addr=inet_addr("192.168.174.32");

int iState=connect ( sockid,(sockaddr FAR*)&SockSock,sizeof(SockSock));    
printf("State of connection =%d ",iState);fflush(stdout);
return sockid;
}



void main(void )
{
    char command[256];
    char header[2],result[256];
int iCount=1;
SockId=InitialiseClient();

    header[0]=1;
command[0]=iCount;
header[1]=(char)1;
header[2]=0;

    strcpy(&command[1],"KILL PROCESS");

if(send(SockId,header,1,0)!=1)
{
printf("Serious Problem header");
}
printf(" Problem header=%s data=%d",header,strlen(header));


int Data=recv(SockId,result,1,0);

if(Data!=1)
{
    printf("Recv :Len=%d LineLen=%d Error=%d",Data,2,WSAGetLastError());
    exit(1);
}

Regards
W.Yinan

0
 

Author Comment

by:mr_walsh
ID: 2571405
Thanks for the quick, concise answer.  --matt
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

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