473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4806
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: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.