472,950 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

C socket programming UDP

Ted
Hi all,

I am trying to learn C socket programming and I have a small program
which is a UP client.
The problem is, when I run the program, I get a runtime error -
"Invalid Argument" - from a call to sendto.

I was hoping that if someone has the time they could take a look at my
code posted below and let me know what i'm doing wrong?

Thanks in advance,

Ted

/******************************* start
***********************************/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
extern int errno;

#define LINELEN 128
#define BUFFLEN 120

struct sockaddr_in * getserveraddr ( char *, char * ) ;/* return
server add */

int main( argc, argv )
int argc;
char *argv[];
{
char *host; /*used to hold name of host */
char *service; /* used to hold port no. as a string */

if( argc == 3 ) {
host = argv[ 1 ];
service = argv[ 2 ];
}
else {
fprintf( stderr, "usage: UDPConn [ host [ port ] ]\n" );
exit( 1 );
}
UDPConn( host, service );
exit( 0 );
}

/*------------------------------------------------------------------------
* UDPConn - send input to ECHO service on specified host and print
reply
*------------------------------------------------------------------------
*/
int UDPConn( host, service )
char *host;
char *service;
{
char buff[ 120 ]="whattime";
int s; /* counter */
int sockno ; /*server socket number */
struct sockaddr_in *addrptr;
//int i;
//int now;

sockno = socket( PF_INET, SOCK_DGRAM, 0 );
if( sockno < 0 ) {
perror( "Cannot open socket...\n" );
}
(struct sockaddr *)addrptr = getserveraddr( host, service );
sendto( sockno, (char *)&buff, sizeof( buff ), 0,
(struct sockaddr *)&addrptr, sizeof( struct sockaddr) );
fprintf(stderr,"after call to sendto in
Client >> %s\n",strerror( errno ) ); //error
"invalid argu..."
....
....

return 0;
}

/*------------------------------------------------------------------------
* getsererveraddr - get server address and put it in servadd
*------------------------------------------------------------------------
*/
struct sockaddr_in * getserveraddr( char *host, char *service) /*
port number as a character string */
{
static struct sockaddr_in servadd; /* structure to hold server
address */
struct hostent *phe;
int s, type;

bzero((char *)&servadd, sizeof( servadd ) );
servadd.sin_family = AF_INET; /* TCP/IP address family*/

if ( ( servadd.sin_port = htons( (ushort)atoi( service ) ) ) == 0 )
{
fprintf( stderr, "cant get \"%s\" service entry\n", service );
//exit(NULL ); // warnings here?
}

if(phe = gethostbyname(host))
bcopy (phe->h_addr,(char*)&servadd.sin_addr,phe->h_length);
else
{fprintf(stderr, "can't get \"%s\"host entry\n",host);
// exit (NULL ); // warnings here?
} return ( &servadd);
}
Nov 14 '05 #1
4 4775
Ted wrote:
sendto( sockno, (char *)&buff, sizeof( buff ), 0,


That call is bogus, rethink what you are doing here, in particular the
relation between a pointer and an array! As a hint, let me tell you that
you don't need any casts, adding them without understanding the reasons
why was a mistake in the first place. There are more such casts throughout
your program that only serve hiding useful compiler warnings.

Uli
Nov 14 '05 #2
Ted wrote:
Hi all,

I am trying to learn C socket programming and I have a small program
which is a UP client.

There is no such thing a socket programming in ISO standard C, the
topic of this newsgroup. It looks very much like you are doing UNIX
sockets, so comp.unix.programmer is likely a good newsgroup for you.


Brian
Nov 14 '05 #3
Ted
Hi again,

Thanks to everyone for their comments and help.
I will also try the sockets, so comp.unix.programmer group.

Thanks again,
Ted

"Default User" <fi********@boeing.com.invalid> wrote in message news:<I7********@news.boeing.com>...
Ted wrote:
Hi all,

I am trying to learn C socket programming and I have a small program
which is a UP client.

There is no such thing a socket programming in ISO standard C, the
topic of this newsgroup. It looks very much like you are doing UNIX
sockets, so comp.unix.programmer is likely a good newsgroup for you.


Brian

Nov 14 '05 #4
Ted,

Not sure if you are still want to read this. I think the problem is
here.

In the call to sendto, since buff and addrptr are already pointers, you
should not pass in their address. So, the call should be

sendto( sockno, buff, sizeof( buff ), 0,
(struct sockaddr *) addrptr, sizeof( struct sockaddr) );

Also, you might want to check the errorno only after you verify the
return code of sendto is -1.

Bharath

int UDPConn( host, service )
char *host;
char *service;
{
char buff[ 120 ]="whattime";
int s; /* counter */
int sockno ; /*server socket number */
struct sockaddr_in *addrptr;
//int i;
//int now;

sockno = socket( PF_INET, SOCK_DGRAM, 0 );
if( sockno < 0 ) {
perror( "Cannot open socket...\n" );
}
(struct sockaddr *)addrptr = getserveraddr( host, service );
sendto( sockno, (char *)&buff, sizeof( buff ), 0,
(struct sockaddr *)&addrptr, sizeof( struct sockaddr) );
fprintf(stderr,"after call to sendto in
Client >> %s\n",strerror( errno ) ); //error
"invalid argu..."
...
...

return 0;
}


Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
0
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
3
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.