From: s9327620 on
Dear All :
I have some problem in UDP clinet programming, my source code
under line.

#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<stdlib.h>

#define PORT 65536
#define SERVER_IP "10.l10.138.200"

main()
{
int s,len;
struct sockaddr_in addr;
int addr_len = sizeof(struct sockaddr_in);
char buffer[256];
if ((s = socket(AF_INET, SOCK_DGRAM, 0))<0)
{
perror("socket");
exit(1);
}
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(SERVER_IP);
while(1)
{
bzero(buffer, sizeof(buffer));
len=read(STDIN_FILENO, buffer, sizeof(buffer));
sendto(s, buffer, len, 0, &addr, addr_len);
len = recvfrom(s, buffer, sizeof(buffer), 0, &addr, &addr_len);
printf("receive: %s",buffer);
}
}

But I complier in Red Hat 9 have three bug
udpclient1.C: In function 'int main()'
udpclient1.C:34: cannot convert 'sockaddr_in*' to 'const sockaddr*'
for argument '5' to 'ssize_t sendto(int, const void*, unsigned int,
int, const sockaddr* . unsigned int)'
udpclient1.C:35: cannot convert 'sockaddr_in*' to 'const sockaddr*'
for argument '5' to 'ssize_t recvfrom(int, void*, unsigned int, int,
const sockaddr* . socklen_t*)'

Does somebody have an idea to resolve my problem?
Thank you.

From: ynotssor on
In news:1172538974.957648.123840(a)z35g2000cwz.googlegroups.com,
s9327620(a)csie.cyut.edu.tw <s9327620(a)csie.cyut.edu.tw> wrote:

> I have some problem in UDP clinet programming, my source code
> under line.
....
> #define SERVER_IP "10.l10.138.200"
....
> But I complier in Red Hat 9 have three bug
> udpclient1.C: In function 'int main()'
> udpclient1.C:34: cannot convert 'sockaddr_in*' to 'const sockaddr*'
> for argument '5' to 'ssize_t sendto(int, const void*, unsigned int,
> int, const sockaddr* . unsigned int)'
> udpclient1.C:35: cannot convert 'sockaddr_in*' to 'const sockaddr*'
> for argument '5' to 'ssize_t recvfrom(int, void*, unsigned int, int,
> const sockaddr* . socklen_t*)'
>
> Does somebody have an idea to resolve my problem?

Try fixing the typo in SERVER_IP.
From: Lew Pitcher on
On Feb 26, 8:16 pm, s9327...(a)csie.cyut.edu.tw wrote:
> Dear All :
> I have some problem in UDP clinet programming, my source code
> under line.
>
> #include<fcntl.h>
> #include<unistd.h>
> #include<sys/types.h>
> #include<sys/socket.h>
> #include<arpa/inet.h>
> #include<netinet/in.h>
> #include<stdlib.h>
>
> #define PORT 65536

/*
** since port numbers range from 1 to 65535,
** the original port number of 65536 is out of range
** and will not work. Use a different port
*/
#define PORT 65534

> #define SERVER_IP "10.l10.138.200"

/*
** The server IP must either be a legal dotted decimal
** number, or a DNS name. The original is neither.
** Use a corrected dotted decimal IP address
*/
#define SERVER_IP "10.10.138.200"


> main()
> {
> int s,len;
> struct sockaddr_in addr;
> int addr_len = sizeof(struct sockaddr_in);
> char buffer[256];
> if ((s = socket(AF_INET, SOCK_DGRAM, 0))<0)
> {
> perror("socket");
> exit(1);}
>
> bzero(&addr, sizeof(addr));
> addr.sin_family = AF_INET;
> addr.sin_port = htons(PORT);
> addr.sin_addr.s_addr = inet_addr(SERVER_IP);
> while(1)
> {
> bzero(buffer, sizeof(buffer));
> len=read(STDIN_FILENO, buffer, sizeof(buffer));
> sendto(s, buffer, len, 0, &addr, addr_len);


/*
** sendto() takes a generic sockaddr pointer, but we have a
** TCP/IP sockaddr_in pointer. Cast the sockaddr_in pointer to
** the generic sockaddr pointer, and continue.
*/
sendto(s, buffer, len, 0, (struct sockaddr *)&addr, addr_len);

> len = recvfrom(s, buffer, sizeof(buffer), 0, &addr, &addr_len);


/*
** recvfrom() takes a generic sockaddr pointer, but we have a
** TCP/IP sockaddr_in pointer. Cast the sockaddr_in pointer to
** the generic sockaddr pointer, and continue.
*/
len = recvfrom(s, buffer, sizeof(buffer), 0 (struct sockaddr
*)&addr, &addr_len);

> printf("receive: %s",buffer);
>
> }
> }
>
> But I complier in Red Hat 9 have three bug
> udpclient1.C: In function 'int main()'
> udpclient1.C:34: cannot convert 'sockaddr_in*' to 'const sockaddr*'
> for argument '5' to 'ssize_t sendto(int, const void*, unsigned int,
> int, const sockaddr* . unsigned int)'
> udpclient1.C:35: cannot convert 'sockaddr_in*' to 'const sockaddr*'
> for argument '5' to 'ssize_t recvfrom(int, void*, unsigned int, int,
> const sockaddr* . socklen_t*)'

HTH
--
Lew

From: s9327620 on
I modify
sendto(s, buffer, len, 0, (struct sockaddr *)&addr, addr_len) and
len = recvfrom(s, buffer, sizeof(buffer), 0 (struct sockaddr *)&addr,
&addr_len) bug , but have some new bug

udpclient1.C: In function 'int main()';
udpclient1.C: 37: invaild conversion from 'int*' to 'socklen_t*'

please help me

From: David Schwartz on
On Feb 26, 6:56 pm, s9327...(a)csie.cyut.edu.tw wrote:
> I modify
> sendto(s, buffer, len, 0, (struct sockaddr *)&addr, addr_len) and
> len = recvfrom(s, buffer, sizeof(buffer), 0 (struct sockaddr *)&addr,
> &addr_len) bug , but have some new bug
>
> udpclient1.C: In function 'int main()';
> udpclient1.C: 37: invaild conversion from 'int*' to 'socklen_t*'
>
> please help me

You want 'addr_len' to be a 'socklen_t', not an 'int'.

DS

 | 
Pages: 1
Prev: Which ICMP reject works best
Next: teddy